From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by sourceware.org (Postfix) with ESMTPS id F0A023858D28 for ; Tue, 18 Jan 2022 14:55:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org F0A023858D28 Received: by mail-pl1-x62d.google.com with SMTP id d1so1297826plh.10 for ; Tue, 18 Jan 2022 06:55:19 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=Nly53JiQmZStkX7rYPDWJWmVngH70ciVcRVtKRZFIb0=; b=4r4qJSIgncPe1ao2ENsT1gsfkat7Ng7Pdw0oAwnfWgjgKe8C0aVz3kEKDcT7NrAG5y 69+FGGoOHuQvfz1KsVc5Nd+w4SaASQSwCmWaKQefONvenwEzLSy5R6nKJz6ZWjQLQV2Y f8Z7rfM0bXfosvfTVxwfL54JJ9wxarcdXgrgzpERjNpV6hF5y40/6725PUZzinnvQTqR prWNMa31rIpwW8yDRNYjFefwWv5g/VNDKi26vqyOtAPOhYL3KgDJI5FSIqRvfDzzu6fA +iO1fg4CTgjVGR2TVieT5Gkh3ZpucojZAt7pYoTMxogNm5kFy4wZ+XkLkiKynmDlyv3w HHtw== X-Gm-Message-State: AOAM532w0LCWh5CEz46LVHbPnouN7GaJ+Wtub4B+kT7b5LGMhQ5mNySr MGe4LMW8fWi8GLOu/xu31SGfdTvZQwKLWnligLgNECcmIRk= X-Google-Smtp-Source: ABdhPJzY16LyK+KtBeWECNPP5pEbDx5ClSVE8N7r7twRhVzGLkdbvrrQIyAI06GD7IUobYCttGO4Gdb/i7QrRfpfuMU= X-Received: by 2002:a17:902:e544:b0:14a:555c:add3 with SMTP id n4-20020a170902e54400b0014a555cadd3mr27217282plf.102.1642517719062; Tue, 18 Jan 2022 06:55:19 -0800 (PST) MIME-Version: 1.0 References: <1642405014-3287-1-git-send-email-xuyang2018.jy@fujitsu.com> <61E6298D.80006@fujitsu.com> <61E64FED.2010906@fujitsu.com> <87fspl6s44.fsf@igel.home> <1f209861-9ab7-340a-f4a9-f28423a39a4c@opteya.com> In-Reply-To: <1f209861-9ab7-340a-f4a9-f28423a39a4c@opteya.com> From: "H.J. Lu" Date: Tue, 18 Jan 2022 06:54:43 -0800 Message-ID: Subject: Re: [PATCH] src/ext4_resize.c: set errno to 0 before the strtoull call To: Yann Droneaud Cc: GNU C Library Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-3021.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 18 Jan 2022 14:55:21 -0000 On Tue, Jan 18, 2022 at 6:43 AM Yann Droneaud wrote: > > Hi, > > Le 18/01/2022 =C3=A0 15:29, H.J. Lu via Libc-alpha a =C3=A9crit : > > On Tue, Jan 18, 2022 at 6:22 AM Andreas Schwab = wrote: > >> On Jan 18 2022, xuyang2018.jy@fujitsu.com wrote: > >> > >>> Now, I use glibc-2.34 and run the following program[1] but the errno = is > >>> not 0 in the beginning. So is this a known bug on glibc-2.34(Theodore > >>> doesn't meet this problem on glicb-2.31)? > >>> > >>> [1]https://git.kernel.org/pub/scm/fs/xfs/xfstests-dev.git/tree/src/ex= t4_resize.c > >> Note that there is a call to open preceding the strtoull call, so no > >> guarantees can be made about the value of errno before the latter. > >> > > strtoull man page: > > > > RETURN VALUE > > The strtoul() function returns either the result of the convers= ion or, > > if there was a leading minus sign, the negation of the resul= t of the > > conversion represented as an unsigned value, unless the origina= l (non=E2=80=90 > > negated) value would overflow; in the latter case, strtoul()= returns > > ULONG_MAX and sets errno to ERANGE. Precisely the same holds f= or str=E2=80=90 > > toull() (with ULLONG_MAX instead of ULONG_MAX). > > > > new_size =3D strtoull(argv[2], &tmp, 10); > > if ((errno) || (*tmp !=3D '\0')) { > > ^^^^^^^^^^^^ Shouldn't errno be checked only after the prior function > > return value is checked first? > > > > fprintf(stderr, "%s: invalid new size\n", argv[0]); > > return 1; > > } > > Something like: > > errno =3D 0; /* must be cleared to distinguish error */ ^^^^^^^^^^^^^^^^^ Remove it. > tmp =3D NULL; > new_size =3D strtoull(argv[2], &p, 0); > if ((new_size =3D=3D 0 || new_size =3D=3D ULONGLONG_MAX) && errn= o) { Change it to if (new_size =3D=3D 0 || (new_size =3D=3D ULONGLONG_MAX) && errno)) { > fprintf(stderr, "%s: invalid new size\n", argv[0]); > return 1; > } > > if (tmp) { > /* error, unused trailing characters */ > if (*tmp) { > fprintf(stderr, "%s: invalid new size\n", argv[0]= ); > return 1; > } > > /* error, empty string */ > if (tmp =3D=3D argv[2]) { > fprintf(stderr, "%s: invalid new size\n", argv[0]= ); > return 1; > > } > } > > -- > Yann Droneaud > OPTEYA > --=20 H.J.