All of lore.kernel.org
 help / color / mirror / Atom feed
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,
	Jakub Wilk <jwilk@jwilk.net>
Subject: Re: [PATCH] cmsg.3, getaddrinfo_a.3 getgrouplist.3: Use sizeof, consistently
Date: Wed, 26 Aug 2020 08:21:12 +0200	[thread overview]
Message-ID: <2b288808-c840-343f-9e56-8097765e5528@gmail.com> (raw)
In-Reply-To: <9fcc512e-a12c-5b28-126f-aaf1854ed290@gmail.com>

Hello Alex,

On 8/25/20 3:05 PM, Alejandro Colomar wrote:
> Hi Michael,
> 
> On 8/25/20 2:35 PM, Michael Kerrisk (man-pages) wrote:
>> Patch applied, but I had to fix up the title line, which got taken
>> from the mail subject line. Probably best to send subsequent patches
>> as new mails, rather than as replies to this thread.
> 
> This time I replied to the thread (so that the conversation can be
> followed in the archive), but changed the subject.  I hope that works.
> 
> (I sent this email only to you by accident; I'm sending it again with
> the CCs).
> 
> Patch (3) as numbered in the previous replies:
> 
> --------------------------------------------------------------------
>>From 1cb973629d94a048c5dcbe13fef76173f99dc3de Mon Sep 17 00:00:00 2001
> From: Alejandro Colomar <colomar.6.4.3@gmail.com>
> Date: Tue, 25 Aug 2020 14:52:03 +0200
> Subject: [PATCH] cmsg.3, getaddrinfo_a.3 getgrouplist.3: Use sizeof
>  consistently
> 
> Use ``sizeof`` consistently through all the examples in the following
> way:
> 
> - When the result of ``sizeof`` is multiplied (or otherwise modified),
>   write ``sizeof`` in the first place.
> 
> Rationale:
> 
> ``(sizeof(x) * INT_MAX * 2)`` doesn't overflow.
> 
> ``(INT_MAX * 2 * sizeof(x))`` overflows, giving incorrect
> results.
> 
> As a side effect, the parentheses of ``sizeof`` are not next to
> the parentheses of the whole expression, and it is visually
> easier to read.
> 
> Detailed rationale:
> 
> In C, successive multiplications are evaluated left to right (*), and
> therefore here is what happens (assuming x86_64):
> 
> ``(sizeof(x) * INT_MAX * 2)``:
> 
> 1) sizeof(x) * INT_MAX	(the type is the largest of both, which is
> 			 size_t (unsigned long; uint64_t)).
> 2) ANS * 2		(the type is again the largest: size_t)
> 
> ``(INT_MAX * 2 * sizeof(x))``:
> 
> 1) INT_MAX * 2		(the type is the largest of both, which is
> 			 int as both are int (int; int32_t), so the
> 			 result is already truncated as it doesn't fit
> 			 an int; at this point, the intermediate result
> 			 will be 2^32 - 2 (``INT_MAX - 1``) (if I did
> 			 the math right)).
> 2) ANS * 2		(the type is again the largest of both: size_t;
> 			 however, ANS was already incorrect, so the
> 			 result will be an incorrect size_t value)
> 
> (*):	https://en.cppreference.com/w/c/language/operator_precedence
> 
> Signed-off-by: Alejandro Colomar <colomar.6.4.3@gmail.com>

Thanks! Patch applied.

Cheers,

Michael

