Re: [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

Jakub Wilk
2020-08-25T11:19:31Z

[-- Attachment #1: Type: text/plain, Size: 799 bytes --]

* Michael Kerrisk <mtk.manpages@gmail.com>, 2020-08-25, 12:29:
>> ``(sizeof(x) * INT_MAX * 2)`` doesn't overflow.
>>
>> ``(INT_MAX * 2 * sizeof(x))`` overflows, giving incorrect
>> results.
>
>(3) Is this true? "gcc -Wall" does not complain about this.

My GCC (10.2.0) does, even without -Wall:

$ gcc test-overflow.c
test-overflow.c: In function 'main':
test-overflow.c:8:52: warning: integer overflow in expression of type 'int' results in '-2' [-Woverflow]
8 | printf("INT_MAX * 2 * sizeof(x) = %zu\n", INT_MAX * 2 * sizeof(x));
| ^

>sizeof((sizeof(x) * INT_MAX * 2)) == 8
>sizeof(INT_MAX * 2 * sizeof(x)) == 8

Hmm? If there was no overflow, surely you should get a number larger
than INT_MAX...

--
Jakub Wilk

[-- Attachment #2: test-overflow.c --]
[-- Type: text/x-csrc, Size: 229 bytes --]

#include <stdio.h>
#include <limits.h>

int main(int arg, char **argv)
{
typedef int x;
printf("sizeof(x) * INT_MAX * 2 = %zu\n", sizeof(x) * INT_MAX * 2);
printf("INT_MAX * 2 * sizeof(x) = %zu\n", INT_MAX * 2 * sizeof(x));
}