From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.1 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 29668 invoked from network); 15 Oct 2021 12:20:21 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 15 Oct 2021 12:20:21 -0000 Received: (qmail 18292 invoked by uid 550); 15 Oct 2021 12:20:18 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 18250 invoked from network); 15 Oct 2021 12:20:17 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=smartx-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=QBgBrWlwqbw0gGEmDPx0xTCSbXEerm1rrz+cn3KCzBE=; b=yZPfN9BGw+bE6CaSaq0rXYz+RbohNI9DuXIaZ17rG+2cGWSI+7C1mdrFAVRzw6nlab UZMegQa9TEOoMMtlVQIzcbusZOYOrGUt/noR4cHbKI0OeMylKdlA64rJQ5B8Cp8FR7Cw /lzWd4mSgsZUXSpgUItIhsaA5QGM/oNskM1EhOVH0QKCM0EkJm/os0n485BSbhhZomGy hdig1B/zrUpko1GD9KEydqQ6DJ5dbNBhZVA2l5OqM41JrubUaWaKu1170Tgy7Gw4B+UC 2OckaS2+zPEG/t8QyR42UN2A+4TjWvZpAUzpvz4PT7ItXSs6QL5PjcT9r23/giwGqLOT e7Zw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=QBgBrWlwqbw0gGEmDPx0xTCSbXEerm1rrz+cn3KCzBE=; b=D3Y/PjXS16we+N1Jzp1ICncCjJgXM/HesOazq+qb6QVCBi9bbyyOlgQAbuhcyqiJ7Q OjnN6uCpp+OpbO5/WwuduC1AuMIWfdQBF58l0cf3Pmt9zeqxbw1fWZ7zbZ26ZR6TCXmg om4oRT0t+hJOHFPvFhyrq51Zac2GZAOlNMCzkw6NLFlQkqeE1lN/uFujQsHUo7PwQhco c57YBuNluU24VJMJvViFZfWUVZzfBSVL8ZPubc7b0jKy00E5JgmFHWQ3WHyqB3Ii6FiI l9G5TU6TDLeM4BL0ykSt+8NzhzVaE8J0rXQTkFzDyOLDrzbMPvlbTxiTJ1hwGUqBM1Bx UH9Q== X-Gm-Message-State: AOAM532xnHNacHrlzxTQNeJ+VGZgbsKHSha3CBewctqQj5eL6muM3e0i BrU6gN5Q9W8OqwQ91lbYnbQ2j/T5Vt3vyh5OF4U= X-Google-Smtp-Source: ABdhPJwayebi9YXIGj9gLMFNY/2fdyPmYkMEYRownkpYjkA/fhR3PyizYYR10UiDzR1upZBcFkJZBg== X-Received: by 2002:a62:1786:0:b0:445:1a9c:952a with SMTP id 128-20020a621786000000b004451a9c952amr11602274pfx.39.1634300405175; Fri, 15 Oct 2021 05:20:05 -0700 (PDT) From: Kaihang Zhang To: musl@lists.openwall.com, 2010267516@qq.com Cc: Kaihang Zhang Date: Fri, 15 Oct 2021 08:20:00 -0400 Message-Id: <20211015122000.2490-1-kaihang.zhang@smartx.com> X-Mailer: git-send-email 2.25.4 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [musl] [PATCH v2] fix: Truncate the too-long mntent in function getmntent_r In function getmntent_r in source misc/mntent.c, entry that is too long will be truncated rather than discarded. The caller can tell by errno whether the supplied buffer is too small, and retry from the beginning of the file. --- src/misc/mntent.c | 53 +++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/src/misc/mntent.c b/src/misc/mntent.c index eabb8200..085ce45d 100644 --- a/src/misc/mntent.c +++ b/src/misc/mntent.c @@ -21,12 +21,12 @@ int endmntent(FILE *f) struct mntent *getmntent_r(FILE *f, struct mntent *mnt, char *linebuf, int buflen) { - int cnt, n[8], use_internal = (linebuf == SENTINEL); - - mnt->mnt_freq = 0; - mnt->mnt_passno = 0; + int use_internal = (linebuf == SENTINEL); + char *sub; do { + char *end_ptr; + if (use_internal) { getline(&internal_buf, &internal_bufsize, f); linebuf = internal_buf; @@ -34,25 +34,38 @@ struct mntent *getmntent_r(FILE *f, struct mntent *mnt, char *linebuf, int bufle fgets(linebuf, buflen, f); } if (feof(f) || ferror(f)) return 0; - if (!strchr(linebuf, '\n')) { + + end_ptr = strchr(linebuf, '\n'); + if (end_ptr != NULL) { + while ((end_ptr[-1] == ' ' || end_ptr[-1] == '\t') && end_ptr != linebuf) end_ptr--; + *end_ptr = '\0'; + } else { fscanf(f, "%*[^\n]%*[\n]"); errno = ERANGE; - return 0; } - cnt = sscanf(linebuf, " %n%*s%n %n%*s%n %n%*s%n %n%*s%n %d %d", - n, n+1, n+2, n+3, n+4, n+5, n+6, n+7, - &mnt->mnt_freq, &mnt->mnt_passno); - } while (cnt < 2 || linebuf[n[0]] == '#'); - - linebuf[n[1]] = 0; - linebuf[n[3]] = 0; - linebuf[n[5]] = 0; - linebuf[n[7]] = 0; - - mnt->mnt_fsname = linebuf+n[0]; - mnt->mnt_dir = linebuf+n[2]; - mnt->mnt_type = linebuf+n[4]; - mnt->mnt_opts = linebuf+n[6]; + + linebuf += strspn(linebuf, " \t"); + } while (linebuf[0] == '\0' || linebuf[0] == '#'); + + mnt->mnt_fsname = strsep(&linebuf, " \t"); + + if (linebuf) linebuf += strspn(linebuf, " \t"); + sub = strsep(&linebuf, " \t"); + mnt->mnt_dir = sub ? sub : (char *) ""; + + if (linebuf) linebuf += strspn(linebuf, " \t"); + sub = strsep (&linebuf, " \t"); + mnt->mnt_type = sub ? sub : (char *) ""; + + if (linebuf) linebuf += strspn(linebuf, " \t"); + sub = strsep(&linebuf, " \t"); + mnt->mnt_opts = sub ? sub : (char *) ""; + + switch (linebuf ? sscanf(linebuf, " %d %d", &mnt->mnt_freq, &mnt->mnt_passno) : 0) { + case 0: mnt->mnt_freq = 0; + case 1: mnt->mnt_passno = 0; + case 2: break; + } return mnt; } -- 2.25.4