From: "Michael Kerrisk (man-pages)" <mtk.manpages@gmail.com>
To: Alejandro Colomar <colomar.6.4.3@gmail.com>
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
Date: Mon, 21 Sep 2020 12:38:58 +0200 [thread overview]
Message-ID: <e48de555-d07c-3ecc-c0eb-1184d89035f3@gmail.com> (raw)
In-Reply-To: <20200921081933.24196-1-colomar.6.4.3@gmail.com>
Hello Alex,
On 9/21/20 10:19 AM, Alejandro Colomar wrote:
> Reported-by: Michael Kerrisk <mtk.manpages@gmail.com>
> Signed-off-by: Alejandro Colomar <colomar.6.4.3@gmail.com>
> ---
>
> 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 <stdint.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <sys/types.h>
> +
> +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/
next prev parent reply other threads:[~2020-09-21 10:39 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-09-20 21:40 [PATCH] system_data_types.7: Add note about length modifiers and conversions to [u]intmax_t, and corresponding example Alejandro Colomar
2020-09-21 5:39 ` Michael Kerrisk (man-pages)
2020-09-21 7:32 ` man-pages.7: Simplify indentation of structure definitions, shell session logs, and so on Alejandro Colomar
2020-09-21 7:49 ` Michael Kerrisk (man-pages)
2020-09-21 14:15 ` G. Branden Robinson
2020-09-24 8:15 ` Michael Kerrisk (man-pages)
2020-09-27 6:03 ` G. Branden Robinson
2020-09-29 13:12 ` Michael Kerrisk (man-pages)
2020-09-29 20:15 ` Michael Kerrisk (man-pages)
2020-09-30 12:02 ` G. Branden Robinson
2020-09-30 12:54 ` G. Branden Robinson
2020-10-01 7:33 ` Michael Kerrisk (man-pages)
2020-10-26 7:00 ` Michael Kerrisk (man-pages)
2020-09-21 8:19 ` [PATCH v2] system_data_types.7: Add note about length modifiers and conversions to [u]intmax_t, and corresponding example Alejandro Colomar
2020-09-21 8:29 ` Alejandro Colomar
2020-09-21 10:38 ` Michael Kerrisk (man-pages) [this message]
2020-09-21 13:32 ` [PATCH v3] " Alejandro Colomar
2020-09-21 14:13 ` Michael Kerrisk (man-pages)
2020-09-21 14:39 ` Alejandro Colomar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=e48de555-d07c-3ecc-c0eb-1184d89035f3@gmail.com \
--to=mtk.manpages@gmail.com \
--cc=colomar.6.4.3@gmail.com \
--cc=eggert@cs.ucla.edu \
--cc=fweimer@redhat.com \
--cc=libc-alpha@sourceware.org \
--cc=linux-man@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.