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
PATCH 1/1 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
From 5df5cae0fb6973df0ab8b3629934f808487112b0 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Fri, 21 Aug 2020 16:58:12 +0200
Subject: [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
Use ``sizeof`` consistently through all the examples, in the following way:
- Never use a space after ``sizeof``, and always use parentheses instead.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#spaces
- Use the name of the variable instead of the type as argument for ``sizeof``, wherever possible.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
- 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.
--- man1/memusage.1 | 12 ++++++------ man2/bind.2 | 6 +++--- man2/eventfd.2 | 8 ++++---- man2/futex.2 | 2 +- man2/open_by_handle_at.2 | 4 ++-- man2/perf_event_open.2 | 6 +++--- man2/poll.2 | 2 +- man2/signalfd.2 | 4 ++-- man2/sysctl.2 | 2 +- man2/timerfd_create.2 | 4 ++-- man3/bsearch.3 | 4 ++-- man3/cmsg.3 | 6 +++--- man3/getaddrinfo.3 | 6 +++--- man3/getaddrinfo_a.3 | 2 +- man3/getgrouplist.3 | 2 +- man3/insque.3 | 2 +- man3/malloc_info.3 | 2 +- man3/mbsinit.3 | 2 +- man3/mbstowcs.3 | 2 +- man3/pthread_create.3 | 2 +- man3/pthread_setaffinity_np.3 | 4 ++-- man3/queue.3 | 32 ++++++++++++++++---------------- man3/rtnetlink.3 | 4 ++-- man3/shm_open.3 | 4 ++-- man3/strptime.3 | 2 +- man3/tsearch.3 | 2 +- man7/aio.7 | 4 ++-- man7/fanotify.7 | 3 +-- man7/inotify.7 | 6 +++--- man7/unix.7 | 8 ++++---- 30 files changed, 74 insertions(+), 75 deletions(-)
man1/memusage.1 +6 -6
@@ -247,8 +247,8 @@ main(int argc, char *argv[]) int i, j; int *p; - printf("malloc: %zd\en", sizeof(int) * 100); - p = malloc(sizeof(int) * 100); + printf("malloc: %zd\en", sizeof(*p) * 100); + p = malloc(sizeof(*p) * 100); for (i = 0; i < CYCLES; i++) { if (i < CYCLES / 2)
@@ -256,11 +256,11 @@ main(int argc, char *argv[]) else j--; - printf("realloc: %zd\en", sizeof(int) * (j * 50 + 110)); - p = realloc(p, sizeof(int) * (j * 50 + 100)); + printf("realloc: %zd\en", sizeof(*p) * (j * 50 + 110)); + p = realloc(p, sizeof(*p) * (j * 50 + 100)); - printf("realloc: %zd\en", sizeof(int) * ((j+1) * 150 + 110)); - p = realloc(p, sizeof(int) * ((j + 1) * 150 + 110)); + printf("realloc: %zd\en", sizeof(*p) * ((j+1) * 150 + 110)); + p = realloc(p, sizeof(*p) * ((j + 1) * 150 + 110)); } free(p);
man2/bind.2 +3 -3
@@ -293,14 +293,14 @@ main(int argc, char *argv[]) if (sfd == \-1) handle_error("socket"); - memset(&my_addr, 0, sizeof(struct sockaddr_un)); + memset(&my_addr, 0, sizeof(my_addr)); /* Clear structure */ my_addr.sun_family = AF_UNIX; strncpy(my_addr.sun_path, MY_SOCK_PATH, sizeof(my_addr.sun_path) \- 1); if (bind(sfd, (struct sockaddr *) &my_addr, - sizeof(struct sockaddr_un)) == \-1) + sizeof(my_addr)) == \-1) handle_error("bind"); if (listen(sfd, LISTEN_BACKLOG) == \-1)
@@ -309,7 +309,7 @@ main(int argc, char *argv[]) /* Now we can accept incoming connections one at a time using accept(2) */ - peer_addr_size = sizeof(struct sockaddr_un); + peer_addr_size = sizeof(peer_addr); cfd = accept(sfd, (struct sockaddr *) &peer_addr, &peer_addr_size); if (cfd == \-1)
man2/eventfd.2 +4 -4
@@ -415,8 +415,8 @@ main(int argc, char *argv[]) printf("Child writing %s to efd\en", argv[j]); u = strtoull(argv[j], NULL, 0); /* strtoull() allows various bases */ - s = write(efd, &u, sizeof(uint64_t)); - if (s != sizeof(uint64_t)) + s = write(efd, &u, sizeof(u)); + if (s != sizeof(u)) handle_error("write"); } printf("Child completed write loop\en");
@@ -427,8 +427,8 @@ main(int argc, char *argv[]) sleep(2); printf("Parent about to read\en"); - s = read(efd, &u, sizeof(uint64_t)); - if (s != sizeof(uint64_t)) + s = read(efd, &u, sizeof(u)); + if (s != sizeof(u)) handle_error("read"); printf("Parent read %llu (0x%llx) from efd\en", (unsigned long long) u, (unsigned long long) u);
man2/futex.2 +1 -1
@@ -1839,7 +1839,7 @@ main(int argc, char *argv[]) subsequently use the "shared" futex operations (i.e., not the ones suffixed "_PRIVATE") */ - iaddr = mmap(NULL, sizeof(int) * 2, PROT_READ | PROT_WRITE, + iaddr = mmap(NULL, sizeof(*iaddr) * 2, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, \-1, 0); if (iaddr == MAP_FAILED) errExit("mmap");
man2/open_by_handle_at.2 +2 -2
@@ -586,7 +586,7 @@ main(int argc, char *argv[]) /* Reallocate file_handle structure with correct size */ - fhsize = sizeof(struct file_handle) + fhp\->handle_bytes; + fhsize = sizeof(*fhp) + fhp\->handle_bytes; fhp = realloc(fhp, fhsize); /* Copies fhp\->handle_bytes */ if (fhp == NULL) errExit("realloc");
@@ -707,7 +707,7 @@ main(int argc, char *argv[]) /* Given handle_bytes, we can now allocate file_handle structure */ - fhp = malloc(sizeof(struct file_handle) + handle_bytes); + fhp = malloc(sizeof(*fhp) + handle_bytes); if (fhp == NULL) errExit("malloc");
man2/perf_event_open.2 +3 -3
@@ -3419,9 +3419,9 @@ main(int argc, char **argv) long long count; int fd; - memset(&pe, 0, sizeof(struct perf_event_attr)); + memset(&pe, 0, sizeof(pe)); pe.type = PERF_TYPE_HARDWARE; - pe.size = sizeof(struct perf_event_attr); + pe.size = sizeof(pe); pe.config = PERF_COUNT_HW_INSTRUCTIONS; pe.disabled = 1; pe.exclude_kernel = 1;
@@ -3439,7 +3439,7 @@ main(int argc, char **argv) printf("Measuring instruction count for this printf\en"); ioctl(fd, PERF_EVENT_IOC_DISABLE, 0); - read(fd, &count, sizeof(long long)); + read(fd, &count, sizeof(count)); printf("Used %lld instructions\en", count);
man2/poll.2 +1 -1
@@ -596,7 +596,7 @@ main(int argc, char *argv[]) } num_open_fds = nfds = argc \- 1; - pfds = calloc(nfds, sizeof(struct pollfd)); + pfds = calloc(nfds, sizeof(*pfds)); if (pfds == NULL) errExit("malloc");
man2/signalfd.2 +2 -2
@@ -502,8 +502,8 @@ main(int argc, char *argv[]) handle_error("signalfd"); for (;;) { - s = read(sfd, &fdsi, sizeof(struct signalfd_siginfo)); - if (s != sizeof(struct signalfd_siginfo)) + s = read(sfd, &fdsi, sizeof(fdsi)); + if (s != sizeof(fdsi)) handle_error("read"); if (fdsi.ssi_signo == SIGINT) {
man2/sysctl.2 +1 -1
@@ -154,7 +154,7 @@ main(void) size_t osnamelth; int name[] = { CTL_KERN, KERN_OSTYPE }; - memset(&args, 0, sizeof(struct __sysctl_args)); + memset(&args, 0, sizeof(args)); args.name = name; args.nlen = sizeof(name)/sizeof(name[0]); args.oldval = osname;
man2/timerfd_create.2 +2 -2
@@ -700,8 +700,8 @@ main(int argc, char *argv[]) printf("timer started\en"); for (tot_exp = 0; tot_exp < max_exp;) { - s = read(fd, &exp, sizeof(uint64_t)); - if (s != sizeof(uint64_t)) + s = read(fd, &exp, sizeof(exp)); + if (s != sizeof(exp)) handle_error("read"); tot_exp += exp;
man3/bsearch.3 +2 -2
@@ -124,12 +124,12 @@ main(int argc, char **argv) { int i; - qsort(months, nr_of_months, sizeof(struct mi), compmi); + qsort(months, nr_of_months, sizeof(months[0]), compmi); for (i = 1; i < argc; i++) { struct mi key, *res; key.name = argv[i]; res = bsearch(&key, months, nr_of_months, - sizeof(struct mi), compmi); + sizeof(months[0]), compmi); if (res == NULL) printf("\(aq%s\(aq: unknown month\en", argv[i]); else
man3/cmsg.3 +3 -3
@@ -203,7 +203,7 @@ for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL; cmsg = CMSG_NXTHDR(&msgh, cmsg)) { if (cmsg\->cmsg_level == IPPROTO_IP && cmsg\->cmsg_type == IP_TTL) { - memcpy(&receive_ttl, CMSG_DATA(cmsg), sizeof(int)); + memcpy(&receive_ttl, CMSG_DATA(cmsg), sizeof(received_ttl)); break; } }
@@ -241,8 +241,8 @@ msg.msg_controllen = sizeof(u.buf); 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)); +cmsg\->cmsg_len = CMSG_LEN(sizeof(myfds)); +memcpy(CMSG_DATA(cmsg), myfds, sizeof(myfds)); .EE .in .SH SEE ALSO
man3/getaddrinfo.3 +3 -3
@@ -679,7 +679,7 @@ main(int argc, char *argv[]) exit(EXIT_FAILURE); } - memset(&hints, 0, sizeof(struct addrinfo)); + memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */ hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
@@ -721,7 +721,7 @@ main(int argc, char *argv[]) /* Read datagrams and echo them back to sender */ for (;;) { - peer_addr_len = sizeof(struct sockaddr_storage); + peer_addr_len = sizeof(peer_addr); nread = recvfrom(sfd, buf, BUF_SIZE, 0, (struct sockaddr *) &peer_addr, &peer_addr_len); if (nread == \-1)
@@ -775,7 +775,7 @@ main(int argc, char *argv[]) /* Obtain address(es) matching host/port */ - memset(&hints, 0, sizeof(struct addrinfo)); + memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */ hints.ai_flags = 0;
man3/getaddrinfo_a.3 +1 -1
@@ -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);
man3/getgrouplist.3 +1 -1
@@ -164,7 +164,7 @@ main(int argc, char *argv[]) ngroups = atoi(argv[2]); - groups = malloc(ngroups * sizeof (gid_t)); + groups = malloc(sizeof(*groups) * ngroups); if (groups == NULL) { perror("malloc"); exit(EXIT_FAILURE);
man3/insque.3 +1 -1
@@ -182,7 +182,7 @@ new_element(void) { struct element *e; - e = malloc(sizeof(struct element)); + e = malloc(sizeof(*e)); if (e == NULL) { fprintf(stderr, "malloc() failed\en"); exit(EXIT_FAILURE);
man3/malloc_info.3 +1 -1
@@ -226,7 +226,7 @@ main(int argc, char *argv[]) blockSize = atoi(argv[3]); sleepTime = (argc > 4) ? atoi(argv[4]) : 0; - thr = calloc(numThreads, sizeof(pthread_t)); + thr = calloc(numThreads, sizeof(*thr)); if (thr == NULL) errExit("calloc");
man3/mbsinit.3 +1 -1
@@ -59,7 +59,7 @@ in initial state is to set it to zero: .in +4n .EX mbstate_t state; -memset(&state,0,sizeof(mbstate_t)); +memset(&state, 0, sizeof(state)); .EE .in .PP
man3/mbstowcs.3 +1 -1
@@ -186,7 +186,7 @@ main(int argc, char *argv[]) /* Allocate wide character string of the desired size. Add 1 to allow for terminating null wide character (L\(aq\e0\(aq). */ - wcs = calloc(mbslen + 1, sizeof(wchar_t)); + wcs = calloc(mbslen + 1, sizeof(*wcs)); if (wcs == NULL) { perror("calloc"); exit(EXIT_FAILURE);
man3/pthread_create.3 +1 -1
@@ -361,7 +361,7 @@ main(int argc, char *argv[]) /* Allocate memory for pthread_create() arguments */ - tinfo = calloc(num_threads, sizeof(struct thread_info)); + tinfo = calloc(num_threads, sizeof(*tinfo)); if (tinfo == NULL) handle_error("calloc");
man3/pthread_setaffinity_np.3 +2 -2
@@ -194,13 +194,13 @@ main(int argc, char *argv[]) for (j = 0; j < 8; j++) CPU_SET(j, &cpuset); - s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset); + s = pthread_setaffinity_np(thread, sizeof(cpuset), &cpuset); if (s != 0) handle_error_en(s, "pthread_setaffinity_np"); /* Check the actual affinity mask assigned to the thread */ - s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset); + s = pthread_getaffinity_np(thread, sizeof(cpuset), &cpuset); if (s != 0) handle_error_en(s, "pthread_getaffinity_np");
man3/queue.3 +16 -16
@@ -558,10 +558,10 @@ struct entry { SLIST_INIT(&head); /* Initialize the list. */ -n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ +n1 = malloc(sizeof(*n1)); /* Insert at the head. */ SLIST_INSERT_HEAD(&head, n1, entries); -n2 = malloc(sizeof(struct entry)); /* Insert after. */ +n2 = malloc(sizeof(*n2)); /* Insert after. */ SLIST_INSERT_AFTER(n1, n2, entries); SLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */
@@ -774,13 +774,13 @@ struct entry { STAILQ_INIT(&head); /* Initialize the queue. */ -n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ +n1 = malloc(sizeof(*n1)); /* Insert at the head. */ STAILQ_INSERT_HEAD(&head, n1, entries); -n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */ +n1 = malloc(sizeof(*n1)); /* Insert at the tail. */ STAILQ_INSERT_TAIL(&head, n1, entries); -n2 = malloc(sizeof(struct entry)); /* Insert after. */ +n2 = malloc(sizeof(*n2)); /* Insert after. */ STAILQ_INSERT_AFTER(&head, n1, n2, entries); /* Deletion. */ STAILQ_REMOVE(&head, n2, entry, entries);
@@ -974,13 +974,13 @@ struct entry { LIST_INIT(&head); /* Initialize the list. */ -n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ +n1 = malloc(sizeof(*n1)); /* Insert at the head. */ LIST_INSERT_HEAD(&head, n1, entries); -n2 = malloc(sizeof(struct entry)); /* Insert after. */ +n2 = malloc(sizeof(*n2)); /* Insert after. */ LIST_INSERT_AFTER(n1, n2, entries); -n3 = malloc(sizeof(struct entry)); /* Insert before. */ +n3 = malloc(sizeof(*n3)); /* Insert before. */ LIST_INSERT_BEFORE(n2, n3, entries); LIST_REMOVE(n2, entries); /* Deletion. */
@@ -1232,16 +1232,16 @@ struct entry { TAILQ_INIT(&head); /* Initialize the queue. */ -n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ +n1 = malloc(sizeof(*n1)); /* Insert at the head. */ TAILQ_INSERT_HEAD(&head, n1, entries); -n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */ +n1 = malloc(sizeof(*n1)); /* Insert at the tail. */ TAILQ_INSERT_TAIL(&head, n1, entries); -n2 = malloc(sizeof(struct entry)); /* Insert after. */ +n2 = malloc(sizeof(*n2)); /* Insert after. */ TAILQ_INSERT_AFTER(&head, n1, n2, entries); -n3 = malloc(sizeof(struct entry)); /* Insert before. */ +n3 = malloc(sizeof(*n3)); /* Insert before. */ TAILQ_INSERT_BEFORE(n2, n3, entries); TAILQ_REMOVE(&head, n2, entries); /* Deletion. */
@@ -1425,16 +1425,16 @@ struct entry { CIRCLEQ_INIT(&head); /* Initialize the queue. */ -n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ +n1 = malloc(sizeof(*n1)); /* Insert at the head. */ CIRCLEQ_INSERT_HEAD(&head, n1, entries); -n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */ +n1 = malloc(sizeof(*n1)); /* Insert at the tail. */ CIRCLEQ_INSERT_TAIL(&head, n1, entries); -n2 = malloc(sizeof(struct entry)); /* Insert after. */ +n2 = malloc(sizeof(*n2)); /* Insert after. */ CIRCLEQ_INSERT_AFTER(&head, n1, n2, entries); -n3 = malloc(sizeof(struct entry)); /* Insert before. */ +n3 = malloc(sizeof(*n3)); /* Insert before. */ CIRCLEQ_INSERT_BEFORE(&head, n2, n3, entries); CIRCLEQ_REMOVE(&head, n2, entries); /* Deletion. */
man3/rtnetlink.3 +2 -2
@@ -105,7 +105,7 @@ unsigned int mtu = 1000; int rtnetlink_sk = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE); memset(&req, 0, sizeof(req)); -req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); +req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(req.if)); req.nh.nlmsg_flags = NLM_F_REQUEST; req.nh.nlmsg_type = RTM_NEWLINK; req.if.ifi_family = AF_UNSPEC;
@@ -114,7 +114,7 @@ req.if.ifi_change = 0xffffffff; /* ??? */ rta = (struct rtattr *)(((char *) &req) + NLMSG_ALIGN(req.nh.nlmsg_len)); rta\->rta_type = IFLA_MTU; -rta\->rta_len = RTA_LENGTH(sizeof(unsigned int)); +rta\->rta_len = RTA_LENGTH(sizeof(mtu)); req.nh.nlmsg_len = NLMSG_ALIGN(req.nh.nlmsg_len) + RTA_LENGTH(sizeof(mtu)); memcpy(RTA_DATA(rta), &mtu, sizeof(mtu));
man3/shm_open.3 +2 -2
@@ -382,7 +382,7 @@ main(int argc, char *argv[]) /* Map the object into the caller\(aqs address space */ - struct shmbuf *shmp = mmap(NULL, sizeof(struct shmbuf), + struct shmbuf *shmp = mmap(NULL, sizeof(*shmp), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (shmp == MAP_FAILED)
@@ -471,7 +471,7 @@ main(int argc, char *argv[]) if (fd == \-1) errExit("shm_open"); - struct shmbuf *shmp = mmap(NULL, sizeof(struct shmbuf), + struct shmbuf *shmp = mmap(NULL, sizeof(*shmp), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (shmp == MAP_FAILED)
man3/strptime.3 +1 -1
@@ -429,7 +429,7 @@ main(void) struct tm tm; char buf[255]; - memset(&tm, 0, sizeof(struct tm)); + memset(&tm, 0, sizeof(tm)); strptime("2001\-11\-12 18:31:01", "%Y\-%m\-%d %H:%M:%S", &tm); strftime(buf, sizeof(buf), "%d %b %Y %H:%M", &tm); puts(buf);
man3/tsearch.3 +1 -1
@@ -327,7 +327,7 @@ main(void) srand(time(NULL)); for (i = 0; i < 12; i++) { - ptr = xmalloc(sizeof(int)); + ptr = xmalloc(sizeof(*ptr)); *ptr = rand() & 0xff; val = tsearch((void *) ptr, &root, compare); if (val == NULL)
man7/aio.7 +2 -2
@@ -311,11 +311,11 @@ main(int argc, char *argv[]) /* Allocate our arrays */ - ioList = calloc(numReqs, sizeof(struct ioRequest)); + ioList = calloc(numReqs, sizeof(*ioList)); if (ioList == NULL) errExit("calloc"); - aiocbList = calloc(numReqs, sizeof(struct aiocb)); + aiocbList = calloc(numReqs, sizeof(*aiocbList)); if (aiocbList == NULL) errExit("calloc");
man7/fanotify.7 +1 -2
@@ -808,8 +808,7 @@ handle_events(int fd) response.fd = metadata\->fd; response.response = FAN_ALLOW; - write(fd, &response, - sizeof(struct fanotify_response)); + write(fd, &response, sizeof(response)); } /* Handle closing of writable file event */
man7/inotify.7 +3 -3
@@ -952,7 +952,7 @@ handle_events(int fd, int *wd, int argc, char* argv[]) /* Read some events. */ - len = read(fd, buf, sizeof buf); + len = read(fd, buf, sizeof(buf)); if (len == \-1 && errno != EAGAIN) { perror("read"); exit(EXIT_FAILURE);
@@ -968,7 +968,7 @@ handle_events(int fd, int *wd, int argc, char* argv[]) /* Loop over all events in the buffer */ for (ptr = buf; ptr < buf + len; - ptr += sizeof(struct inotify_event) + event\->len) { + ptr += sizeof(*event) + event\->len) { event = (const struct inotify_event *) ptr;
@@ -1031,7 +1031,7 @@ main(int argc, char* argv[]) /* Allocate memory for watch descriptors */ - wd = calloc(argc, sizeof(int)); + wd = calloc(argc, sizeof(*wd)); if (wd == NULL) { perror("calloc"); exit(EXIT_FAILURE);
man7/unix.7 +4 -4
@@ -948,7 +948,7 @@ main(int argc, char *argv[]) * the structure. */ - memset(&name, 0, sizeof(struct sockaddr_un)); + memset(&name, 0, sizeof(name)); /* Bind socket to socket name. */
@@ -956,7 +956,7 @@ main(int argc, char *argv[]) strncpy(name.sun_path, SOCKET_NAME, sizeof(name.sun_path) \- 1); ret = bind(connection_socket, (const struct sockaddr *) &name, - sizeof(struct sockaddr_un)); + sizeof(name)); if (ret == \-1) { perror("bind"); exit(EXIT_FAILURE);
@@ -1082,7 +1082,7 @@ main(int argc, char *argv[]) * the structure. */ - memset(&addr, 0, sizeof(struct sockaddr_un)); + memset(&addr, 0, sizeof(addr)); /* Connect socket to socket address */
@@ -1090,7 +1090,7 @@ main(int argc, char *argv[]) strncpy(addr.sun_path, SOCKET_NAME, sizeof(addr.sun_path) \- 1); ret = connect (data_socket, (const struct sockaddr *) &addr, - sizeof(struct sockaddr_un)); + sizeof(addr)); if (ret == \-1) { fprintf(stderr, "The server is down.\en"); exit(EXIT_FAILURE);
Signature · 1 line
2.28.0
Hello Alex,
Quoted 12 lines · Alejandro Colomar
Quoted 1 line
From
5df5cae0fb6973df0ab8b3629934f808487112b0Mon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Fri, 21 Aug 2020 16:58:12 +0200
Subject: [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
Quoted 2 lines
Use ``sizeof`` consistently through all the examples, in the following way:
I would really have preferred three patches here, since:
Quoted 6 lines
- Never use a space after ``sizeof``, and always use parentheses instead.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#spaces
(1) This is completely unproblematic from my point of view.
Quoted 6 lines
- Use the name of the variable instead of the type as argument for ``sizeof``, wherever possible.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
(2) This one is up for debate. In many cases it makes sense to do this. However, there are cases where I think that using the struct name can actually help readability. And when I grep through the kernel source, of around 139k lines that use "sizeof", some 37k take a 'struct type' as an argument. SI, I think this kind of change may need to be considered on a case by case basis, rather than as a blanket change.
Quoted 9 lines
- 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.
(3) Is this true? "gcc -Wall" does not complain about this. And, I thought that in both cases, all operands in the expression would be promoted to the largest type. And, on my x86-64 system,
sizeof((sizeof(x) * INT_MAX * 2)) == 8sizeof(INT_MAX * 2 * sizeof(x)) == 8
But, I will say tht I'm not a language lawyer, and C still sometimes has surprises for me. At the least, I'd like to know more about this point.
Thanks,
Michael
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
* Michael Kerrisk <[email protected]>, 2020-08-25, 12:29:
Quoted 6 lines
Quoted 4 lines
``(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));
| ^Quoted 2 lines
sizeof((sizeof(x) * INT_MAX * 2))== 8sizeof(INT_MAX * 2 * sizeof(x))== 8
Hmm? If there was no overflow, surely you should get a number larger than INT_MAX...
Signature · 1 line
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));
}Hello Michael & Jakub,
Quoted 1 line · Michael Kerrisk (man-pages)
I would really have preferred three patches here, since:
I can do that.
Quoted 9 lines
Quoted 7 lines
- Never use a space after ``sizeof``, and always use parentheses instead.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#spaces
(1) This is completely unproblematic from my point of view.
Actually there was only one appearance of that (and another one that used a space before the parentheses). It's unproblematic, but it's so minor that it can be fixed easily.
Quoted 6 lines
Quoted 6 lines
- Use the name of the variable instead of the type as argument for ``sizeof``, wherever possible.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
Quoted 5 lines
(2) This one is up for debate. In many cases it makes sense to do this. However, there are cases where I think that using the struct name can actually help readability. And when I grep through the kernel source, of around 139k lines that use "sizeof", some 37k take a
'struct type'
Quoted 1 line
as an argument. SI, I think this kind of change may need to be
considered on
Quoted 1 line
a case by case basis, rather than as a blanket change.
Ok. I can send a set of patches with a patch for each page.
Quoted 15 lines
Quoted 10 lines
- 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.(3) Is this true? "gcc -Wall" does not complain about this. And, I thought that in both cases, all operands in the expression would be promoted to the largest type. And, on my x86-64 system,
Quoted 6 lines
sizeof((sizeof(x) * INT_MAX * 2))== 8sizeof(INT_MAX * 2 * sizeof(x))== 8But, I will say tht I'm not a language lawyer, and C still sometimes has surprises for me. At the least, I'd like to know more about this point.
Well, when I said the first one doesn't overflow, I meant it's much less likely.
In C, successive multiplications are evaluated left to right (*), and therefore here is what happens:
``(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)
Quoted 1 line
sizeof((sizeof(x) * INT_MAX * 2))== 8
Here you were overflowing a uint64_t (if x were a char, it would not overflow, and the result would be close to UINT64_MAX).
Quoted 1 line
sizeof(INT_MAX * 2 * sizeof(x))== 8
Here you were overflowing int32_t, and it would overflow regardless of sizeof(x).
I wrote an extreme case, but we can agree that 32 bit is much easier to overflow than 64.
(*): https://en.cppreference.com/w/c/language/operator_precedence
Quoted 10 lines · Jakub Wilk
* Michael Kerrisk <[email protected]>, 2020-08-25, 12:29:
Quoted 6 lines
Quoted 4 lines
``(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:
Quoted 7 lines
$ 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));
| ^I guess it will only complain for the first one, doesn't it?
Quoted 7 lines
Quoted 3 lines
sizeof((sizeof(x) * INT_MAX * 2))== 8sizeof(INT_MAX * 2 * sizeof(x))== 8Hmm? If there was no overflow, surely you should get a number larger than
INT_MAX...
INT_MAX is INT32_MAX, and given that size_t is 64 bit, sure we can, but only in the first case.
Hello Alex,
Quoted 34 lines · Alejandro Colomar
Hello Michael & Jakub,
Quoted 1 line · Michael Kerrisk (man-pages)
I would really have preferred three patches here, since:
I can do that.
Quoted 9 lines
Quoted 7 lines
- Never use a space after ``sizeof``, and always use parentheses instead.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#spaces
(1) This is completely unproblematic from my point of view.
Actually there was only one appearance of that (and another one that used a space before the parentheses). It's unproblematic, but it's so minor that it can be fixed easily.
Quoted 6 lines
Quoted 6 lines
- Use the name of the variable instead of the type as argument for ``sizeof``, wherever possible.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
Quoted 5 lines
(2) This one is up for debate. In many cases it makes sense to do this. However, there are cases where I think that using the struct name can actually help readability. And when I grep through the kernel source, of around 139k lines that use "sizeof", some 37k take a
Quoted 6 lines
'struct type'
Quoted 1 line
as an argument. SI, I think this kind of change may need to be
considered on
Quoted 1 line
a case by case basis, rather than as a blanket change.
Ok. I can send a set of patches with a patch for each page.
Okay. Don't do them all at once, since we may change strategy while discussing the first few patches.
Quoted 47 lines
Quoted 14 lines
Quoted 9 lines
- 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.
(3) Is this true? "gcc -Wall" does not complain about this. And, I thought that in both cases, all operands in the expression would be promoted to the largest type. And, on my x86-64 system,
Quoted 6 lines
sizeof((sizeof(x) * INT_MAX * 2))== 8sizeof(INT_MAX * 2 * sizeof(x))== 8But, I will say tht I'm not a language lawyer, and C still sometimes has surprises for me. At the least, I'd like to know more about this point.
Well, when I said the first one doesn't overflow, I meant it's much less likely.
In C, successive multiplications are evaluated left to right (*), and therefore here is what happens:
``(sizeof(x) *
INT_MAX* 2)``:1) sizeof(x) *
INT_MAX(the type is the largest of both, which issize_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 incorrectsize_tvalue)Quoted 1 line
sizeof((sizeof(x) * INT_MAX * 2))== 8
Quoted 7 lines
Here you were overflowing a
uint64_t(if x were a char, it would not overflow, and the result would be close toUINT64_MAX).Quoted 1 line
sizeof(INT_MAX * 2 * sizeof(x))== 8Here you were overflowing
int32_t, and it would overflow regardless of sizeof(x).
Thanks for the lesson in C basics. I clearly did need a refresher :-}.
You and Jakub are of course correct.
If you send the patches in the order (as I numbered in my previous reply):
(1)
(3)
(2) as multiple pieces
that would be best, since the first two patches should obviously be applied, and then we can discuss the last patch(es) case by case.
Thanks,
Michael
Hi Michael,
Quoted 21 lines · Michael Kerrisk (man-pages)
Quoted 21 lines
In C, successive multiplications are evaluated left to right (*), and therefore here is what happens:
``(sizeof(x) *
INT_MAX* 2)``:1) sizeof(x) *
INT_MAX(the type is the largest of both, which issize_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 incorrectsize_tvalue)
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
sizeof((sizeof(x) * INT_MAX * 2))== 8Here you were overflowing a
uint64_t(if x were a char, it would not overflow, and the result would be close toUINT64_MAX).
I also goofed :) I didn't see the sizeof applying to everything. And I was wrong: you aren't overflowing 64 bits, which would need x to be enormous. But the idea is there; the explanation above was right.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
sizeof(INT_MAX * 2 * sizeof(x))== 8Here you were overflowing
int32_t, and it would overflow regardless of sizeof(x).Thanks for the lesson in C basics. I clearly did need a refresher :-}.
You're welcome! It happens me from time to time too. :)
Quoted 11 lines
You and Jakub are of course correct.
If you send the patches in the order (as I numbered in my previous reply):
(1)
(3)
(2) as multiple piecesthat would be best, since the first two patches should obviously be applied, and then we can discuss the last patch(es) case by case.
Ok.
Quoted 5 lines
Thanks,
Michael
Cheers,
Alex.
This is the patch for (1) as numbered in the previous replies:
-----------------------------------------------------------------------
From b0880bd0485d9abc93bcb3055342ec328240b7cf Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Tue, 25 Aug 2020 14:14:21 +0200
Subject: [PATCH] getgrouplist.3, inotify.7: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Never use a space after ``sizeof``, and always use parentheses around the argument.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#spaces
--- man3/getgrouplist.3 | 2 +- man7/inotify.7 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
man3/getgrouplist.3 +1 -1
@@ -164,7 +164,7 @@ main(int argc, char *argv[]) ngroups = atoi(argv[2]); - groups = malloc(ngroups * sizeof (gid_t)); + groups = malloc(ngroups * sizeof(gid_t)); if (groups == NULL) { perror("malloc"); exit(EXIT_FAILURE);
man7/inotify.7 +1 -1
@@ -952,7 +952,7 @@ handle_events(int fd, int *wd, int argc, char* argv[]) /* Read some events. */ - len = read(fd, buf, sizeof buf); + len = read(fd, buf, sizeof(buf)); if (len == \-1 && errno != EAGAIN) { perror("read"); exit(EXIT_FAILURE);
Signature · 1 line
2.28.0
Hi Alex,
Quoted 11 lines · Alejandro Colomar
This is the patch for (1) as numbered in the previous replies:
-----------------------------------------------------------------------
Quoted 1 line
From
b0880bd0485d9abc93bcb3055342ec328240b7cfMon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Tue, 25 Aug 2020 14:14:21 +0200
Subject: [PATCH] getgrouplist.3, inotify.7: Use sizeof consistentlyUse ``sizeof`` consistently through all the examples in the following way:
Quoted 6 lines
- Never use a space after ``sizeof``, and always use parentheses around the argument.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#spaces
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.
Thanks,
Michael
Quoted 33 lines
Signed-off-by Alejandro Colomar <[email protected]>--- man3/getgrouplist.3 | 2 +- man7/inotify.7 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)man3/getgrouplist.3 +1
-1@@ -164,7 +164,7 @@ main(int argc, char *argv[]) ngroups = atoi(argv[2]); - groups = malloc(ngroups * sizeof (gid_t)); + groups = malloc(ngroups * sizeof(gid_t)); if (groups == NULL) { perror("malloc"); exit(EXIT_FAILURE);man7/inotify.7 +1
-1@@ -952,7 +952,7 @@ handle_events(int fd, int *wd, int argc, char* argv[]) /* Read some events. */ - len = read(fd, buf, sizeof buf); + len = read(fd, buf, sizeof(buf)); if (len == \-1 && errno != EAGAIN) { perror("read"); exit(EXIT_FAILURE);
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
PATCH 1/1 cmsg.3, getaddrinfo_a.3 getgrouplist.3: Use sizeof, consistently
Hi Michael,
Quoted 3 lines · Michael Kerrisk (man-pages)
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 <[email protected]>
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
--- man3/cmsg.3 | 2 +- man3/getaddrinfo_a.3 | 2 +- man3/getgrouplist.3 | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
man3/cmsg.3 +1 -1
@@ -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
man3/getaddrinfo_a.3 +1 -1
@@ -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);
man3/getgrouplist.3 +1 -1
@@ -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);
Signature · 1 line
2.28.0
Re: [PATCH] cmsg.3, getaddrinfo_a.3 getgrouplist.3: Use sizeof, consistently
Hello Alex,
Quoted 62 lines · Alejandro Colomar
Hi Michael,
Quoted 3 lines · Michael Kerrisk (man-pages)
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:
--------------------------------------------------------------------
Quoted 1 line
From
1cb973629d94a048c5dcbe13fef76173f99dc3deMon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Tue, 25 Aug 2020 14:52:03 +0200
Subject: [PATCH] cmsg.3, getaddrinfo_a.3 getgrouplist.3: Use sizeof
consistentlyUse ``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 issize_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 incorrectsize_tvalue)
Quoted 3 lines
(*): https://en.cppreference.com/w/c/language/operator_precedence
Signed-off-by Alejandro Colomar <[email protected]>
Thanks! Patch applied.
Cheers,
Michael
Quoted 46 lines
--- man3/cmsg.3 | 2 +- man3/getaddrinfo_a.3 | 2 +- man3/getgrouplist.3 | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)man3/cmsg.3 +1
-1@@ -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 ALSOman3/getaddrinfo_a.3 +1
-1@@ -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);man3/getgrouplist.3 +1
-1@@ -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);
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
PATCH 1/1 memusage.1: Use sizeof consistently
Hi Michael,
Continuing with the series, this is the first of the last set of
patches: (2).1 as numbered in previous emails.
Regards,
Alex.
------------------------------------------------------------------------
From ad5f958ed68079791d6e35f9d70ca5ec2a72c43b Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 12:11:18 +0200
Subject: [PATCH] memusage.1: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man1/memusage.1 | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
man1/memusage.1 +6 -6
@@ -247,8 +247,8 @@ main(int argc, char *argv[]) int i, j; int *p; - printf("malloc: %zd\en", sizeof(int) * 100); - p = malloc(sizeof(int) * 100); + printf("malloc: %zd\en", sizeof(*p) * 100); + p = malloc(sizeof(*p) * 100); for (i = 0; i < CYCLES; i++) { if (i < CYCLES / 2)
@@ -256,11 +256,11 @@ main(int argc, char *argv[]) else j--; - printf("realloc: %zd\en", sizeof(int) * (j * 50 + 110)); - p = realloc(p, sizeof(int) * (j * 50 + 100)); + printf("realloc: %zd\en", sizeof(*p) * (j * 50 + 110)); + p = realloc(p, sizeof(*p) * (j * 50 + 100)); - printf("realloc: %zd\en", sizeof(int) * ((j+1) * 150 + 110)); - p = realloc(p, sizeof(int) * ((j + 1) * 150 + 110)); + printf("realloc: %zd\en", sizeof(*p) * ((j+1) * 150 + 110)); + p = realloc(p, sizeof(*p) * ((j + 1) * 150 + 110)); } free(p);
Signature · 1 line
2.28.0
Re: [PATCH] memusage.1: Use sizeof consistently
Hello Alex,
Quoted 4 lines · Alejandro Colomar
Hi Michael,
Continuing with the series, this is the first of the last set of
patches: (2).1 as numbered in previous emails.
I must admit that I don't care too much either way on this. That is to say, I'm not sure one way is any clearer than the other. However, I have applied the patch.
In passing, I note that there is a clarity issue that I do find more significant though: the repeated calculations in the malloc() and printf() calls. So I changed that:
https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/commit/?id=15fc4aab1f22c2d4f62ab7f74bbb844942708633
Thanks,
Michael
Quoted 52 lines
------------------------------------------------------------------------
Quoted 1 line
From
ad5f958ed68079791d6e35f9d70ca5ec2a72c43bMon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 12:11:18 +0200
Subject: [PATCH] memusage.1: Use sizeof consistentlyUse ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memorySigned-off-by Alejandro Colomar <[email protected]>--- man1/memusage.1 | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)man1/memusage.1 +6
-6@@ -247,8 +247,8 @@ main(int argc, char *argv[]) int i, j; int *p; - printf("malloc: %zd\en", sizeof(int) * 100); - p = malloc(sizeof(int) * 100); + printf("malloc: %zd\en", sizeof(*p) * 100); + p = malloc(sizeof(*p) * 100); for (i = 0; i < CYCLES; i++) { if (i < CYCLES / 2)@@ -256,11 +256,11 @@ main(int argc, char *argv[]) else j--; - printf("realloc: %zd\en", sizeof(int) * (j * 50 + 110)); - p = realloc(p, sizeof(int) * (j * 50 + 100)); + printf("realloc: %zd\en", sizeof(*p) * (j * 50 + 110)); + p = realloc(p, sizeof(*p) * (j * 50 + 100)); - printf("realloc: %zd\en", sizeof(int) * ((j+1) * 150 + 110)); - p = realloc(p, sizeof(int) * ((j + 1) * 150 + 110)); + printf("realloc: %zd\en", sizeof(*p) * ((j+1) * 150 + 110)); + p = realloc(p, sizeof(*p) * ((j + 1) * 150 + 110)); } free(p);
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
PATCH 2/34 bind.2: Use sizeof consistently
Hello Michael,
Quoted 3 lines · Michael Kerrisk (man-pages)
I must admit that I don't care too much either way on this. That is to say, I'm not sure one way is any clearer than the other. However, I have applied the patch.
There are places where I wouldn't say there are any readability benefits. However, there are functions such as malloc() or memset(), where using the type could lead to future bugs, so IMHO it's better to just be consistent and use always the name, unless there are clear readability problems (or other problems).
In the end, someone thought it to be important enough to write it in the kernel coding style.
I don't expect all of these patches to be applied, as I had doubts when writing some of them, but we can discuss those where it is better to keep the type.
Quoted 5 lines
In passing, I note that there is a clarity issue that I do find more significant though: the repeated calculations in the
malloc()andprintf()calls. So I changed that:
:)
Cheers,
Alex
------------------------------------------------------------------------
From 54016160b603454fbe4f38d6a81886a03fe2ffdf Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:24:43 +0200
Subject: [PATCH 02/34] bind.2: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man2/bind.2 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
man2/bind.2 +3 -3
@@ -293,14 +293,14 @@ main(int argc, char *argv[]) if (sfd == \-1) handle_error("socket"); - memset(&my_addr, 0, sizeof(struct sockaddr_un)); + memset(&my_addr, 0, sizeof(my_addr)); /* Clear structure */ my_addr.sun_family = AF_UNIX; strncpy(my_addr.sun_path, MY_SOCK_PATH, sizeof(my_addr.sun_path) \- 1); if (bind(sfd, (struct sockaddr *) &my_addr, - sizeof(struct sockaddr_un)) == \-1) + sizeof(my_addr)) == \-1) handle_error("bind"); if (listen(sfd, LISTEN_BACKLOG) == \-1)
@@ -309,7 +309,7 @@ main(int argc, char *argv[]) /* Now we can accept incoming connections one at a time using accept(2) */ - peer_addr_size = sizeof(struct sockaddr_un); + peer_addr_size = sizeof(peer_addr); cfd = accept(sfd, (struct sockaddr *) &peer_addr, &peer_addr_size); if (cfd == \-1)
Signature · 1 line
2.28.0
PATCH 3/34 eventfd.2: Use sizeof consistently
From 5399e0a620c417c1003c17fb04a45ce1a7854acd Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:25:59 +0200
Subject: [PATCH 03/34] eventfd.2: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man2/eventfd.2 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
man2/eventfd.2 +4 -4
@@ -415,8 +415,8 @@ main(int argc, char *argv[]) printf("Child writing %s to efd\en", argv[j]); u = strtoull(argv[j], NULL, 0); /* strtoull() allows various bases */ - s = write(efd, &u, sizeof(uint64_t)); - if (s != sizeof(uint64_t)) + s = write(efd, &u, sizeof(u)); + if (s != sizeof(u)) handle_error("write"); } printf("Child completed write loop\en");
@@ -427,8 +427,8 @@ main(int argc, char *argv[]) sleep(2); printf("Parent about to read\en"); - s = read(efd, &u, sizeof(uint64_t)); - if (s != sizeof(uint64_t)) + s = read(efd, &u, sizeof(u)); + if (s != sizeof(u)) handle_error("read"); printf("Parent read %llu (0x%llx) from efd\en", (unsigned long long) u, (unsigned long long) u);
Signature · 1 line
2.28.0
Re: [PATCH (2) 03/34] eventfd.2: Use sizeof consistently
Hello Alex,
Quoted 36 lines · Alejandro Colomar
Quoted 1 line
From
5399e0a620c417c1003c17fb04a45ce1a7854acdMon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:25:59 +0200
Subject: [PATCH 03/34] eventfd.2: Use sizeof consistentlyUse ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memorySigned-off-by Alejandro Colomar <[email protected]>--- man2/eventfd.2 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)man2/eventfd.2 +2
-2@@ -415,8 +415,8 @@ main(int argc, char *argv[]) printf("Child writing %s to efd\en", argv[j]); u = strtoull(argv[j], NULL, 0); /* strtoull() allows various bases */ - s = write(efd, &u, sizeof(uint64_t)); - if (s != sizeof(uint64_t)) + s = write(efd, &u, sizeof(u)); + if (s != sizeof(u)) handle_error("write"); } printf("Child completed write loop\en");
man2/eventfd.2 main(int argc, char *argv[]) · line 434
Quoted 10 lines
man2/eventfd.2 +2
-2@@ -427,8 +427,8 @@ main(int argc, char *argv[]) sleep(2); printf("Parent about to read\en"); - s = read(efd, &u, sizeof(uint64_t)); - if (s != sizeof(uint64_t)) + s = read(efd, &u, sizeof(u)); + if (s != sizeof(u)) handle_error("read"); printf("Parent read %llu (0x%llx) from efd\en", (unsigned long long) u, (unsigned long long) u);
This is an example where I am dubious that the change is a good idea. For this API, the units that are being read/written really *must* be 8-byte integers (it's baked into the API), and I feel that explicitly mentioning the types in the example code reinforces that point.
For the moment, I won't apply this patch.
Cheers,
Michael
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
PATCH 4/34 futex.2: Use sizeof consistently
In reply to Alejandro ColomarFrom 21cd85c6f11390c71ed4475f1f9d55910891cf23 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:27:01 +0200
Subject: [PATCH 04/34] futex.2: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man2/futex.2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
man2/futex.2 +1 -1
@@ -1839,7 +1839,7 @@ main(int argc, char *argv[]) subsequently use the "shared" futex operations (i.e., not the ones suffixed "_PRIVATE") */ - iaddr = mmap(NULL, sizeof(int) * 2, PROT_READ | PROT_WRITE, + iaddr = mmap(NULL, sizeof(*iaddr) * 2, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, \-1, 0); if (iaddr == MAP_FAILED) errExit("mmap");
Signature · 1 line
2.28.0
PATCH 5/34 open_by_handle_at.2: Use sizeof consistently
From 0176a7981aead1a202d5d0295b074e165b0d39dd Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:27:55 +0200
Subject: [PATCH 05/34] open_by_handle_at.2: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man2/open_by_handle_at.2 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
man2/open_by_handle_at.2 +2 -2
@@ -586,7 +586,7 @@ main(int argc, char *argv[]) /* Reallocate file_handle structure with correct size */ - fhsize = sizeof(struct file_handle) + fhp\->handle_bytes; + fhsize = sizeof(*fhp) + fhp\->handle_bytes; fhp = realloc(fhp, fhsize); /* Copies fhp\->handle_bytes */ if (fhp == NULL) errExit("realloc");
@@ -707,7 +707,7 @@ main(int argc, char *argv[]) /* Given handle_bytes, we can now allocate file_handle structure */ - fhp = malloc(sizeof(struct file_handle) + handle_bytes); + fhp = malloc(sizeof(*fhp) + handle_bytes); if (fhp == NULL) errExit("malloc");
Signature · 1 line
2.28.0
PATCH 6/34 perf_event_open.2: Use sizeof consistently
From dbb7b520bd4314488122835c87f45b685ce45b28 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:29:02 +0200
Subject: [PATCH 06/34] perf_event_open.2: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man2/perf_event_open.2 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
man2/perf_event_open.2 +2 -2
@@ -3419,9 +3419,9 @@ main(int argc, char **argv) long long count; int fd; - memset(&pe, 0, sizeof(struct perf_event_attr)); + memset(&pe, 0, sizeof(pe)); pe.type = PERF_TYPE_HARDWARE; - pe.size = sizeof(struct perf_event_attr); + pe.size = sizeof(pe); pe.config = PERF_COUNT_HW_INSTRUCTIONS; pe.disabled = 1; pe.exclude_kernel = 1;
Signature · 1 line
2.28.0
PATCH 7/34 perf_event_open.2: Use sizeof consistently
From ec1f70a162b0f4ea7a191baf8c098d7872dedce6 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:29:33 +0200
Subject: [PATCH 07/34] perf_event_open.2: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man2/perf_event_open.2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
man2/perf_event_open.2 +1 -1
@@ -3439,7 +3439,7 @@ main(int argc, char **argv) printf("Measuring instruction count for this printf\en"); ioctl(fd, PERF_EVENT_IOC_DISABLE, 0); - read(fd, &count, sizeof(long long)); + read(fd, &count, sizeof(count)); printf("Used %lld instructions\en", count);
Signature · 1 line
2.28.0
Re: [PATCH (2) 07/34] perf_event_open.2: Use sizeof consistently
Hello ALex,
Quoted 5 lines · Alejandro Colomar
Quoted 1 line
From
ec1f70a162b0f4ea7a191baf8c098d7872dedce6Mon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:29:33 +0200
Subject: [PATCH 07/34] perf_event_open.2: Use sizeof consistently
Quoted 8 lines
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
Thanks. Patch applied.
Cheers,
Michael
Quoted 10 lines
Signed-off-by Alejandro Colomar <[email protected]>--- man2/perf_event_open.2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)man2/perf_event_open.2 +0
-0
man2/perf_event_open.2 main(int argc, char **argv) · line 3445
Quoted 8 lines
man2/perf_event_open.2 +1
-1@@ -3439,7 +3439,7 @@ main(int argc, char **argv) printf("Measuring instruction count for this printf\en"); ioctl(fd, PERF_EVENT_IOC_DISABLE, 0); - read(fd, &count, sizeof(long long)); + read(fd, &count, sizeof(count)); printf("Used %lld instructions\en", count);
Hello
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
PATCH 8/34 poll.2: Use sizeof consistently
In reply to Alejandro ColomarFrom 791b2082c91de4fb49a1a46e9c11d294aac09050 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:34:52 +0200
Subject: [PATCH 08/34] poll.2: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man2/poll.2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
man2/poll.2 +1 -1
@@ -596,7 +596,7 @@ main(int argc, char *argv[]) } num_open_fds = nfds = argc \- 1; - pfds = calloc(nfds, sizeof(struct pollfd)); + pfds = calloc(nfds, sizeof(*pfds)); if (pfds == NULL) errExit("malloc");
Signature · 1 line
2.28.0
PATCH 9/34 sysctl.2: Use sizeof consistently
From 90572d59a4f3b1986e57c0b32e14e4e68ecab716 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:39:56 +0200
Subject: [PATCH 09/34] sysctl.2: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man2/sysctl.2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
man2/sysctl.2 +1 -1
@@ -154,7 +154,7 @@ main(void) size_t osnamelth; int name[] = { CTL_KERN, KERN_OSTYPE }; - memset(&args, 0, sizeof(struct __sysctl_args)); + memset(&args, 0, sizeof(args)); args.name = name; args.nlen = sizeof(name)/sizeof(name[0]); args.oldval = osname;
Signature · 1 line
2.28.0
PATCH 10/34 signalfd.2: Use sizeof consistently
From 9a10e681849a57f75d89cf6943901a3df8097399 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:41:31 +0200
Subject: [PATCH 10/34] signalfd.2: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man2/signalfd.2 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
man2/signalfd.2 +2 -2
@@ -502,8 +502,8 @@ main(int argc, char *argv[]) handle_error("signalfd"); for (;;) { - s = read(sfd, &fdsi, sizeof(struct signalfd_siginfo)); - if (s != sizeof(struct signalfd_siginfo)) + s = read(sfd, &fdsi, sizeof(fdsi)); + if (s != sizeof(fdsi)) handle_error("read"); if (fdsi.ssi_signo == SIGINT) {
Signature · 1 line
2.28.0
PATCH 11/34 timerfd_create.2: Use sizeof consistently
From d347c933a8c253028f8f76c4170b65b85ce7d605 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:42:28 +0200
Subject: [PATCH 11/34] timerfd_create.2: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man2/timerfd_create.2 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
man2/timerfd_create.2 +2 -2
@@ -700,8 +700,8 @@ main(int argc, char *argv[]) printf("timer started\en"); for (tot_exp = 0; tot_exp < max_exp;) { - s = read(fd, &exp, sizeof(uint64_t)); - if (s != sizeof(uint64_t)) + s = read(fd, &exp, sizeof(exp)); + if (s != sizeof(exp)) handle_error("read"); tot_exp += exp;
Signature · 1 line
2.28.0
PATCH 12/34 bsearch.3: Use sizeof consistently
From 886db3bad74f35fc40a1238a0d2f35ace3dc7620 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:43:23 +0200
Subject: [PATCH 12/34] bsearch.3: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man3/bsearch.3 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
man3/bsearch.3 +2 -2
@@ -124,12 +124,12 @@ main(int argc, char **argv) { int i; - qsort(months, nr_of_months, sizeof(struct mi), compmi); + qsort(months, nr_of_months, sizeof(months[0]), compmi); for (i = 1; i < argc; i++) { struct mi key, *res; key.name = argv[i]; res = bsearch(&key, months, nr_of_months, - sizeof(struct mi), compmi); + sizeof(months[0]), compmi); if (res == NULL) printf("\(aq%s\(aq: unknown month\en", argv[i]); else
Signature · 1 line
2.28.0
PATCH 13/34 cmsg.3: Use sizeof consistently
From ba70b1e8addad4ef9f2a490d2069b112d09ba9f1 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:44:41 +0200
Subject: [PATCH 13/34] cmsg.3: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man3/cmsg.3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
man3/cmsg.3 +1 -1
@@ -203,7 +203,7 @@ for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL; cmsg = CMSG_NXTHDR(&msgh, cmsg)) { if (cmsg\->cmsg_level == IPPROTO_IP && cmsg\->cmsg_type == IP_TTL) { - memcpy(&receive_ttl, CMSG_DATA(cmsg), sizeof(int)); + memcpy(&receive_ttl, CMSG_DATA(cmsg), sizeof(received_ttl)); break; } }
Signature · 1 line
2.28.0
PATCH 14/34 cmsg.3: Use sizeof consistently
From 374b31bee6762314ab48988e2e78a3a6a2f96834 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:44:54 +0200
Subject: [PATCH 14/34] cmsg.3: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man3/cmsg.3 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
man3/cmsg.3 +2 -2
@@ -241,8 +241,8 @@ msg.msg_controllen = sizeof(u.buf); 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, sizeof(int) * NUM_FD); +cmsg\->cmsg_len = CMSG_LEN(sizeof(myfds)); +memcpy(CMSG_DATA(cmsg), myfds, sizeof(myfds)); .EE .in .SH SEE ALSO
Signature · 1 line
2.28.0
PATCH 15/34 getaddrinfo.3: Use sizeof consistently
From eef612c1f5f039421bd0fa167e1972e98d934bce Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:46:06 +0200
Subject: [PATCH 15/34] getaddrinfo.3: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man3/getaddrinfo.3 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
man3/getaddrinfo.3 +2 -2
@@ -679,7 +679,7 @@ main(int argc, char *argv[]) exit(EXIT_FAILURE); } - memset(&hints, 0, sizeof(struct addrinfo)); + memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */ hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
@@ -775,7 +775,7 @@ main(int argc, char *argv[]) /* Obtain address(es) matching host/port */ - memset(&hints, 0, sizeof(struct addrinfo)); + memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */ hints.ai_flags = 0;
Signature · 1 line
2.28.0
PATCH 16/34 getaddrinfo.3: Use sizeof consistently
From f926f02eede41183aed7ae279eb333273bd0c8dc Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:46:35 +0200
Subject: [PATCH 16/34] getaddrinfo.3: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man3/getaddrinfo.3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
man3/getaddrinfo.3 +1 -1
@@ -721,7 +721,7 @@ main(int argc, char *argv[]) /* Read datagrams and echo them back to sender */ for (;;) { - peer_addr_len = sizeof(struct sockaddr_storage); + peer_addr_len = sizeof(peer_addr); nread = recvfrom(sfd, buf, BUF_SIZE, 0, (struct sockaddr *) &peer_addr, &peer_addr_len); if (nread == \-1)
Signature · 1 line
2.28.0
PATCH 17/34 getgrouplist.3: Use sizeof consistently
From 7ca60fc88b831818c1f1722919af220a646761ab Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:47:15 +0200
Subject: [PATCH 17/34] getgrouplist.3: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man3/getgrouplist.3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
man3/getgrouplist.3 +1 -1
@@ -164,7 +164,7 @@ main(int argc, char *argv[]) ngroups = atoi(argv[2]); - groups = malloc(sizeof(gid_t) * ngroups); + groups = malloc(sizeof(*groups) * ngroups); if (groups == NULL) { perror("malloc"); exit(EXIT_FAILURE);
Signature · 1 line
2.28.0
PATCH 18/34 insque.3: Use sizeof consistently
From b43c812d1e129a8d2b34df7624a78baa3bf1273e Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:47:45 +0200
Subject: [PATCH 18/34] insque.3: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man3/insque.3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
man3/insque.3 +1 -1
@@ -182,7 +182,7 @@ new_element(void) { struct element *e; - e = malloc(sizeof(struct element)); + e = malloc(sizeof(*e)); if (e == NULL) { fprintf(stderr, "malloc() failed\en"); exit(EXIT_FAILURE);
Signature · 1 line
2.28.0
PATCH 19/34 malloc_info.3: Use sizeof consistently
From bf1a7799161fe1f08570c4f25d87a0f4e0b51ef1 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:49:17 +0200
Subject: [PATCH 19/34] malloc_info.3: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man3/malloc_info.3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
man3/malloc_info.3 +1 -1
@@ -226,7 +226,7 @@ main(int argc, char *argv[]) blockSize = atoi(argv[3]); sleepTime = (argc > 4) ? atoi(argv[4]) : 0; - thr = calloc(numThreads, sizeof(pthread_t)); + thr = calloc(numThreads, sizeof(*thr)); if (thr == NULL) errExit("calloc");
Signature · 1 line
2.28.0
Re: [PATCH (2) 20/34] mbsinit.3: Use sizeof consistently
From 77587b9c0b58cde232ea3bfaeef5f50607d83b5e Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:50:10 +0200
Subject: [PATCH 20/34] mbsinit.3: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man3/mbsinit.3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
man3/mbsinit.3 +1 -1
@@ -59,7 +59,7 @@ in initial state is to set it to zero: .in +4n .EX mbstate_t state; -memset(&state, 0, sizeof(mbstate_t)); +memset(&state, 0, sizeof(state)); .EE .in .PP
Signature · 1 line
2.28.0
PATCH 21/34 mbstowcs.3: Use sizeof consistently
From 1b70e7a1da093e4a8e3be79aaed623b21c10e763 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:51:07 +0200
Subject: [PATCH 21/34] mbstowcs.3: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man3/mbstowcs.3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
man3/mbstowcs.3 +1 -1
@@ -186,7 +186,7 @@ main(int argc, char *argv[]) /* Allocate wide character string of the desired size. Add 1 to allow for terminating null wide character (L\(aq\e0\(aq). */ - wcs = calloc(mbslen + 1, sizeof(wchar_t)); + wcs = calloc(mbslen + 1, sizeof(*wcs)); if (wcs == NULL) { perror("calloc"); exit(EXIT_FAILURE);
Signature · 1 line
2.28.0
PATCH 22/34 pthread_create.3: Use sizeof consistently
From 03783d811fad4783b394c01a4ac68ca6d92b6c58 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:52:22 +0200
Subject: [PATCH 22/34] pthread_create.3: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man3/pthread_create.3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
man3/pthread_create.3 +1 -1
@@ -361,7 +361,7 @@ main(int argc, char *argv[]) /* Allocate memory for pthread_create() arguments */ - tinfo = calloc(num_threads, sizeof(struct thread_info)); + tinfo = calloc(num_threads, sizeof(*tinfo)); if (tinfo == NULL) handle_error("calloc");
Signature · 1 line
2.28.0
PATCH 23/34 pthread_setaffinity_np.3: Use sizeof consistently
From ba0d73749a85e835aa43a4134a1b6906062943f3 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:53:12 +0200
Subject: [PATCH 23/34] pthread_setaffinity_np.3: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man3/pthread_setaffinity_np.3 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
man3/pthread_setaffinity_np.3 +2 -2
@@ -194,13 +194,13 @@ main(int argc, char *argv[]) for (j = 0; j < 8; j++) CPU_SET(j, &cpuset); - s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset); + s = pthread_setaffinity_np(thread, sizeof(cpuset), &cpuset); if (s != 0) handle_error_en(s, "pthread_setaffinity_np"); /* Check the actual affinity mask assigned to the thread */ - s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset); + s = pthread_getaffinity_np(thread, sizeof(cpuset), &cpuset); if (s != 0) handle_error_en(s, "pthread_getaffinity_np");
Signature · 1 line
2.28.0
PATCH 24/34 queue.3: Use sizeof consistently
From 6f3e17cf38bc3718332c7151899e253dbc91affb Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:53:51 +0200
Subject: [PATCH 24/34] queue.3: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man3/queue.3 | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-)
man3/queue.3 +16 -16
@@ -559,10 +559,10 @@ struct entry { SLIST_INIT(&head); /* Initialize the list. */ -n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ +n1 = malloc(sizeof(*n1)); /* Insert at the head. */ SLIST_INSERT_HEAD(&head, n1, entries); -n2 = malloc(sizeof(struct entry)); /* Insert after. */ +n2 = malloc(sizeof(*n2)); /* Insert after. */ SLIST_INSERT_AFTER(n1, n2, entries); SLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */
@@ -775,13 +775,13 @@ struct entry { STAILQ_INIT(&head); /* Initialize the queue. */ -n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ +n1 = malloc(sizeof(*n1)); /* Insert at the head. */ STAILQ_INSERT_HEAD(&head, n1, entries); -n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */ +n1 = malloc(sizeof(*n1)); /* Insert at the tail. */ STAILQ_INSERT_TAIL(&head, n1, entries); -n2 = malloc(sizeof(struct entry)); /* Insert after. */ +n2 = malloc(sizeof(*n2)); /* Insert after. */ STAILQ_INSERT_AFTER(&head, n1, n2, entries); /* Deletion. */ STAILQ_REMOVE(&head, n2, entry, entries);
@@ -975,13 +975,13 @@ struct entry { LIST_INIT(&head); /* Initialize the list. */ -n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ +n1 = malloc(sizeof(*n1)); /* Insert at the head. */ LIST_INSERT_HEAD(&head, n1, entries); -n2 = malloc(sizeof(struct entry)); /* Insert after. */ +n2 = malloc(sizeof(*n2)); /* Insert after. */ LIST_INSERT_AFTER(n1, n2, entries); -n3 = malloc(sizeof(struct entry)); /* Insert before. */ +n3 = malloc(sizeof(*n3)); /* Insert before. */ LIST_INSERT_BEFORE(n2, n3, entries); LIST_REMOVE(n2, entries); /* Deletion. */
@@ -1233,16 +1233,16 @@ struct entry { TAILQ_INIT(&head); /* Initialize the queue. */ -n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ +n1 = malloc(sizeof(*n1)); /* Insert at the head. */ TAILQ_INSERT_HEAD(&head, n1, entries); -n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */ +n1 = malloc(sizeof(*n1)); /* Insert at the tail. */ TAILQ_INSERT_TAIL(&head, n1, entries); -n2 = malloc(sizeof(struct entry)); /* Insert after. */ +n2 = malloc(sizeof(*n2)); /* Insert after. */ TAILQ_INSERT_AFTER(&head, n1, n2, entries); -n3 = malloc(sizeof(struct entry)); /* Insert before. */ +n3 = malloc(sizeof(*n3)); /* Insert before. */ TAILQ_INSERT_BEFORE(n2, n3, entries); TAILQ_REMOVE(&head, n2, entries); /* Deletion. */
@@ -1426,16 +1426,16 @@ struct entry { CIRCLEQ_INIT(&head); /* Initialize the queue. */ -n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ +n1 = malloc(sizeof(*n1)); /* Insert at the head. */ CIRCLEQ_INSERT_HEAD(&head, n1, entries); -n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */ +n1 = malloc(sizeof(*n1)); /* Insert at the tail. */ CIRCLEQ_INSERT_TAIL(&head, n1, entries); -n2 = malloc(sizeof(struct entry)); /* Insert after. */ +n2 = malloc(sizeof(*n2)); /* Insert after. */ CIRCLEQ_INSERT_AFTER(&head, n1, n2, entries); -n3 = malloc(sizeof(struct entry)); /* Insert before. */ +n3 = malloc(sizeof(*n3)); /* Insert before. */ CIRCLEQ_INSERT_BEFORE(&head, n2, n3, entries); CIRCLEQ_REMOVE(&head, n2, entries); /* Deletion. */
Signature · 1 line
2.28.0
PATCH 25/34 rtnetlink.3: Use sizeof consistently
From eca0c1500aa4db77c8c8d079798fe080e83e9935 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:55:00 +0200
Subject: [PATCH 25/34] rtnetlink.3: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man3/rtnetlink.3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
man3/rtnetlink.3 +1 -1
@@ -105,7 +105,7 @@ unsigned int mtu = 1000; int rtnetlink_sk = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE); memset(&req, 0, sizeof(req)); -req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); +req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(req.if)); req.nh.nlmsg_flags = NLM_F_REQUEST; req.nh.nlmsg_type = RTM_NEWLINK; req.if.ifi_family = AF_UNSPEC;
Signature · 1 line
2.28.0
PATCH 26/34 rtnetlink.3: Use sizeof consistently
From 2b7a02916c24af8cbb0cbb071a7223fe48a52571 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:55:11 +0200
Subject: [PATCH 26/34] rtnetlink.3: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man3/rtnetlink.3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
man3/rtnetlink.3 +1 -1
@@ -114,7 +114,7 @@ req.if.ifi_change = 0xffffffff; /* ??? */ rta = (struct rtattr *)(((char *) &req) + NLMSG_ALIGN(req.nh.nlmsg_len)); rta\->rta_type = IFLA_MTU; -rta\->rta_len = RTA_LENGTH(sizeof(unsigned int)); +rta\->rta_len = RTA_LENGTH(sizeof(mtu)); req.nh.nlmsg_len = NLMSG_ALIGN(req.nh.nlmsg_len) + RTA_LENGTH(sizeof(mtu)); memcpy(RTA_DATA(rta), &mtu, sizeof(mtu));
Signature · 1 line
2.28.0
PATCH 27/34 shm_open.3: Use sizeof consistently
From d3f439e697c9a384cad55e84a584493088eb4291 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:55:48 +0200
Subject: [PATCH 27/34] shm_open.3: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man3/shm_open.3 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
man3/shm_open.3 +2 -2
@@ -382,7 +382,7 @@ main(int argc, char *argv[]) /* Map the object into the caller\(aqs address space */ - struct shmbuf *shmp = mmap(NULL, sizeof(struct shmbuf), + struct shmbuf *shmp = mmap(NULL, sizeof(*shmp), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (shmp == MAP_FAILED)
@@ -471,7 +471,7 @@ main(int argc, char *argv[]) if (fd == \-1) errExit("shm_open"); - struct shmbuf *shmp = mmap(NULL, sizeof(struct shmbuf), + struct shmbuf *shmp = mmap(NULL, sizeof(*shmp), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (shmp == MAP_FAILED)
Signature · 1 line
2.28.0
PATCH 28/34 strptime.3: Use sizeof consistently
From f000e36d106a22d68f26e9cebe84758854739a42 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:56:21 +0200
Subject: [PATCH 28/34] strptime.3: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man3/strptime.3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
man3/strptime.3 +1 -1
@@ -429,7 +429,7 @@ main(void) struct tm tm; char buf[255]; - memset(&tm, 0, sizeof(struct tm)); + memset(&tm, 0, sizeof(tm)); strptime("2001\-11\-12 18:31:01", "%Y\-%m\-%d %H:%M:%S", &tm); strftime(buf, sizeof(buf), "%d %b %Y %H:%M", &tm); puts(buf);
Signature · 1 line
2.28.0
Re: [PATCH (2) 28/34] strptime.3: Use sizeof consistently
Hi Alex,
Quoted 8 lines · Alejandro Colomar
Quoted 1 line
From
f000e36d106a22d68f26e9cebe84758854739a42Mon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:56:21 +0200
Subject: [PATCH 28/34] strptime.3: Use sizeof consistentlyUse ``sizeof`` consistently through all the examples in the following way:
Quoted 7 lines
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memorySigned-off-by Alejandro Colomar <[email protected]>
Patch applied.
Thanks,
Michael
Quoted 18 lines
--- man3/strptime.3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)man3/strptime.3 +1
-1@@ -429,7 +429,7 @@ main(void) struct tm tm; char buf[255]; - memset(&tm, 0, sizeof(struct tm)); + memset(&tm, 0, sizeof(tm)); strptime("2001\-11\-12 18:31:01", "%Y\-%m\-%d %H:%M:%S", &tm); strftime(buf, sizeof(buf), "%d %b %Y %H:%M", &tm); puts(buf);
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
PATCH 29/34 tsearch.3: Use sizeof consistently
In reply to Alejandro ColomarFrom ff3fe9cdfd280fce97e9289e9a91fb6cc2c1c368 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:56:52 +0200
Subject: [PATCH 29/34] tsearch.3: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man3/tsearch.3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
man3/tsearch.3 +1 -1
@@ -327,7 +327,7 @@ main(void) srand(time(NULL)); for (i = 0; i < 12; i++) { - ptr = xmalloc(sizeof(int)); + ptr = xmalloc(sizeof(*ptr)); *ptr = rand() & 0xff; val = tsearch((void *) ptr, &root, compare); if (val == NULL)
Signature · 1 line
2.28.0
Re: [PATCH (2) 29/34] tsearch.3: Use sizeof consistently
Hello Alex,
Quoted 8 lines · Alejandro Colomar
Quoted 1 line
From
ff3fe9cdfd280fce97e9289e9a91fb6cc2c1c368Mon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:56:52 +0200
Subject: [PATCH 29/34] tsearch.3: Use sizeof consistentlyUse ``sizeof`` consistently through all the examples in the following way:
Quoted 7 lines
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memorySigned-off-by Alejandro Colomar <[email protected]>
Patch applied.
Cheers,
Michael
Quoted 18 lines
--- man3/tsearch.3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)man3/tsearch.3 +1
-1@@ -327,7 +327,7 @@ main(void) srand(time(NULL)); for (i = 0; i < 12; i++) { - ptr = xmalloc(sizeof(int)); + ptr = xmalloc(sizeof(*ptr)); *ptr = rand() & 0xff; val = tsearch((void *) ptr, &root, compare); if (val == NULL)
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
PATCH 30/34 aio.7: Use sizeof consistently
In reply to Alejandro ColomarFrom 33a09662331be9666c2f69dc60a792d9deee761e Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:57:28 +0200
Subject: [PATCH 30/34] aio.7: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man7/aio.7 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
man7/aio.7 +2 -2
@@ -311,11 +311,11 @@ main(int argc, char *argv[]) /* Allocate our arrays */ - ioList = calloc(numReqs, sizeof(struct ioRequest)); + ioList = calloc(numReqs, sizeof(*ioList)); if (ioList == NULL) errExit("calloc"); - aiocbList = calloc(numReqs, sizeof(struct aiocb)); + aiocbList = calloc(numReqs, sizeof(*aiocbList)); if (aiocbList == NULL) errExit("calloc");
Signature · 1 line
2.28.0
Re: [PATCH (2) 30/34] aio.7: Use sizeof consistently
Hi Alex,
Quoted 8 lines · Alejandro Colomar
Quoted 1 line
From
33a09662331be9666c2f69dc60a792d9deee761eMon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:57:28 +0200
Subject: [PATCH 30/34] aio.7: Use sizeof consistentlyUse ``sizeof`` consistently through all the examples in the following way:
Quoted 7 lines
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memorySigned-off-by Alejandro Colomar <[email protected]>
Thanks. Patch applied.
Cheers,
Michael
Quoted 22 lines
--- man7/aio.7 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)man7/aio.7 +2
-2@@ -311,11 +311,11 @@ main(int argc, char *argv[]) /* Allocate our arrays */ - ioList = calloc(numReqs, sizeof(struct ioRequest)); + ioList = calloc(numReqs, sizeof(*ioList)); if (ioList == NULL) errExit("calloc"); - aiocbList = calloc(numReqs, sizeof(struct aiocb)); + aiocbList = calloc(numReqs, sizeof(*aiocbList)); if (aiocbList == NULL) errExit("calloc");
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
PATCH 31/34 fanotify.7: Use sizeof consistently
In reply to Alejandro ColomarFrom 75af70465a8e60aa1d96b32d843b074966b7a878 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:58:14 +0200
Subject: [PATCH 31/33] fanotify.7: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man7/fanotify.7 | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
man7/fanotify.7 +1 -2
@@ -808,8 +808,7 @@ handle_events(int fd) response.fd = metadata\->fd; response.response = FAN_ALLOW; - write(fd, &response, - sizeof(struct fanotify_response)); + write(fd, &response, sizeof(response)); } /* Handle closing of writable file event */
Signature · 1 line
2.28.0
Re: [PATCH (2) 31/34] fanotify.7: Use sizeof consistently
Hello Alex,
Quoted 8 lines · Alejandro Colomar
Quoted 1 line
From
75af70465a8e60aa1d96b32d843b074966b7a878Mon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:58:14 +0200
Subject: [PATCH 31/33] fanotify.7: Use sizeof consistentlyUse ``sizeof`` consistently through all the examples in the following way:
Quoted 7 lines
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memorySigned-off-by Alejandro Colomar <[email protected]>
Thanks. Patch applied.
Cheers,
Michael
Quoted 19 lines
--- man7/fanotify.7 | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)man7/fanotify.7 +1
-2@@ -808,8 +808,7 @@ handle_events(int fd) response.fd = metadata\->fd; response.response = FAN_ALLOW; - write(fd, &response, - sizeof(struct fanotify_response)); + write(fd, &response, sizeof(response)); } /* Handle closing of writable file event */
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
PATCH 32/34 inotify.7: Use sizeof consistently
In reply to Alejandro ColomarFrom 464c2941b936df850f03d7d9df382dc2c46f37f1 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:58:59 +0200
Subject: [PATCH 32/34] inotify.7: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man7/inotify.7 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
man7/inotify.7 +1 -1
@@ -968,7 +968,7 @@ handle_events(int fd, int *wd, int argc, char* argv[]) /* Loop over all events in the buffer */ for (ptr = buf; ptr < buf + len; - ptr += sizeof(struct inotify_event) + event\->len) { + ptr += sizeof(*event) + event\->len) { event = (const struct inotify_event *) ptr;
Signature · 1 line
2.28.0
Re: [PATCH (2) 32/34] inotify.7: Use sizeof consistently
Hi Alex,
Quoted 8 lines · Alejandro Colomar
Quoted 1 line
From
464c2941b936df850f03d7d9df382dc2c46f37f1Mon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:58:59 +0200
Subject: [PATCH 32/34] inotify.7: Use sizeof consistentlyUse ``sizeof`` consistently through all the examples in the following way:
Quoted 7 lines
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memorySigned-off-by Alejandro Colomar <[email protected]>
I think this change would weaken the example. The point is that when traversing through these variable-length structures, the length of each structure is the size of the fixed-size component (struct inotify_event) plus the variable-length piece. The code as it stands emphasizes that. The patch would leave that point less clear. I won't apply this one.
Thanks,
Michael
Quoted 17 lines
--- man7/inotify.7 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)man7/inotify.7 +1
-1@@ -968,7 +968,7 @@ handle_events(int fd, int *wd, int argc, char* argv[]) /* Loop over all events in the buffer */ for (ptr = buf; ptr < buf + len; - ptr += sizeof(struct inotify_event) + event\->len) { + ptr += sizeof(*event) + event\->len) { event = (const struct inotify_event *) ptr;
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
PATCH 33/34 inotify.7: Use sizeof consistently
In reply to Alejandro ColomarFrom 0d4adf855466fe5c36e378eb704abafd45fc6417 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:59:11 +0200
Subject: [PATCH 33/34] inotify.7: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man7/inotify.7 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
man7/inotify.7 +1 -1
@@ -1031,7 +1031,7 @@ main(int argc, char* argv[]) /* Allocate memory for watch descriptors */ - wd = calloc(argc, sizeof(int)); + wd = calloc(argc, sizeof(*wd)); if (wd == NULL) { perror("calloc"); exit(EXIT_FAILURE);
Signature · 1 line
2.28.0
Re: [PATCH (2) 33/34] inotify.7: Use sizeof consistently
Hello Alex,
I noticed that you sometimes split out several patches to the same page. Thanks for doing that. As you realize sometimes, there might be differnt decisions for different patches to the same page.
Quoted 8 lines · Alejandro Colomar
Quoted 1 line
From
0d4adf855466fe5c36e378eb704abafd45fc6417Mon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:59:11 +0200
Subject: [PATCH 33/34] inotify.7: Use sizeof consistentlyUse ``sizeof`` consistently through all the examples in the following way:
Quoted 7 lines
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memorySigned-off-by Alejandro Colomar <[email protected]>
The above said, I'm also not inclined to apply this patch. Watch descriptors are by definition 'int', so I don't think this change improves readability. I think I won't apply this.
Cheers,
Michael
Quoted 18 lines
--- man7/inotify.7 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)man7/inotify.7 +1
-1@@ -1031,7 +1031,7 @@ main(int argc, char* argv[]) /* Allocate memory for watch descriptors */ - wd = calloc(argc, sizeof(int)); + wd = calloc(argc, sizeof(*wd)); if (wd == NULL) { perror("calloc"); exit(EXIT_FAILURE);
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
PATCH 34/34 unix.7: Use sizeof consistently
In reply to Alejandro ColomarFrom 63fa45f7c45c49df2eeab0735342de9304507e46 Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 22:24:12 +0200
Subject: [PATCH 34/34] unix.7: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man7/unix.7 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
man7/unix.7 +4 -4
@@ -948,7 +948,7 @@ main(int argc, char *argv[]) * the structure. */ - memset(&name, 0, sizeof(struct sockaddr_un)); + memset(&name, 0, sizeof(name)); /* Bind socket to socket name. */
@@ -956,7 +956,7 @@ main(int argc, char *argv[]) strncpy(name.sun_path, SOCKET_NAME, sizeof(name.sun_path) \- 1); ret = bind(connection_socket, (const struct sockaddr *) &name, - sizeof(struct sockaddr_un)); + sizeof(name)); if (ret == \-1) { perror("bind"); exit(EXIT_FAILURE);
@@ -1082,7 +1082,7 @@ main(int argc, char *argv[]) * the structure. */ - memset(&addr, 0, sizeof(struct sockaddr_un)); + memset(&addr, 0, sizeof(addr)); /* Connect socket to socket address */
@@ -1090,7 +1090,7 @@ main(int argc, char *argv[]) strncpy(addr.sun_path, SOCKET_NAME, sizeof(addr.sun_path) \- 1); ret = connect(data_socket, (const struct sockaddr *) &addr, - sizeof(struct sockaddr_un)); + sizeof(addr)); if (ret == \-1) { fprintf(stderr, "The server is down.\en"); exit(EXIT_FAILURE);
Signature · 1 line
2.28.0
Re: [PATCH (2) 34/34] unix.7: Use sizeof consistently
Hi Michael,
Sorry for the deep threading, I noticed it late and I tried to fix it in the last messages. Also there's some email where I forgot to remove "Re:" from the subject.
Well, that's all! I can see 2 or 3 patches where I have doubts, but I think most of them are straightforward.
Cheers,
Alex.
Re: [PATCH (2) 34/34] unix.7: Use sizeof consistently
Hi Alex,
Quoted 5 lines · Alejandro Colomar
Hi Michael,
Sorry for the deep threading, I noticed it late and I tried to fix it in the last messages. Also there's some email where I forgot to remove "Re:" from the subject.
Yes, the threading made things a little tricky, especially when it came to trying to review what I'd done. Did you not send with "git send-email"? Usually that threads things nicely (all patches after the first as replies to the first patch).
Quoted 2 lines
Well, that's all! I can see 2 or 3 patches where I have doubts, but I think most of them are straightforward.
So, I've still not processed patches 21, 22, and 29. And in review, I see that I am wondering about whether I should maintain 1, 5, 17, 18, and 19. These all involve the use of malloc() or similar.
The existing pattern was something like:
struct mytype *x; // Or some simple type such as 'int'
...
x = malloc(n * sizeof(struct mytpe));and your patches change it to:
struct mytype *x;
...
x = malloc(n * sizeof(*x));I'm not sure that always helps readability.
Part of the problem is the use of C90 in the code.
Do you both agree with me that both of the following c99 forms are better than the original:
struct mytype *x = malloc(n * sizeof(struct mytpe));
struct mytype *x = malloc(n * sizeof(*x));?
I *think* I mildly prefer the first form, but I'm open to arguments that the latter form is preferable. Of course, the fact that there might be more than one point where an 'alloc' is done and assigned to 'x' may influence the argument. Thus
struct mytype *x = malloc(n * sizeof(struct mytpe));
...
x = malloc(p * sizeof(struct mytype));vs
struct mytype *x = malloc(n * sizeof(*x));
...
x = malloc(p * sizeof(*x));Thanks,
Michael
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCH (2) 34/34] unix.7: Use sizeof consistently
Hi Michael,
Quoted 4 lines · Michael Kerrisk (man-pages)
Yes, the threading made things a little tricky, especially when it came to trying to review what I'd done. Did you not send with "git send-email"? Usually that threads things nicely (all patches after the first as replies to the first patch).
In this case, I didn't, because the conversation was already started, even the set of patches was already started, and I had to edit all of the subjects, and sometimes add introductory messages, and I felt more comfortable with the GUI.
Quoted 9 lines
So, I've still not processed patches 21, 22, and 29. And in review, I see that I am wondering about whether I should maintain 1, 5, 17, 18, and 19. These all involve the use of
malloc()or similar.The existing pattern was something like:
struct mytype *x; // Or some simple type such as 'int' ... x = malloc(n * sizeof(struct mytpe));
Not to forget malloc(sizeof(struct mytpe) * n);
Quoted 10 lines
and your patches change it to:
struct mytype *x; ... x = malloc(n * sizeof(*x));>I'm not sure that always helps readability.
Part of the problem is the use of C90 in the code.
Quoted 7 lines
Do you both agree with me that both of the following c99 forms are better than the original:
struct mytype *x = malloc(n * sizeof(struct mytpe)); struct mytype *x = malloc(n * sizeof(*x));?
Yes, I would say both of these are an improvement.
Quoted 11 lines
I *think* I mildly prefer the first form, but I'm open to arguments that the latter form is preferable. Of course, the fact that there might be more than one point where an 'alloc' is done and assigned to 'x' may influence the argument. Thus
struct mytype *x = malloc(n * sizeof(struct mytpe)); ... x = malloc(p * sizeof(struct mytype));
Quoted 5 lines
vs
struct mytype *x = malloc(n * sizeof(*x)); ... x = malloc(p * sizeof(*x));
In case there are 2 or more allocs, in general, I prefer the name of the variable.
In case there is only 1 alloc in the same line as the declaration, I still prefer the name of the variable: for consistency, and because some day you may add another alloc, and then separate the original declaration+alloc in two lines, and forget to fix sizeof to use the name of the variable.
The cases where I see the type much better are cases where it is impossible for the type to change (and if it ever changed it would be an accident and cause a deserved bug) such as in those cases where you really need an (u)int64_t because of the API.
There's also cases where in real code I would prefer the name of the variable (to avoid future bugs because of type change), but in the man pages it is clearer if you write the type to be more explicit and consistent. Example: queue.3 (PATCH 24/34): It's clearer if you consistently use the type across all the code (and it may be therefore better to use it in the man-pages), because the name of the variable looks like it's different from one alloc to the next, but I can imagine some real code implementing a TAILQ and later deciding to use a CIRCLEQ, and if any of the types in the allocation are not updated accordingly, there will appear bugs, while if the name of the node is used for allocating the memory, the transition will be really simple.
Regards,
Alex.
Re: [PATCH (2) 34/34] unix.7: Use sizeof consistently
Hello Alex,
Quoted 4 lines · Alejandro Colomar
Hi Michael,
On 9/5/20 10:27 AM, Michael Kerrisk (man-pages) wrote:
[...]
Quoted 11 lines
So, I've still not processed patches 21, 22, and 29. And in review,
Quoted 8 lines
I see that I am wondering about whether I should maintain 1, 5, 17, 18, and 19. These all involve the use of
malloc()or similar.The existing pattern was something like:
struct mytype *x; // Or some simple type such as 'int' ... x = malloc(n * sizeof(struct mytpe));Not to forget
malloc(sizeof(struct mytpe) * n);
<Cough> yes <cough>...
Quoted 29 lines
Quoted 9 lines
and your patches change it to:
struct mytype *x; ... x = malloc(n * sizeof(*x));>I'm not sure that always helps readability.
Part of the problem is the use of C90 in the code.
Quoted 7 lines
Do you both agree with me that both of the following c99 forms are better than the original:
struct mytype *x = malloc(n * sizeof(struct mytpe)); struct mytype *x = malloc(n * sizeof(*x));?
Yes, I would say both of these are an improvement.
Quoted 11 lines
I *think* I mildly prefer the first form, but I'm open to arguments that the latter form is preferable. Of course, the fact that there might be more than one point where an 'alloc' is done and assigned to 'x' may influence the argument. Thus
struct mytype *x = malloc(n * sizeof(struct mytpe)); ... x = malloc(p * sizeof(struct mytype));
Quoted 8 lines
Quoted 5 lines
vs
struct mytype *x = malloc(n * sizeof(*x)); ... x = malloc(p * sizeof(*x));In case there are 2 or more allocs, in general, I prefer the name of the variable.
Yes, by the time I'd written the two allocs examples, I'd started to lean that way too, but didn't say so because I wanted to hear your independent perspective .
Quoted 5 lines
In case there is only 1 alloc in the same line as the declaration, I still prefer the name of the variable: for consistency, and because some day you may add another alloc, and then separate the original declaration+alloc in two lines, and forget to fix sizeof to use the name of the variable.
Yes.
Quoted 4 lines
The cases where I see the type much better are cases where it is impossible for the type to change (and if it ever changed it would be an accident and cause a deserved bug) such as in those cases where you really need an (u)int64_t because of the API.
Yes.
Quoted 11 lines
There's also cases where in real code I would prefer the name of the variable (to avoid future bugs because of type change), but in the man pages it is clearer if you write the type to be more explicit and consistent. Example: queue.3 (PATCH 24/34): It's clearer if you consistently use the type across all the code (and it may be therefore better to use it in the man-pages), because the name of the variable looks like it's different from one alloc to the next, but I can imagine some real code implementing a TAILQ and later deciding to use a CIRCLEQ, and if any of the types in the allocation are not updated accordingly, there will appear bugs, while if the name of the node is used for allocating the memory, the transition will be really simple.
Agreed.
I've applied patches 21, 22, and 29. And then in line with our discussion above, I moved some pages to the style discussed above:
struct mytype *x = malloc(sizeof(*x));
See commit 48d05103071830b6708a3ecabeddcdef5f3daa44.
Thanks for your input Alex, it's been really helpful!
Cheers,
Michael
--
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCH (2) 34/34] unix.7: Use sizeof consistently
Hello Michael,
Quoted 1 line · Michael Kerrisk (man-pages)
See commit
48d05103071830b6708a3ecabeddcdef5f3daa44.
It looks good. Maybe I'd keep the cosmetic blank line after the declaration+alloc in insque.3...
Quoted 1 line
Thanks for your input Alex, it's been really helpful!
Thank you very much! I'm really happy to read that! I learnt a lot these days.
Cheers,
Alex.
Re: [PATCH (2) 34/34] unix.7: Use sizeof consistently
Hello Alex,
Quoted 7 lines · Alejandro Colomar
Hello Michael,
Quoted 1 line · Michael Kerrisk (man-pages)
See commit
48d05103071830b6708a3ecabeddcdef5f3daa44.It looks good. Maybe I'd keep the cosmetic blank line after the declaration+alloc in insque.3...
I kinda prefer not to have it.
Quoted 4 lines
Quoted 1 line
Thanks for your input Alex, it's been really helpful!
Thank you very much! I'm really happy to read that! I learnt a lot these days.
I learned a few things too :-).
Cheers,
Michael
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCH (2) 34/34] unix.7: Use sizeof consistently
In reply to Alejandro ColomarHello Alex,
man7/unix.7
Quoted 21 lines · Alejandro Colomar
Quoted 1 line
From
63fa45f7c45c49df2eeab0735342de9304507e46Mon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 22:24:12 +0200
Subject: [PATCH 34/34] unix.7: Use sizeof consistentlyUse ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memorySigned-off-by Alejandro Colomar <[email protected]>--- man7/unix.7 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)man7/unix.7 +0
-0
Thanks. Patch applied.
Cheers,
Michael
Quoted 39 lines
man7/unix.7 +4
-4@@ -948,7 +948,7 @@ main(int argc, char *argv[]) * the structure. */ - memset(&name, 0, sizeof(struct sockaddr_un)); + memset(&name, 0, sizeof(name)); /* Bind socket to socket name. */@@ -956,7 +956,7 @@ main(int argc, char *argv[]) strncpy(name.sun_path, SOCKET_NAME, sizeof(name.sun_path) \- 1); ret = bind(connection_socket, (const struct sockaddr *) &name, - sizeof(struct sockaddr_un)); + sizeof(name)); if (ret == \-1) { perror("bind"); exit(EXIT_FAILURE);@@ -1082,7 +1082,7 @@ main(int argc, char *argv[]) * the structure. */ - memset(&addr, 0, sizeof(struct sockaddr_un)); + memset(&addr, 0, sizeof(addr)); /* Connect socket to socket address */@@ -1090,7 +1090,7 @@ main(int argc, char *argv[]) strncpy(addr.sun_path, SOCKET_NAME, sizeof(addr.sun_path) \- 1); ret = connect(data_socket, (const struct sockaddr *) &addr, - sizeof(struct sockaddr_un)); + sizeof(addr)); if (ret == \-1) { fprintf(stderr, "The server is down.\en"); exit(EXIT_FAILURE);
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCH (2) 27/34] shm_open.3: Use sizeof consistently
In reply to Alejandro ColomarHello Alex,
Quoted 8 lines · Alejandro Colomar
Quoted 1 line
From
d3f439e697c9a384cad55e84a584493088eb4291Mon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:55:48 +0200
Subject: [PATCH 27/34] shm_open.3: Use sizeof consistentlyUse ``sizeof`` consistently through all the examples in the following way:
Quoted 7 lines
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memorySigned-off-by Alejandro Colomar <[email protected]>
Thanks. Patch applied.
Cheers,
Michael
Quoted 27 lines
--- man3/shm_open.3 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)man3/shm_open.3 +2
-2@@ -382,7 +382,7 @@ main(int argc, char *argv[]) /* Map the object into the caller\(aqs address space */ - struct shmbuf *shmp = mmap(NULL, sizeof(struct shmbuf), + struct shmbuf *shmp = mmap(NULL, sizeof(*shmp), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (shmp == MAP_FAILED)@@ -471,7 +471,7 @@ main(int argc, char *argv[]) if (fd == \-1) errExit("shm_open"); - struct shmbuf *shmp = mmap(NULL, sizeof(struct shmbuf), + struct shmbuf *shmp = mmap(NULL, sizeof(*shmp), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (shmp == MAP_FAILED)
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCH (2) 26/34] rtnetlink.3: Use sizeof consistently
In reply to Alejandro ColomarHello Alex,
Quoted 8 lines · Alejandro Colomar
Quoted 1 line
From
2b7a02916c24af8cbb0cbb071a7223fe48a52571Mon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:55:11 +0200
Subject: [PATCH 26/34] rtnetlink.3: Use sizeof consistentlyUse ``sizeof`` consistently through all the examples in the following way:
Quoted 7 lines
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memorySigned-off-by Alejandro Colomar <[email protected]>
Thanks. Patch applied.
Cheers,
Michael
Quoted 18 lines
--- man3/rtnetlink.3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)man3/rtnetlink.3 +1
-1@@ -114,7 +114,7 @@ req.if.ifi_change = 0xffffffff; /* ??? */ rta = (struct rtattr *)(((char *) &req) + NLMSG_ALIGN(req.nh.nlmsg_len)); rta\->rta_type = IFLA_MTU; -rta\->rta_len = RTA_LENGTH(sizeof(unsigned int)); +rta\->rta_len = RTA_LENGTH(sizeof(mtu)); req.nh.nlmsg_len = NLMSG_ALIGN(req.nh.nlmsg_len) + RTA_LENGTH(sizeof(mtu)); memcpy(RTA_DATA(rta), &mtu, sizeof(mtu));
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCH (2) 25/34] rtnetlink.3: Use sizeof consistently
In reply to Alejandro ColomarHello Alex,
man3/rtnetlink.3
Quoted 21 lines · Alejandro Colomar
Quoted 1 line
From
eca0c1500aa4db77c8c8d079798fe080e83e9935Mon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:55:00 +0200
Subject: [PATCH 25/34] rtnetlink.3: Use sizeof consistentlyUse ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memorySigned-off-by Alejandro Colomar <[email protected]>--- man3/rtnetlink.3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)man3/rtnetlink.3 +0
-0
Patch applied.
Thanks,
Michael
Quoted 12 lines
man3/rtnetlink.3 +1
-1@@ -105,7 +105,7 @@ unsigned int mtu = 1000; int rtnetlink_sk = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE); memset(&req, 0, sizeof(req)); -req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); +req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(req.if)); req.nh.nlmsg_flags = NLM_F_REQUEST; req.nh.nlmsg_type = RTM_NEWLINK; req.if.ifi_family = AF_UNSPEC;
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCH (2) 24/34] queue.3: Use sizeof consistently
In reply to Alejandro ColomarHello Alex,
Quoted 8 lines · Alejandro Colomar
Quoted 1 line
From
6f3e17cf38bc3718332c7151899e253dbc91affbMon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:53:51 +0200
Subject: [PATCH 24/34] queue.3: Use sizeof consistentlyUse ``sizeof`` consistently through all the examples in the following way:
Quoted 7 lines
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memorySigned-off-by Alejandro Colomar <[email protected]>
I don't think this change improves readability. Looking at the code as it is, one can immediately see that it is the same structure being allocated everywhere. After the change, that's not so obvious. I think I won't apply this.
Cheers,
Michael
Quoted 98 lines
--- man3/queue.3 | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-)man3/queue.3 +16
-16@@ -559,10 +559,10 @@ struct entry { SLIST_INIT(&head); /* Initialize the list. */ -n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ +n1 = malloc(sizeof(*n1)); /* Insert at the head. */ SLIST_INSERT_HEAD(&head, n1, entries); -n2 = malloc(sizeof(struct entry)); /* Insert after. */ +n2 = malloc(sizeof(*n2)); /* Insert after. */ SLIST_INSERT_AFTER(n1, n2, entries); SLIST_REMOVE(&head, n2, entry, entries);/* Deletion. */@@ -775,13 +775,13 @@ struct entry { STAILQ_INIT(&head); /* Initialize the queue. */ -n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ +n1 = malloc(sizeof(*n1)); /* Insert at the head. */ STAILQ_INSERT_HEAD(&head, n1, entries); -n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */ +n1 = malloc(sizeof(*n1)); /* Insert at the tail. */ STAILQ_INSERT_TAIL(&head, n1, entries); -n2 = malloc(sizeof(struct entry)); /* Insert after. */ +n2 = malloc(sizeof(*n2)); /* Insert after. */ STAILQ_INSERT_AFTER(&head, n1, n2, entries); /* Deletion. */ STAILQ_REMOVE(&head, n2, entry, entries);@@ -975,13 +975,13 @@ struct entry { LIST_INIT(&head); /* Initialize the list. */ -n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ +n1 = malloc(sizeof(*n1)); /* Insert at the head. */ LIST_INSERT_HEAD(&head, n1, entries); -n2 = malloc(sizeof(struct entry)); /* Insert after. */ +n2 = malloc(sizeof(*n2)); /* Insert after. */ LIST_INSERT_AFTER(n1, n2, entries); -n3 = malloc(sizeof(struct entry)); /* Insert before. */ +n3 = malloc(sizeof(*n3)); /* Insert before. */ LIST_INSERT_BEFORE(n2, n3, entries); LIST_REMOVE(n2, entries); /* Deletion. */@@ -1233,16 +1233,16 @@ struct entry { TAILQ_INIT(&head); /* Initialize the queue. */ -n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ +n1 = malloc(sizeof(*n1)); /* Insert at the head. */ TAILQ_INSERT_HEAD(&head, n1, entries); -n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */ +n1 = malloc(sizeof(*n1)); /* Insert at the tail. */ TAILQ_INSERT_TAIL(&head, n1, entries); -n2 = malloc(sizeof(struct entry)); /* Insert after. */ +n2 = malloc(sizeof(*n2)); /* Insert after. */ TAILQ_INSERT_AFTER(&head, n1, n2, entries); -n3 = malloc(sizeof(struct entry)); /* Insert before. */ +n3 = malloc(sizeof(*n3)); /* Insert before. */ TAILQ_INSERT_BEFORE(n2, n3, entries); TAILQ_REMOVE(&head, n2, entries); /* Deletion. */@@ -1426,16 +1426,16 @@ struct entry { CIRCLEQ_INIT(&head); /* Initialize the queue. */ -n1 = malloc(sizeof(struct entry)); /* Insert at the head. */ +n1 = malloc(sizeof(*n1)); /* Insert at the head. */ CIRCLEQ_INSERT_HEAD(&head, n1, entries); -n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */ +n1 = malloc(sizeof(*n1)); /* Insert at the tail. */ CIRCLEQ_INSERT_TAIL(&head, n1, entries); -n2 = malloc(sizeof(struct entry)); /* Insert after. */ +n2 = malloc(sizeof(*n2)); /* Insert after. */ CIRCLEQ_INSERT_AFTER(&head, n1, n2, entries); -n3 = malloc(sizeof(struct entry)); /* Insert before. */ +n3 = malloc(sizeof(*n3)); /* Insert before. */ CIRCLEQ_INSERT_BEFORE(&head, n2, n3, entries); CIRCLEQ_REMOVE(&head, n2, entries); /* Deletion. */
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCH (2) 23/34] pthread_setaffinity_np.3: Use sizeof consistently
In reply to Alejandro ColomarHello Alex,
Quoted 5 lines · Alejandro Colomar
Quoted 1 line
From
ba0d73749a85e835aa43a4134a1b6906062943f3Mon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:53:12 +0200
Subject: [PATCH 23/34] pthread_setaffinity_np.3: Use sizeof consistently
Quoted 9 lines
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
Thanks. Patch applied.
Cheers,
Michael
Quoted 25 lines
Signed-off-by Alejandro Colomar <[email protected]>--- man3/pthread_setaffinity_np.3 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)man3/pthread_setaffinity_np.3 +2
-2@@ -194,13 +194,13 @@ main(int argc, char *argv[]) for (j = 0; j < 8; j++) CPU_SET(j, &cpuset); - s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset); + s = pthread_setaffinity_np(thread, sizeof(cpuset), &cpuset); if (s != 0) handle_error_en(s, "pthread_setaffinity_np"); /* Check the actual affinity mask assigned to the thread */ - s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset); + s = pthread_getaffinity_np(thread, sizeof(cpuset), &cpuset); if (s != 0) handle_error_en(s, "pthread_getaffinity_np");
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCH (2) 22/34] pthread_create.3: Use sizeof consistently
In reply to Alejandro ColomarHello Alex,
Quoted 5 lines · Alejandro Colomar
Quoted 1 line
From
03783d811fad4783b394c01a4ac68ca6d92b6c58Mon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:52:22 +0200
Subject: [PATCH 22/34] pthread_create.3: Use sizeof consistently
Quoted 8 lines
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
Patch applied.
Cheers,
Michael
Quoted 18 lines
Signed-off-by Alejandro Colomar <[email protected]>--- man3/pthread_create.3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)man3/pthread_create.3 +1
-1@@ -361,7 +361,7 @@ main(int argc, char *argv[]) /* Allocate memory for pthread_create() arguments */ - tinfo = calloc(num_threads, sizeof(struct thread_info)); + tinfo = calloc(num_threads, sizeof(*tinfo)); if (tinfo == NULL) handle_error("calloc");
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCH (2) 21/34] mbstowcs.3: Use sizeof consistently
In reply to Alejandro ColomarHello Alex,
Quoted 5 lines · Alejandro Colomar
Quoted 1 line
From
1b70e7a1da093e4a8e3be79aaed623b21c10e763Mon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:51:07 +0200
Subject: [PATCH 21/34] mbstowcs.3: Use sizeof consistently
Quoted 8 lines
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
Patch applied.
Cheers,
Michael
Quoted 20 lines
Signed-off-by Alejandro Colomar <[email protected]>--- man3/mbstowcs.3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)man3/mbstowcs.3 +1
-1@@ -186,7 +186,7 @@ main(int argc, char *argv[]) /* Allocate wide character string of the desired size. Add 1 to allow for terminating null wide character (L\(aq\e0\(aq). */ - wcs = calloc(mbslen + 1, sizeof(wchar_t)); + wcs = calloc(mbslen + 1, sizeof(*wcs)); if (wcs == NULL) { perror("calloc"); exit(EXIT_FAILURE);
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCH (2) 20/34] mbsinit.3: Use sizeof consistently
In reply to Alejandro ColomarHello Alex,
Quoted 5 lines · Alejandro Colomar
Quoted 1 line
From
77587b9c0b58cde232ea3bfaeef5f50607d83b5eMon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:50:10 +0200
Subject: [PATCH 20/34] mbsinit.3: Use sizeof consistently
Quoted 8 lines
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
Thanks. Patch applied.
Cheers,
Michael
Quoted 19 lines
Signed-off-by Alejandro Colomar <[email protected]>--- man3/mbsinit.3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)man3/mbsinit.3 +1
-1@@ -59,7 +59,7 @@ in initial state is to set it to zero: .in +4n .EX mbstate_t state; -memset(&state, 0, sizeof(mbstate_t)); +memset(&state, 0, sizeof(state)); .EE .in .PP
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCH (2) 19/34] malloc_info.3: Use sizeof consistently
In reply to Alejandro ColomarHello Alex,
Quoted 8 lines · Alejandro Colomar
Quoted 1 line
From
bf1a7799161fe1f08570c4f25d87a0f4e0b51ef1Mon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:49:17 +0200
Subject: [PATCH 19/34] malloc_info.3: Use sizeof consistentlyUse ``sizeof`` consistently through all the examples in the following way:
Quoted 7 lines
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memorySigned-off-by Alejandro Colomar <[email protected]>
Thanks. Patch applied.
Cheers,
Michael
Quoted 17 lines
--- man3/malloc_info.3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)man3/malloc_info.3 +1
-1@@ -226,7 +226,7 @@ main(int argc, char *argv[]) blockSize = atoi(argv[3]); sleepTime = (argc > 4) ? atoi(argv[4]) : 0; - thr = calloc(numThreads, sizeof(pthread_t)); + thr = calloc(numThreads, sizeof(*thr)); if (thr == NULL) errExit("calloc");
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCH (2) 18/34] insque.3: Use sizeof consistently
In reply to Alejandro ColomarHello Alex,
Quoted 8 lines · Alejandro Colomar
Quoted 1 line
From
b43c812d1e129a8d2b34df7624a78baa3bf1273eMon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:47:45 +0200
Subject: [PATCH 18/34] insque.3: Use sizeof consistentlyUse ``sizeof`` consistently through all the examples in the following way:
Quoted 7 lines
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memorySigned-off-by Alejandro Colomar <[email protected]>
Thanks. Patch applied.
Cheers,
Michael
Quoted 18 lines
--- man3/insque.3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)man3/insque.3 +1
-1@@ -182,7 +182,7 @@ new_element(void) { struct element *e; - e = malloc(sizeof(struct element)); + e = malloc(sizeof(*e)); if (e == NULL) { fprintf(stderr, "malloc() failed\en"); exit(EXIT_FAILURE);
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCH (2) 17/34] getgrouplist.3: Use sizeof consistently
In reply to Alejandro ColomarHello Alex,
Quoted 8 lines · Alejandro Colomar
Quoted 1 line
From
7ca60fc88b831818c1f1722919af220a646761abMon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:47:15 +0200
Subject: [PATCH 17/34] getgrouplist.3: Use sizeof consistentlyUse ``sizeof`` consistently through all the examples in the following way:
Quoted 7 lines
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memorySigned-off-by Alejandro Colomar <[email protected]>
Thanks. Applied.
Cheers,
Michael
Quoted 18 lines
--- man3/getgrouplist.3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)man3/getgrouplist.3 +1
-1@@ -164,7 +164,7 @@ main(int argc, char *argv[]) ngroups = atoi(argv[2]); - groups = malloc(sizeof(gid_t) * ngroups); + groups = malloc(sizeof(*groups) * ngroups); if (groups == NULL) { perror("malloc"); exit(EXIT_FAILURE);
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCH (2) 16/34] getaddrinfo.3: Use sizeof consistently
In reply to Alejandro ColomarHello Alex,
Quoted 8 lines · Alejandro Colomar
Quoted 1 line
From
f926f02eede41183aed7ae279eb333273bd0c8dcMon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:46:35 +0200
Subject: [PATCH 16/34] getaddrinfo.3: Use sizeof consistentlyUse ``sizeof`` consistently through all the examples in the following way:
Quoted 7 lines
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memorySigned-off-by Alejandro Colomar <[email protected]>
Patch applied. Thanks!
Cheers,
Michael
Quoted 18 lines
--- man3/getaddrinfo.3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)man3/getaddrinfo.3 +1
-1@@ -721,7 +721,7 @@ main(int argc, char *argv[]) /* Read datagrams and echo them back to sender */ for (;;) { - peer_addr_len = sizeof(struct sockaddr_storage); + peer_addr_len = sizeof(peer_addr); nread = recvfrom(sfd, buf, BUF_SIZE, 0, (struct sockaddr *) &peer_addr, &peer_addr_len); if (nread == \-1)
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCH (2) 15/34] getaddrinfo.3: Use sizeof consistently
In reply to Alejandro ColomarHi Alex,
Quoted 8 lines · Alejandro Colomar
Quoted 1 line
From
eef612c1f5f039421bd0fa167e1972e98d934bceMon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:46:06 +0200
Subject: [PATCH 15/34] getaddrinfo.3: Use sizeof consistentlyUse ``sizeof`` consistently through all the examples in the following way:
Quoted 7 lines
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memorySigned-off-by Alejandro Colomar <[email protected]>
Patch applied.
Cheers,
Michael
Quoted 27 lines
--- man3/getaddrinfo.3 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)man3/getaddrinfo.3 +2
-2@@ -679,7 +679,7 @@ main(int argc, char *argv[]) exit(EXIT_FAILURE); } - memset(&hints, 0, sizeof(struct addrinfo)); + memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */ hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */@@ -775,7 +775,7 @@ main(int argc, char *argv[]) /* Obtain address(es) matching host/port */ - memset(&hints, 0, sizeof(struct addrinfo)); + memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */ hints.ai_flags = 0;
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
PATCH 15/34 getaddrinfo.3: Use sizeof consistently
In reply to Alejandro ColomarFrom eef612c1f5f039421bd0fa167e1972e98d934bce Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:46:06 +0200
Subject: [PATCH 15/34] getaddrinfo.3: Use sizeof consistently
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man3/getaddrinfo.3 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
man3/getaddrinfo.3 +2 -2
@@ -679,7 +679,7 @@ main(int argc, char *argv[]) exit(EXIT_FAILURE); } - memset(&hints, 0, sizeof(struct addrinfo)); + memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */ hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
@@ -775,7 +775,7 @@ main(int argc, char *argv[]) /* Obtain address(es) matching host/port */ - memset(&hints, 0, sizeof(struct addrinfo)); + memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */ hints.ai_flags = 0;
Signature · 1 line
2.28.0
Re: [PATCH (2) 14/34] cmsg.3: Use sizeof consistently
In reply to Alejandro ColomarHi Alex,
Quoted 24 lines · Alejandro Colomar
Quoted 1 line
From
374b31bee6762314ab48988e2e78a3a6a2f96834Mon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:44:54 +0200
Subject: [PATCH 14/34] cmsg.3: Use sizeof consistentlyUse ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memorySigned-off-by Alejandro Colomar <[email protected]>--- man3/cmsg.3 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)man3/cmsg.3 +0
-0
man3/cmsg.3 msg.msg_controllen = sizeof(u.buf); · line 248
Quoted 10 lines
man3/cmsg.3 +2
-2@@ -241,8 +241,8 @@ msg.msg_controllen = sizeof(u.buf); 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, sizeof(int) * NUM_FD); +cmsg\->cmsg_len = CMSG_LEN(sizeof(myfds)); +memcpy(CMSG_DATA(cmsg), myfds, sizeof(myfds)); .EE .in .SH SEE ALSO
Thanks. Patch applied.
Cheers,
Michael
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCH (2) 13/34] cmsg.3: Use sizeof consistently
In reply to Alejandro ColomarQuoted 5 lines · Alejandro Colomar
Quoted 1 line
From
ba70b1e8addad4ef9f2a490d2069b112d09ba9f1Mon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:44:41 +0200
Subject: [PATCH 13/34] cmsg.3: Use sizeof consistently
Quoted 8 lines
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
Patch applied. Thanks.
Cheers,
Michael
Quoted 19 lines
Signed-off-by Alejandro Colomar <[email protected]>--- man3/cmsg.3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)man3/cmsg.3 +1
-1@@ -203,7 +203,7 @@ for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL; cmsg = CMSG_NXTHDR(&msgh, cmsg)) { if (cmsg\->cmsg_level == IPPROTO_IP && cmsg\->cmsg_type == IP_TTL) { - memcpy(&receive_ttl, CMSG_DATA(cmsg), sizeof(int)); + memcpy(&receive_ttl, CMSG_DATA(cmsg), sizeof(received_ttl)); break; } }
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCH (2) 12/34] bsearch.3: Use sizeof consistently
In reply to Alejandro ColomarHello Alex,
Quoted 8 lines · Alejandro Colomar
Quoted 1 line
From
886db3bad74f35fc40a1238a0d2f35ace3dc7620Mon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:43:23 +0200
Subject: [PATCH 12/34] bsearch.3: Use sizeof consistentlyUse ``sizeof`` consistently through all the examples in the following way:
Quoted 7 lines
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memorySigned-off-by Alejandro Colomar <[email protected]>
Patch applied. Thanks!
Cheers,
Michael
Quoted 24 lines
--- man3/bsearch.3 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)man3/bsearch.3 +2
-2@@ -124,12 +124,12 @@ main(int argc, char **argv) { int i; - qsort(months, nr_of_months, sizeof(struct mi), compmi); + qsort(months, nr_of_months, sizeof(months[0]), compmi); for (i = 1; i < argc; i++) { struct mi key, *res; key.name = argv[i]; res = bsearch(&key, months, nr_of_months, - sizeof(struct mi), compmi); + sizeof(months[0]), compmi); if (res == NULL) printf("\(aq%s\(aq: unknown month\en", argv[i]); else
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Michael Kerrisk (man-pages)
AppliedRe: [PATCH (2) 11/34] timerfd_create.2: Use sizeof consistently
In reply to Alejandro ColomarHello Alex,
Quoted 8 lines · Alejandro Colomar
Quoted 1 line
From
d347c933a8c253028f8f76c4170b65b85ce7d605Mon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:42:28 +0200
Subject: [PATCH 11/34] timerfd_create.2: Use sizeof consistentlyUse ``sizeof`` consistently through all the examples in the following way:
Quoted 7 lines
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memorySigned-off-by Alejandro Colomar <[email protected]>
Applied. Thanks!
Michael
Quoted 20 lines
--- man2/timerfd_create.2 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)man2/timerfd_create.2 +2
-2@@ -700,8 +700,8 @@ main(int argc, char *argv[]) printf("timer started\en"); for (tot_exp = 0; tot_exp < max_exp;) { - s = read(fd, &exp, sizeof(uint64_t)); - if (s != sizeof(uint64_t)) + s = read(fd, &exp, sizeof(exp)); + if (s != sizeof(exp)) handle_error("read"); tot_exp += exp;
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCH (2) 11/34] timerfd_create.2: Use sizeof consistently
Quoted 14 lines · Michael Kerrisk (man-pages)
Hello Alex,
Quoted 11 lines · Alejandro Colomar
Quoted 1 line
From
d347c933a8c253028f8f76c4170b65b85ce7d605Mon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:42:28 +0200
Subject: [PATCH 11/34] timerfd_create.2: Use sizeof consistentlyUse ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Quoted 8 lines
Quoted 4 lines
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memorySigned-off-by Alejandro Colomar <[email protected]>Applied. Thanks!
Michael
I've changed my mind on this one (for similar reasons to patch 03/34). The buffer here must be an 8-byte integer; it's baked into the API, and I think it's helpful to show the type, to emphasize this point. I've decided not to apply this patch.
Thanks,
Michael
Quoted 22 lines
Quoted 20 lines
--- man2/timerfd_create.2 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)man2/timerfd_create.2 +2
-2@@ -700,8 +700,8 @@ main(int argc, char *argv[]) printf("timer started\en"); for (tot_exp = 0; tot_exp < max_exp;) { - s = read(fd, &exp, sizeof(uint64_t)); - if (s != sizeof(uint64_t)) + s = read(fd, &exp, sizeof(exp)); + if (s != sizeof(exp)) handle_error("read"); tot_exp += exp;
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCH (2) 10/34] signalfd.2: Use sizeof consistently
In reply to Alejandro ColomarHello Alex,
Quoted 8 lines · Alejandro Colomar
Quoted 1 line
From
9a10e681849a57f75d89cf6943901a3df8097399Mon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:41:31 +0200
Subject: [PATCH 10/34] signalfd.2: Use sizeof consistentlyUse ``sizeof`` consistently through all the examples in the following way:
Quoted 7 lines
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memorySigned-off-by Alejandro Colomar <[email protected]>
Patch applied. Thanks.
Michael
Quoted 20 lines
--- man2/signalfd.2 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)man2/signalfd.2 +2
-2@@ -502,8 +502,8 @@ main(int argc, char *argv[]) handle_error("signalfd"); for (;;) { - s = read(sfd, &fdsi, sizeof(struct signalfd_siginfo)); - if (s != sizeof(struct signalfd_siginfo)) + s = read(sfd, &fdsi, sizeof(fdsi)); + if (s != sizeof(fdsi)) handle_error("read"); if (fdsi.ssi_signo == SIGINT) {
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCH (2) 09/34] sysctl.2: Use sizeof consistently
In reply to Alejandro ColomarHi Alex,
Quoted 8 lines · Alejandro Colomar
Quoted 1 line
From
90572d59a4f3b1986e57c0b32e14e4e68ecab716Mon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:39:56 +0200
Subject: [PATCH 09/34] sysctl.2: Use sizeof consistentlyUse ``sizeof`` consistently through all the examples in the following way:
Quoted 7 lines
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memorySigned-off-by Alejandro Colomar <[email protected]>
Patch applied. Thanks!
Cheers,
Michael
Quoted 18 lines
--- man2/sysctl.2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)man2/sysctl.2 +1
-1@@ -154,7 +154,7 @@ main(void) size_t osnamelth; int name[] = { CTL_KERN, KERN_OSTYPE }; - memset(&args, 0, sizeof(struct __sysctl_args)); + memset(&args, 0, sizeof(args)); args.name = name; args.nlen = sizeof(name)/sizeof(name[0]); args.oldval = osname;
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCH (2) 08/34] poll.2: Use sizeof consistently
In reply to Alejandro ColomarHi Alex,
Quoted 5 lines · Alejandro Colomar
Quoted 1 line
From
791b2082c91de4fb49a1a46e9c11d294aac09050Mon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:34:52 +0200
Subject: [PATCH 08/34] poll.2: Use sizeof consistently
Quoted 8 lines
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
This is another of those cases where I am inclined not to apply the patch. It feels to me like this reduces readability somewhat, since the very point is to allocate an array of pollfd structs. So, I think I won't apply this one.
Thanks,
Michael
Quoted 18 lines
Signed-off-by Alejandro Colomar <[email protected]>--- man2/poll.2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)man2/poll.2 +1
-1@@ -596,7 +596,7 @@ main(int argc, char *argv[]) } num_open_fds = nfds = argc \- 1; - pfds = calloc(nfds, sizeof(struct pollfd)); + pfds = calloc(nfds, sizeof(*pfds)); if (pfds == NULL) errExit("malloc");
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCH (2) 06/34] perf_event_open.2: Use sizeof consistently
In reply to Alejandro ColomarHello Alex,
Quoted 5 lines · Alejandro Colomar
Quoted 1 line
From
dbb7b520bd4314488122835c87f45b685ce45b28Mon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:29:02 +0200
Subject: [PATCH 06/34] perf_event_open.2: Use sizeof consistently
Quoted 8 lines
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
Patch applied.
Cheers,
Michael
Quoted 22 lines
Signed-off-by Alejandro Colomar <[email protected]>--- man2/perf_event_open.2 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)man2/perf_event_open.2 +2
-2@@ -3419,9 +3419,9 @@ main(int argc, char **argv) long long count; int fd; - memset(&pe, 0, sizeof(struct perf_event_attr)); + memset(&pe, 0, sizeof(pe)); pe.type = PERF_TYPE_HARDWARE; - pe.size = sizeof(struct perf_event_attr); + pe.size = sizeof(pe); pe.config = PERF_COUNT_HW_INSTRUCTIONS; pe.disabled = 1; pe.exclude_kernel = 1;
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCH (2) 05/34] open_by_handle_at.2: Use sizeof consistently
In reply to Alejandro ColomarHello Alex,
Quoted 5 lines · Alejandro Colomar
Quoted 1 line
From
0176a7981aead1a202d5d0295b074e165b0d39ddMon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:27:55 +0200
Subject: [PATCH 05/34] open_by_handle_at.2: Use sizeof consistently
Quoted 9 lines
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
Well the page was already quite inconsistent, wasn't it, since "sizeof(*fhp)" was already used elsewhere in the example!
Patch applied.
Thanks,
Michael
Quoted 27 lines
Signed-off-by Alejandro Colomar <[email protected]>--- man2/open_by_handle_at.2 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)man2/open_by_handle_at.2 +2
-2@@ -586,7 +586,7 @@ main(int argc, char *argv[]) /* Reallocate file_handle structure with correct size */ - fhsize = sizeof(struct file_handle) + fhp\->handle_bytes; + fhsize = sizeof(*fhp) + fhp\->handle_bytes; fhp = realloc(fhp, fhsize); /* Copies fhp\->handle_bytes */ if (fhp == NULL) errExit("realloc");@@ -707,7 +707,7 @@ main(int argc, char *argv[]) /* Given handle_bytes, we can now allocate file_handle structure */ - fhp = malloc(sizeof(struct file_handle) + handle_bytes); + fhp = malloc(sizeof(*fhp) + handle_bytes); if (fhp == NULL) errExit("malloc");
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCH (2) 04/34] futex.2: Use sizeof consistently
In reply to Alejandro ColomarHello Alex,
Quoted 8 lines · Alejandro Colomar
Quoted 1 line
From
21cd85c6f11390c71ed4475f1f9d55910891cf23Mon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:27:01 +0200
Subject: [PATCH 04/34] futex.2: Use sizeof consistentlyUse ``sizeof`` consistently through all the examples in the following way:
Quoted 7 lines
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memorySigned-off-by Alejandro Colomar <[email protected]>
Patch applied.
Cheers,
Michael
Quoted 18 lines
--- man2/futex.2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)man2/futex.2 +1
-1@@ -1839,7 +1839,7 @@ main(int argc, char *argv[]) subsequently use the "shared" futex operations (i.e., not the ones suffixed "_PRIVATE") */ - iaddr = mmap(NULL, sizeof(int) * 2, PROT_READ | PROT_WRITE, + iaddr = mmap(NULL, sizeof(*iaddr) * 2, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, \-1, 0); if (iaddr == MAP_FAILED) errExit("mmap");
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCH (2) 02/34] bind.2: Use sizeof consistently
In reply to Alejandro ColomarHello Alex,
Quoted 21 lines · Alejandro Colomar
Hello Michael,
Quoted 3 lines · Michael Kerrisk (man-pages)
I must admit that I don't care too much either way on this. That is to say, I'm not sure one way is any clearer than the other. However, I have applied the patch.
There are places where I wouldn't say there are any readability benefits. However, there are functions such as
malloc()ormemset(), where using the type could lead to future bugs, so IMHO it's better to just be consistent and use always the name, unless there are clear readability problems (or other problems).In the end, someone thought it to be important enough to write it in the kernel coding style.
I don't expect all of these patches to be applied, as I had doubts when writing some of them, but we can discuss those where it is better to keep the type.
Quoted 1 line
Quoted 5 lines
Quoted 4 lines
In passing, I note that there is a clarity issue that I do find more significant though: the repeated calculations in the
malloc()andprintf()calls. So I changed that:
Patch applied.
Cheers,
Michael
Quoted 51 lines
------------------------------------------------------------------------
Quoted 1 line
From
54016160b603454fbe4f38d6a81886a03fe2ffdfMon Sep 17 00:00:00 2001From: Alejandro Colomar <[email protected]>
Date: Thu, 3 Sep 2020 21:24:43 +0200
Subject: [PATCH 02/34] bind.2: Use sizeof consistentlyUse ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memorySigned-off-by Alejandro Colomar <[email protected]>--- man2/bind.2 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)man2/bind.2 +3
-3@@ -293,14 +293,14 @@ main(int argc, char *argv[]) if (sfd == \-1) handle_error("socket"); - memset(&my_addr, 0, sizeof(struct sockaddr_un)); + memset(&my_addr, 0, sizeof(my_addr)); /* Clear structure */ my_addr.sun_family = AF_UNIX; strncpy(my_addr.sun_path, MY_SOCK_PATH, sizeof(my_addr.sun_path) \- 1); if (bind(sfd, (struct sockaddr *) &my_addr, - sizeof(struct sockaddr_un)) == \-1) + sizeof(my_addr)) == \-1) handle_error("bind"); if (listen(sfd, LISTEN_BACKLOG) == \-1)@@ -309,7 +309,7 @@ main(int argc, char *argv[]) /* Now we can accept incoming connections one at a time using accept(2) */ - peer_addr_size = sizeof(struct sockaddr_un); + peer_addr_size = sizeof(peer_addr); cfd = accept(sfd, (struct sockaddr *) &peer_addr, &peer_addr_size); if (cfd == \-1)
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Hi Jakub,
Once again, thanks for jumping in...
Quoted 17 lines · Jakub Wilk
* Michael Kerrisk <[email protected]>, 2020-08-25, 12:29:
Quoted 6 lines
Quoted 4 lines
``(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)); | ^
Quoted 5 lines
Quoted 2 lines
sizeof((sizeof(x) * INT_MAX * 2))== 8sizeof(INT_MAX * 2 * sizeof(x))== 8Hmm? If there was no overflow, surely you should get a number larger than
INT_MAX...
Yes, I goofed :-(. There is no compiler warning for "sizeof(INT_MAX * 2 * sizeof(x))", which is what I tested, but as you point out, there is a warning for "INT_MAX * 2 * sizeof(x)".
Thanks,
Michael
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
PATCH 35/35 qsort.3: Use sizeof consistently
In reply to Alejandro ColomarUse ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
--- man3/qsort.3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
man3/qsort.3 +1 -1
@@ -150,7 +150,7 @@ main(int argc, char *argv[]) exit(EXIT_FAILURE); } - qsort(&argv[1], argc \- 1, sizeof(char *), cmpstringp); + qsort(&argv[1], argc \- 1, sizeof(argv[0]), cmpstringp); for (j = 1; j < argc; j++) puts(argv[j]);
Signature · 1 line
2.28.0
Re: [PATCH 35/35] qsort.3: Use sizeof consistently
Hi Michael,
This one I sent it with git. It is *really* *really* simple :)
Cheers,
Alex
Quoted 28 lines · Alejandro Colomar
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memorySigned-off-by Alejandro Colomar <[email protected]>--- man3/qsort.3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)man3/qsort.3 +1
-1@@ -150,7 +150,7 @@ main(int argc, char *argv[]) exit(EXIT_FAILURE); } - qsort(&argv[1], argc \- 1, sizeof(char *), cmpstringp); + qsort(&argv[1], argc \- 1, sizeof(argv[0]), cmpstringp); for (j = 1; j < argc; j++) puts(argv[j]);
Re: [PATCH 35/35] qsort.3: Use sizeof consistently
In reply to Alejandro ColomarQuoted 10 lines · Alejandro Colomar
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memorySigned-off-by Alejandro Colomar <[email protected]>
I'm not convinced about this one. Do you really think it improves readability? I kinda feel that it does not.
Thanks,
Michael
Quoted 18 lines
--- man3/qsort.3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)man3/qsort.3 +1
-1@@ -150,7 +150,7 @@ main(int argc, char *argv[]) exit(EXIT_FAILURE); } - qsort(&argv[1], argc \- 1, sizeof(char *), cmpstringp); + qsort(&argv[1], argc \- 1, sizeof(argv[0]), cmpstringp); for (j = 1; j < argc; j++) puts(argv[j]);
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/
Re: [PATCH 35/35] qsort.3: Use sizeof consistently
Quoted 8 lines · Alejandro Colomar
Quoted 8 lines
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
Quoted 5 lines
Quoted 2 lines
Signed-off-by Alejandro Colomar <[email protected]>I'm not convinced about this one. Do you really think it improves readability? I kinda feel that it does not.
Well, I have two different ideas for this one.
In terms of readability, char * might seem clearer. In terms of preventing bugs, argv[0] seems safer.
For real code I would use the latter. For the man, I have doubts.
Follow your intuition :)
Quoted 25 lines
Thanks,
Michael
Quoted 18 lines
--- man3/qsort.3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)man3/qsort.3 +1
-1@@ -150,7 +150,7 @@ main(int argc, char *argv[]) exit(EXIT_FAILURE); } - qsort(&argv[1], argc \- 1, sizeof(char *), cmpstringp); + qsort(&argv[1], argc \- 1, sizeof(argv[0]), cmpstringp); for (j = 1; j < argc; j++) puts(argv[j]);
Re: [PATCH 35/35] qsort.3: Use sizeof consistently
Quoted 17 lines · Alejandro Colomar
Quoted 8 lines · Alejandro Colomar
Quoted 8 lines
Use ``sizeof`` consistently through all the examples in the following way:
- Use the name of the variable instead of its type as argument for ``sizeof``.
Rationale:
https://www.kernel.org/doc/html/v5.8/process/coding-style.html#allocating-memory
Quoted 5 lines
Quoted 2 lines
Signed-off-by Alejandro Colomar <[email protected]>I'm not convinced about this one. Do you really think it improves readability? I kinda feel that it does not.
Quoted 6 lines
Well, I have two different ideas for this one.
In terms of readability,char *might seem clearer. In terms of preventing bugs,argv[0]seems safer.
For real code I would use the latter. For the man, I have doubts.Follow your intuition :)
Nothing personal, but I'm rejecting this one :-).
Signature · 3 lines
Michael Kerrisk Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/ Linux/UNIX System Programming Training: http://man7.org/training/