ctf, ctf-writer: Fix -Wnull-dereference warnings
authorSimon Marchi <simon.marchi@efficios.com>
Mon, 28 Oct 2019 00:45:18 +0000 (20:45 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Tue, 29 Oct 2019 15:35:18 +0000 (11:35 -0400)
commit8fe10fa82e1ced6204b27d285b3d22db58d1bd06
tree9a91538af5b95e91700edc56c9a20bc82d00f0d6
parent96741e7fdda9d90e3c8b0ee5995cdd2baece42ef
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.024558 seconds and 4 git commands to generate.