From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x442.google.com (mail-wr1-x442.google.com [IPv6:2a00:1450:4864:20::442]) by sourceware.org (Postfix) with ESMTPS id B81D3385043A for ; Mon, 21 Sep 2020 10:39:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org B81D3385043A Received: by mail-wr1-x442.google.com with SMTP id m6so12205807wrn.0 for ; Mon, 21 Sep 2020 03:39:00 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:cc:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=gK2fJu35Z2qX01cKlnEu2+b1MFtCF2XdGa3f9j9hoi4=; b=SWs8nlUVm3i1CFGoQuqHvnnHr0Q2ufNryGMGLbrrW0IilZC+9Je9rOIi42ThNpPj1g rA86qxpimCMI6MdOq500eT7vjWQImXgCm4qX5nPnxdN8XvMkgMGv6gv9IuOHQ2PBG4Tz MpeU1FO08wFTTZapzMZ/RhW/BCI8+NKCPsijGnit88oN9EjIwNlBRiINelo400SfDExn yzz/gUA4A5fgWhZhcSAYAJamt2Cz+SopUK1cBKjnUWr44AtMOoCxa3iu9gTpq9oAejvX uxZsgM5PqeLY9JV65UZiS0Xvx4bNeJp2dVEmg1hlX5DrQEObEACS1zFxV81HjNsP4eMX ZChA== X-Gm-Message-State: AOAM532GjqFE+9vrU/a+frMfXZsI3jcA/iDndN013pEfROZuh+GzD6Y+ u4GrfiV1B4tVyQjy3Rcem64= X-Google-Smtp-Source: ABdhPJyTH9/OqAueny5esS1g4op2dYvDJSv7ApFePGxqn+ocZ10KPk1oXBwhHWSMSxndRai35kqFjA== X-Received: by 2002:a05:6000:124d:: with SMTP id j13mr54848372wrx.182.1600684739770; Mon, 21 Sep 2020 03:38:59 -0700 (PDT) Received: from ?IPv6:2001:a61:2479:6801:d8fe:4132:9f23:7e8f? ([2001:a61:2479:6801:d8fe:4132:9f23:7e8f]) by smtp.gmail.com with ESMTPSA id p1sm35722803wma.0.2020.09.21.03.38.58 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 21 Sep 2020 03:38:59 -0700 (PDT) Cc: mtk.manpages@gmail.com, linux-man@vger.kernel.org, libc-alpha@sourceware.org, eggert@cs.ucla.edu, fweimer@redhat.com Subject: Re: [PATCH v2] system_data_types.7: Add note about length modifiers and conversions to [u]intmax_t, and corresponding example To: Alejandro Colomar References: <61f4e2a4-d468-ceba-2ccf-ce0c061aa20b@gmail.com> <20200921081933.24196-1-colomar.6.4.3@gmail.com> From: "Michael Kerrisk (man-pages)" Message-ID: Date: Mon, 21 Sep 2020 12:38:58 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <20200921081933.24196-1-colomar.6.4.3@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-10.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: Mon, 21 Sep 2020 10:39:02 -0000 Hello Alex, On 9/21/20 10:19 AM, Alejandro Colomar wrote: > Reported-by: Michael Kerrisk > Signed-off-by: Alejandro Colomar > --- > > Hi Michael, > > I added the part about range checking, and used a type with defined > limits to show a complete example. Thanks! Still a few coments. > Thanks, > > Alex > > > man7/system_data_types.7 | 62 ++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 62 insertions(+) > > diff --git a/man7/system_data_types.7 b/man7/system_data_types.7 > index dd1d01aab..ba1338179 100644 > --- a/man7/system_data_types.7 > +++ b/man7/system_data_types.7 > @@ -629,6 +629,68 @@ See also: > .SH NOTES > The structures described in this manual page shall contain, > at least, the members shown in their definition, in no particular order. > +.PP > +Most of the integer types described in this page don't have > +a corresponding length modifier for the > +.BR printf (3) > +and the > +.BR scanf (3) > +families of functions. > +To print a value of an integer type that doesn't have a length modifier, > +it should be converted to > +.I intmax_t > +or > +.I uintmax_t > +by an explicit cast. > +To scan into a variable of a type that doesn't have a length modifier, s/a type/an integer type/ > +an intermediate temporary variable of type > +.I intmax_t > +or > +.I uintmax_t > +should be used. > +When copying from the temporary variable to the actual variable, s/actual/destination/ > +the value could overflow. > +If POSIX provides lower and upper limits to the type, > +the user should check that the value is within those limits, > +before actually copying the value. > +The example below shows how these conversions should be done. > +.SH EXAMPLES > +The program shown below scans from a string and prints a value stored in > +a variable of an integer type that doesn't have a length modifier. > +The appropriate conversions from and to > +.IR intmax_t , > +and the appropriate range checkings, > +are used as explained in the notes section above: > +.PP > +.EX > +#include > +#include > +#include > +#include > + > +int > +main (void) > +{ > + static const char *const str = "500000 us in half a second"; > + suseconds_t us; > + intmax_t tmp; > + > + /* Scan the number from the string into the temporary variable */ > + sscanf(str, "%jd", &tmp); > + > + /* Check that the value is within the valid range */ > + if (tmp < -1 || tmp > 1000000) I think the first part of the check here should be 'tmp < 0'. (Yes, the defined range for the type must allow -1, but speaking of -1 microseconds is nonsensical, right? > + exit(EXIT_FAILURE); > + > + /* Copy the value to the suseconds_t variable 'us' */ > + us = tmp; > + > + /* Print the value */ > + printf("There are %jd us in half a second.\en", (intmax_t) us); > + > + exit(EXIT_SUCCESS); > +} > +.EE > .SH SEE ALSO > .BR feature_test_macros (7), > .BR standards (7) Thanks, Michael -- Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/