ctf, ctf-writer: Fix -Wnull-dereference warnings
authorSimon Marchi <simon.marchi@efficios.com>
Mon, 28 Oct 2019 00:45:18 +0000 (20:45 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 30 Oct 2019 19:14:53 +0000 (15:14 -0400)
commit198f26cdfd3effd3272eaf238b3d4a437b6621e9
treeeaac30ae8421922fa0f3e6991bb4faf583ce14f3
parent100acf4a8f40dc60afaaaa788ed94118e1e93d43
ctf, ctf-writer: Fix -Wnull-dereference warnings

When building at the -O2 optimization level with gcc, I get this:

/home/simark/src/babeltrace/src/ctf-writer/resolve.c: In function ‘resolve_type’:
/home/simark/src/babeltrace/src/ctf-writer/resolve.c:541:7: error: potential null pointer dereference [-Werror=null-dereference]
  541 |   int cur_index = type_stack_at(ctx->type_stack,
      |       ^~~~~~~~~

gcc sees that type_stack_at can possibly return NULL, but we dereference
it unconditionally.  All callers of type_stack_at don't check NULL, so
that is a good sign that it never happens.  From what I understand, if
it were to happen it would be a programming error, not a legitimate
error.  So I think we can simplify this code by making it a precondition
of calling type_stack_at that `stack` should not be NULL and `index`
should represent a valid element of `stack`.  This gets rid of the
warning at the same time.

There is the same pattern in the ctf plugin code, so fix it there too.

Change-Id: I82a7cdf74ea987a597cdf219346ec3b36e8ab200
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/2268
Reviewed-by: Francis Deslauriers <francis.deslauriers@efficios.com>
src/ctf-writer/resolve.c
src/plugins/ctf/common/metadata/ctf-meta-resolve.c
This page took 0.024827 seconds and 4 git commands to generate.