libcli icon indicating copy to clipboard operation
libcli copied to clipboard

Arguments to calloc are reversed

Open WebbyMD1 opened this issue 1 year ago • 0 comments

Building libcli with GCC 14 or newer fails. It seems that some (but not all) calls to calloc are done with the size of a struct as the first parameter and the second parameter the number of those objects to allocate memory for. The call to calloc is the reverse of this. Gcc is now checking for this error (-Werror=calloc-transposed-args) and because it is detecting the parameters swapped the build fails. I have found this on an Ubunut 24.10 machine, with GCC 14.2.0 and the current head of the stable branch. The errors reported by GCC are as below:

gcc -g -O3 -Wall -std=c99 -pedantic -Wformat-security -Wno-format-zero-length -Werror -Wwrite-strings -Wformat -fdiagnostics-show-option -Wextra -Wsign-compare -Wcast-align -Wno-unused-parameter -fPIC -o libcli.o -c libcli.c libcli.c: In function ‘cli_register_command’: libcli.c:430:27: error: ‘calloc’ sizes specified with ‘sizeof’ in the earlier argument and not in the later argument [-Werror=calloc-transposed-args] 430 | if (!(c = calloc(sizeof(struct cli_command), 1))) return NULL; | ^~~~~~ libcli.c:430:27: note: earlier argument should specify number of elements, later size of each element libcli.c: In function ‘cli_init’: libcli.c:600:29: error: ‘calloc’ sizes specified with ‘sizeof’ in the earlier argument and not in the later argument [-Werror=calloc-transposed-args] 600 | if (!(cli = calloc(sizeof(struct cli_def), 1))) return 0; | ^~~~~~ libcli.c:600:29: note: earlier argument should specify number of elements, later size of each element libcli.c: In function ‘cli_match_filter_init’: libcli.c:1960:38: error: ‘calloc’ sizes specified with ‘sizeof’ in the earlier argument and not in the later argument [-Werror=calloc-transposed-args] 1960 | filt->data = state = calloc(sizeof(struct cli_match_filter_state), 1); | ^~~~~~ libcli.c:1960:38: note: earlier argument should specify number of elements, later size of each element libcli.c: In function ‘cli_range_filter_init’: libcli.c:2053:38: error: ‘calloc’ sizes specified with ‘sizeof’ in the earlier argument and not in the later argument [-Werror=calloc-transposed-args] 2053 | filt->data = state = calloc(sizeof(struct cli_range_filter_state), 1); | ^~~~~~ libcli.c:2053:38: note: earlier argument should specify number of elements, later size of each element libcli.c: In function ‘cli_count_filter_init’: libcli.c:2090:36: error: ‘calloc’ sizes specified with ‘sizeof’ in the earlier argument and not in the later argument [-Werror=calloc-transposed-args] 2090 | if (!(filt->data = calloc(sizeof(int), 1))) return CLI_ERROR; | ^~~ libcli.c:2090:36: note: earlier argument should specify number of elements, later size of each element libcli.c: In function ‘cli_register_filter’: libcli.c:2147:27: error: ‘calloc’ sizes specified with ‘sizeof’ in the earlier argument and not in the later argument [-Werror=calloc-transposed-args] 2147 | if (!(c = calloc(sizeof(struct cli_command), 1))) return NULL; | ^~~~~~ libcli.c:2147:27: note: earlier argument should specify number of elements, later size of each element libcli.c: In function ‘cli_register_optarg’: libcli.c:2259:32: error: ‘calloc’ sizes specified with ‘sizeof’ in the earlier argument and not in the later argument [-Werror=calloc-transposed-args] 2259 | if (!(optarg = calloc(sizeof(struct cli_optarg), 1))) goto CLEANUP; | ^~~~~~ libcli.c:2259:32: note: earlier argument should specify number of elements, later size of each element libcli.c: In function ‘cli_int_register_buildmode_command’: libcli.c:2535:27: error: ‘calloc’ sizes specified with ‘sizeof’ in the earlier argument and not in the later argument [-Werror=calloc-transposed-args] 2535 | if (!(c = calloc(sizeof(struct cli_command), 1))) return NULL; | ^~~~~~ libcli.c:2535:27: note: earlier argument should specify number of elements, later size of each element libcli.c: In function ‘cli_int_execute_pipeline’: libcli.c:3098:27: error: ‘calloc’ sizes specified with ‘sizeof’ in the earlier argument and not in the later argument [-Werror=calloc-transposed-args] 3098 | *filt = calloc(sizeof(struct cli_filter), 1); | ^~~~~~ libcli.c:3098:27: note: earlier argument should specify number of elements, later size of each element cc1: all warnings being treated as errors make: *** [Makefile:53: libcli.o] Error 1

WebbyMD1 avatar Nov 27 '24 14:11 WebbyMD1