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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



* Michael Kerrisk <mtk.manpages@xxxxxxxxx>, 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
#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));
}

[Index of Archives]     [Kernel Documentation]     [Netdev]     [Linux Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux