namhyung / uftrace

Function graph tracer for C/C++/Rust/Python
https://uftrace.github.io/slide/
GNU General Public License v2.0
2.92k stars 419 forks source link

Compile warning on `‘sizeof’ in the earlier argument and not in the later argument` #1933

Closed yskelg closed 2 weeks ago

yskelg commented 2 weeks ago

I checked there are some warning on clang version 18.1.6 based AFL++ compiler.

So, I need to update some warning codes.

$ CC=$HOME/AFLplusplus/afl-clang-fast CFLGAS="-fno-omit-frame-pointer" LDFLGAS="-fno-omit-frame-pointer" DEBUG=1 ASAN=1 make -j4
  GEN      utils/mermaid.js.cstr
  GEN      version.h
  GEN      utils/mermaid.html.cstr
  CC       cmds/graph.o
  CC       cmds/info.o
  CC       cmds/live.o
  CC       cmds/record.o
  CC       cmds/recv.o
In file included from /home/paran/uftrace/utils/symbol.h:18,
                 from /home/paran/uftrace/uftrace.h:14,
                 from /home/paran/uftrace/cmds/graph.c:7:
/home/paran/uftrace/cmds/graph.c: In function ‘create_graph’:
/home/paran/uftrace/cmds/graph.c:213:53: error: ‘calloc’ sizes specified with ‘sizeof’ in the earlier argument and not in the later argument [-Werror=calloc-transposed-args]
  213 |         struct session_graph *graph = xzalloc(sizeof(*graph));
      |                                                     ^
/home/paran/uftrace/utils/utils.h:193:38: note: in definition of macro ‘xzalloc’
  193 |                 void *__ptr = calloc(sz, 1);                                                       \
      |                                      ^~
/home/paran/uftrace/cmds/graph.c:213:53: note: earlier argument should specify number of elements, later size of each element
  213 |         struct session_graph *graph = xzalloc(sizeof(*graph));
      |                                                     ^
/home/paran/uftrace/utils/utils.h:193:38: note: in definition of macro ‘xzalloc’
  193 |                 void *__ptr = calloc(sz, 1);                                                       \
      |                                      ^~
cc1: all warnings being treated as errors
make: *** [Makefile:330: /home/paran/uftrace/cmds/graph.o] 오류 1
make: *** 끝나지 않은 작업을 기다리고 있습니다....
In file included from /home/paran/uftrace/utils/symbol.h:18,
                 from /home/paran/uftrace/uftrace.h:14,
                 from /home/paran/uftrace/cmds/info.c:20:
/home/paran/uftrace/cmds/info.c: In function ‘read_taskinfo’:
/home/paran/uftrace/cmds/info.c:542:51: error: ‘calloc’ sizes specified with ‘sizeof’ in the earlier argument and not in the later argument [-Werror=calloc-transposed-args]
  542 |                         int *tids = xcalloc(sizeof(*tids), info->nr_tid);
      |                                                   ^
/home/paran/uftrace/utils/utils.h:202:38: note: in definition of macro ‘xcalloc’
  202 |                 void *__ptr = calloc(sz, n);                                                       \
      |                                      ^~
/home/paran/uftrace/cmds/info.c:542:51: note: earlier argument should specify number of elements, later size of each element
  542 |                         int *tids = xcalloc(sizeof(*tids), info->nr_tid);
      |                                                   ^
/home/paran/uftrace/utils/utils.h:202:38: note: in definition of macro ‘xcalloc’
  202 |                 void *__ptr = calloc(sz, n);                                                       \
      |                                      ^~
cc1: all warnings being treated as errors
make: *** [Makefile:330: /home/paran/uftrace/cmds/info.o] 오류 1

My Fedora 40 and clang environments.

$ uname -a
Linux fedora 6.10.0-0.rc2.20240608gitdc772f8237f9.29.fc41.aarch64 #1 SMP PREEMPT_DYNAMIC Mon Jun 10 04:14:31 KST 2024 aarch64 GNU/Linux

This warning also generated in clang, gcc compiler.

$ clang -v
clang version 18.1.6 (Fedora 18.1.6-3.fc40)
Target: aarch64-redhat-linux-gnu
$ gcc -v
Target: aarch64-redhat-linux
gcc version 14.1.1 20240522 (Red Hat 14.1.1-4) (GCC) 
yskelg commented 2 weeks ago

I've also compiled also with linux perf tools, there was no warning or error. I think nothing todo with uftace. but I will trying to update code similarly.

yskelg commented 2 weeks ago

I knew exactly how to use the function.