> ---
>  man3/cmsg.3          | 2 +-
>  man3/getaddrinfo_a.3 | 2 +-
>  man3/getgrouplist.3  | 2 +-
>  3 files changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/man3/cmsg.3 b/man3/cmsg.3
> index 2f9910093..99ee950f9 100644
> --- a/man3/cmsg.3
> +++ b/man3/cmsg.3
> @@ -242,7 +242,7 @@ cmsg = CMSG_FIRSTHDR(&msg);
>  cmsg\->cmsg_level = SOL_SOCKET;
>  cmsg\->cmsg_type = SCM_RIGHTS;
>  cmsg\->cmsg_len = CMSG_LEN(sizeof(int) * NUM_FD);
> -memcpy(CMSG_DATA(cmsg), myfds, NUM_FD * sizeof(int));
> +memcpy(CMSG_DATA(cmsg), myfds, sizeof(int) * NUM_FD);
>  .EE
>  .in
>  .SH SEE ALSO
> diff --git a/man3/getaddrinfo_a.3 b/man3/getaddrinfo_a.3
> index af8f88937..cd4cad0dc 100644
> --- a/man3/getaddrinfo_a.3
> +++ b/man3/getaddrinfo_a.3
> @@ -473,7 +473,7 @@ add_requests(void)
> 
>      while ((host = strtok(NULL, " "))) {
>          nreqs++;
> -        reqs = realloc(reqs, nreqs * sizeof(reqs[0]));
> +        reqs = realloc(reqs, sizeof(reqs[0]) * nreqs);
> 
>          reqs[nreqs \- 1] = calloc(1, sizeof(*reqs[0]));
>          reqs[nreqs \- 1]\->ar_name = strdup(host);
> diff --git a/man3/getgrouplist.3 b/man3/getgrouplist.3
> index aea52d999..372f2613f 100644
> --- a/man3/getgrouplist.3
> +++ b/man3/getgrouplist.3
> @@ -164,7 +164,7 @@ main(int argc, char *argv[])
> 
>      ngroups = atoi(argv[2]);
> 
> -    groups = malloc(ngroups * sizeof(gid_t));
> +    groups = malloc(sizeof(gid_t) * ngroups);
>      if (groups == NULL) {
>          perror("malloc");
>          exit(EXIT_FAILURE);
> 


-- 
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/

  reply	other threads:[~2020-08-26  6:21 UTC|newest]

Thread overview: 92+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-24 13:29 [patch] memusage.1, bind.2, eventfd.2, futex.2, open_by_handle_at.2, perf_event_open.2, poll.2, signalfd.2, sysctl.2, timerfd_create.2, bsearch.3, cmsg.3, getaddrinfo.3, getaddrinfo_a.3 getgrouplist.3, insque.3, malloc_info.3, mbsinit.3, mbstowcs.3, pthread_create.3, pthread_setaffinity_np.3, queue.3, rtnetlink.3, shm_open.3, strptime.3, tsearch.3, aio.7, fanotify.7, inotify.7, unix.7: Use sizeof consistently Alejandro Colomar
2020-08-25 10:29 ` Michael Kerrisk (man-pages)
2020-08-25 11:19   ` Jakub Wilk
2020-08-25 11:34     ` Alejandro Colomar
2020-08-25 11:41       ` Michael Kerrisk (man-pages)
2020-08-25 11:48         ` Alejandro Colomar
2020-08-25 12:21           ` Alejandro Colomar
2020-08-25 12:35             ` Michael Kerrisk (man-pages)
2020-08-25 13:05               ` [PATCH] cmsg.3, getaddrinfo_a.3 getgrouplist.3: Use sizeof, consistently Alejandro Colomar
2020-08-26  6:21                 ` Michael Kerrisk (man-pages) [this message]
2020-09-03 10:23                   ` [PATCH] memusage.1: Use sizeof consistently Alejandro Colomar
2020-09-04  8:20                     ` Michael Kerrisk (man-pages)
2020-09-04 10:19                       ` [PATCH (2) 02/34] bind.2: " Alejandro Colomar
2020-09-04 10:21                         ` [PATCH (2) 03/34] eventfd.2: " Alejandro Colomar
2020-09-04 10:46                           ` Michael Kerrisk (man-pages)
2020-09-04 10:54                           ` [PATCH (2) 04/34] futex.2: " Alejandro Colomar
2020-09-04 10:55                             ` [PATCH (2) 05/34] open_by_handle_at.2: " Alejandro Colomar
2020-09-04 10:56                               ` [PATCH (2) 06/34] perf_event_open.2: " Alejandro Colomar
2020-09-04 10:57                                 ` [PATCH (2) 07/34] " Alejandro Colomar
2020-09-04 12:25                                   ` Michael Kerrisk (man-pages)
2020-09-04 13:42                                   ` [PATCH (2) 08/34] poll.2: " Alejandro Colomar
2020-09-04 13:43                                     ` [PATCH (2) 09/34] sysctl.2: " Alejandro Colomar
2020-09-04 13:44                                       ` [PATCH (2) 10/34] signalfd.2: " Alejandro Colomar
2020-09-04 13:45                                         ` [PATCH (2) 11/34] timerfd_create.2: " Alejandro Colomar
2020-09-04 13:46                                           ` [PATCH (2) 12/34] bsearch.3: " Alejandro Colomar
2020-09-04 13:50                                             ` [PATCH (2) 13/34] cmsg.3: " Alejandro Colomar
2020-09-04 13:52                                               ` [PATCH (2) 14/34] " Alejandro Colomar
2020-09-04 13:53                                                 ` [PATCH (2) 15/34] getaddrinfo.3: " Alejandro Colomar
2020-09-04 14:32                                                   ` [PATCH (2) 16/34] " Alejandro Colomar
2020-09-04 14:34                                                     ` [PATCH (2) 17/34] getgrouplist.3: " Alejandro Colomar
2020-09-04 14:37                                                       ` [PATCH (2) 18/34] insque.3: " Alejandro Colomar
2020-09-04 14:41                                                         ` [PATCH (2) 19/34] malloc_info.3: " Alejandro Colomar
2020-09-04 14:44                                                           ` [PATCH (2) 20/34] mbsinit.3: " Alejandro Colomar
2020-09-04 14:45                                                             ` [PATCH (2) 21/34] mbstowcs.3: " Alejandro Colomar
2020-09-04 14:48                                                               ` [PATCH (2) 22/34] pthread_create.3: " Alejandro Colomar
2020-09-04 14:50                                                                 ` [PATCH (2) 23/34] pthread_setaffinity_np.3: " Alejandro Colomar
2020-09-04 14:50                                                                   ` [PATCH (2) 24/34] queue.3: " Alejandro Colomar
2020-09-04 14:52                                                                     ` [PATCH (2) 25/34] rtnetlink.3: " Alejandro Colomar
2020-09-04 14:54                                                                       ` [PATCH (2) 26/34] " Alejandro Colomar
2020-09-04 14:55                                                                         ` [PATCH (2) 27/34] shm_open.3: " Alejandro Colomar
2020-09-04 14:57                                                                           ` [PATCH (2) 28/34] strptime.3: " Alejandro Colomar
2020-09-04 15:37                                                                             ` Michael Kerrisk (man-pages)
2020-09-04 15:03                                                                           ` [PATCH (2) 29/34] tsearch.3: " Alejandro Colomar
2020-09-05 14:22                                                                             ` Michael Kerrisk (man-pages)
2020-09-04 15:04                                                                           ` [PATCH (2) 30/34] aio.7: " Alejandro Colomar
2020-09-04 17:15                                                                             ` Michael Kerrisk (man-pages)
2020-09-04 15:05                                                                           ` [PATCH (2) 31/34] fanotify.7: " Alejandro Colomar
2020-09-04 15:38                                                                             ` Michael Kerrisk (man-pages)
2020-09-04 15:06                                                                           ` [PATCH (2) 32/34] inotify.7: " Alejandro Colomar
2020-09-04 17:08                                                                             ` Michael Kerrisk (man-pages)
2020-09-04 15:07                                                                           ` [PATCH (2) 33/34] " Alejandro Colomar
2020-09-04 17:14                                                                             ` Michael Kerrisk (man-pages)
2020-09-04 15:08                                                                           ` [PATCH (2) 34/34] unix.7: " Alejandro Colomar
2020-09-04 15:12                                                                             ` Alejandro Colomar
2020-09-05  8:27                                                                               ` Michael Kerrisk (man-pages)
2020-09-05  9:37                                                                                 ` Alejandro Colomar
2020-09-05 14:38                                                                                   ` Michael Kerrisk (man-pages)
2020-09-05 14:52                                                                                     ` Alejandro Colomar
2020-09-05 15:24                                                                                       ` Michael Kerrisk (man-pages)
2020-09-04 15:40                                                                             ` Michael Kerrisk (man-pages)
2020-09-04 17:26                                                                           ` [PATCH (2) 27/34] shm_open.3: " Michael Kerrisk (man-pages)
2020-09-04 17:04                                                                         ` [PATCH (2) 26/34] rtnetlink.3: " Michael Kerrisk (man-pages)
2020-09-04 16:59                                                                       ` [PATCH (2) 25/34] " Michael Kerrisk (man-pages)
2020-09-04 16:58                                                                     ` [PATCH (2) 24/34] queue.3: " Michael Kerrisk (man-pages)
2020-09-04 15:42                                                                   ` [PATCH (2) 23/34] pthread_setaffinity_np.3: " Michael Kerrisk (man-pages)
2020-09-05 14:21                                                                 ` [PATCH (2) 22/34] pthread_create.3: " Michael Kerrisk (man-pages)
2020-09-05 14:21                                                               ` [PATCH (2) 21/34] mbstowcs.3: " Michael Kerrisk (man-pages)
2020-09-04 15:27                                                             ` [PATCH (2) 20/34] mbsinit.3: " Michael Kerrisk (man-pages)
2020-09-04 15:27                                                           ` [PATCH (2) 19/34] malloc_info.3: " Michael Kerrisk (man-pages)
2020-09-04 15:25                                                         ` [PATCH (2) 18/34] insque.3: " Michael Kerrisk (man-pages)
2020-09-04 15:25                                                       ` [PATCH (2) 17/34] getgrouplist.3: " Michael Kerrisk (man-pages)
2020-09-04 15:21                                                     ` [PATCH (2) 16/34] getaddrinfo.3: " Michael Kerrisk (man-pages)
2020-09-04 15:20                                                   ` [PATCH (2) 15/34] " Michael Kerrisk (man-pages)
2020-09-04 13:54                                                 ` Alejandro Colomar
2020-09-04 15:20                                                 ` [PATCH (2) 14/34] cmsg.3: " Michael Kerrisk (man-pages)
2020-09-04 15:18                                               ` [PATCH (2) 13/34] " Michael Kerrisk (man-pages)
2020-09-04 15:17                                             ` [PATCH (2) 12/34] bsearch.3: " Michael Kerrisk (man-pages)
2020-09-04 15:14                                           ` [PATCH (2) 11/34] timerfd_create.2: " Michael Kerrisk (man-pages)
2020-09-05  8:07                                             ` Michael Kerrisk (man-pages)
2020-09-04 15:13                                         ` [PATCH (2) 10/34] signalfd.2: " Michael Kerrisk (man-pages)
2020-09-04 15:11                                       ` [PATCH (2) 09/34] sysctl.2: " Michael Kerrisk (man-pages)
2020-09-04 15:11                                     ` [PATCH (2) 08/34] poll.2: " Michael Kerrisk (man-pages)
2020-09-04 12:24                                 ` [PATCH (2) 06/34] perf_event_open.2: " Michael Kerrisk (man-pages)
2020-09-04 12:20                               ` [PATCH (2) 05/34] open_by_handle_at.2: " Michael Kerrisk (man-pages)
2020-09-04 12:19                             ` [PATCH (2) 04/34] futex.2: " Michael Kerrisk (man-pages)
2020-09-04 10:44                         ` [PATCH (2) 02/34] bind.2: " Michael Kerrisk (man-pages)
2020-08-25 11:35     ` [patch] memusage.1, bind.2, eventfd.2, futex.2, open_by_handle_at.2, perf_event_open.2, poll.2, signalfd.2, sysctl.2, timerfd_create.2, bsearch.3, cmsg.3, getaddrinfo.3, getaddrinfo_a.3 getgrouplist.3, insque.3, malloc_info.3, mbsinit.3, mbstowcs.3, pthread_create.3, pthread_setaffinity_np.3, queue.3, rtnetlink.3, shm_open.3, strptime.3, tsearch.3, aio.7, fanotify.7, inotify.7, unix.7: " Michael Kerrisk (man-pages)
2020-09-05 14:20 ` [PATCH 35/35] qsort.3: " Alejandro Colomar
2020-09-05 14:23   ` Alejandro Colomar
2020-09-05 15:27   ` Michael Kerrisk (man-pages)
2020-09-05 15:32     ` Alejandro Colomar
2020-09-05 15:36       ` Michael Kerrisk (man-pages)

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=2b288808-c840-343f-9e56-8097765e5528@gmail.com \
    --to=mtk.manpages@gmail.com \
    --cc=colomar.6.4.3@gmail.com \
    --cc=jwilk@jwilk.net \
    --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.