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 11519 invoked from network); 27 Sep 2021 13:44:12 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 27 Sep 2021 13:44:12 -0000 Received: (qmail 9512 invoked by uid 550); 27 Sep 2021 13:44:08 -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 7685 invoked from network); 27 Sep 2021 08:26:23 -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=L7EOsJtOFENJTW2coDLWJF6qxd0rssjBLZOg54FLHKg=; b=eRkLiz/MUNkiQVukc8MM8Ns3jOIGBLw3nEQ5AcHa8LUwStPwW+IN3lwi3G8jpb12bm 54IyJiMafaDa4BvLsw8HOUtxJdi9YDCx55x1zb4OSANANeNo1NEYSFczU8Ew8a+xsWfe 65OntamX9TtuCVg9rONchipvknQRbRJgvi52dNAdUDxjhifAaHyPo7fZ5MD4nZJXqPcw OxBLvnnGHjeOIJCvhEnFfE+W7Z0RGMNPKvVI34MWJ3Ko4Hzy8wtRK0aZ0mpxR6n1IGQn 0ip5jw5FIYErDCQMyKQEk1KAKqg6UFWyauaB3cybwbc8DL9cGMgoXBMZMzMD745RJYWK 6a0w== 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=L7EOsJtOFENJTW2coDLWJF6qxd0rssjBLZOg54FLHKg=; b=fWCYOsjOzYgnGyAfEV4thu80pgoO4dExsnt5iFQjXTeOqfrUHjY5xy7PBsClTkrUMG 03sdPKrA4QDWw0QSfAee0ngJffW4mhfz/BkVrNsq2mFA5iySDHKEP5bHbmI+YFd4l2OG zA1XlWxeOXpxiTuU7jK6mymuYz+BFbf2pOhSNd6ipALwpmnHVu6+yUt8N1TlqQwyy5AI ZLtOyBo2vXijBOuh/fTZZdBciF30cuFtiouLS9Gkbcv7xOrzuTFzCDWSdgF+AS1NdgLx P3/oamrektMzLKYxFTF9OITgkkGCpVVotJbNzd/N+6fwT1zQRivpOtMJJ/XoZjbwlR2U gZzA== X-Gm-Message-State: AOAM533AOubtonizs5y38dNGw6cV0kmEfsdzADD10GZwEPeO6xFdAiit 0SuF0N9lhRR/FZnWKctBpaUabw== X-Google-Smtp-Source: ABdhPJw7n1cCbrLEOR1LbjOizE+Jt+H9G4ggGqsYwL8UdtFi22P7Ze+pBP0BFpxFMcBMBmPhE6wZyQ== X-Received: by 2002:a05:6a00:d4b:b0:444:9854:2ee6 with SMTP id n11-20020a056a000d4b00b0044498542ee6mr21862181pfv.13.1632731171480; Mon, 27 Sep 2021 01:26:11 -0700 (PDT) From: Kaihang Zhang To: 2010267516@qq.com, musl@lists.openwall.com Cc: Kaihang Zhang Date: Mon, 27 Sep 2021 04:26:03 -0400 Message-Id: <20210927082603.32757-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: Assign default value to mntent when linebuf is too small Function getmntent_r in source misc/mntent.c will do what glibc users expect. The rest of the line will be discarded when can not be read into linebuf, and the fields of struct mntent will be assigned to empty string or zero when can not be found in linebuf, instead of setting errno to ERANGE and exiting. --- src/misc/mntent.c | 54 +++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/src/misc/mntent.c b/src/misc/mntent.c index eabb8200..007c0b8d 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,37 @@ 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