Skip to content

Commit

Permalink
Fix gcc warnings (#842)
Browse files Browse the repository at this point in the history
* src/augprint.c: Fix reallocarray warnings

GCC 14.2.1 gives lots of warnings of this sort about the order of
parameters of reallocarray being the wrong way round:

augprint.c: In function ‘find_or_create_group’:
augprint.c:462:60: error: ‘reallocarray’ sizes specified with ‘sizeof’ in the earlier argument and not in the later argument [-Werror=calloc-transposed-args]
  462 |       all_groups_realloc = reallocarray(all_groups, sizeof(struct group *), num_groups_newsize);
      |                                                            ^~~~~~
augprint.c:462:60: note: earlier argument should specify number of elements, later size of each element

Signed-off-by: Richard W.M. Jones <[email protected]>

* acinclude.m4: Disable -Winline warnings

GCC 14.2.1 gives warnings like:

In file included from memory.h:26,
                 from augmatch.c:30:
In function ‘guess_lens_name’,
    inlined from ‘main’ at augmatch.c:396:16:
internal.h:229:19: error: inlining failed in call to ‘streqv’: call is unlikely and code size would grow [-Werror=inline]
  229 | static inline int streqv(const char *a, const char *b) {
      |                   ^~~~~~
augmatch.c:294:9: note: called from here
  294 |     if (streqv(ext, ".json")) {
      |         ^~~~~~~~~~~~~~~~~~~~

These warnings don't seem very useful in Augeas, since we are not so
sensitive to code size and don't care about inlining "purity".  Rather
than attempting to "fix" code which isn't really broken, I chose to
disable an unhelpful warning.

Signed-off-by: Richard W.M. Jones <[email protected]>

* src/get.c: Disable false GCC warning

GCC 14.2.1 doesn't seem to be able to analyze this stack frame code
correctly.  When compiling augeas with --enable-compile-warnings=error
that causes the build to fail with the errors below.  Disable the
error around this code.

In file included from /usr/include/string.h:548,
                 from ../gnulib/lib/string.h:41,
                 from internal.h:31,
                 from get.c:30:
In function ‘memset’,
    inlined from ‘push_frame’ at get.c:1095:5:
/usr/include/bits/string_fortified.h:59:10: error: ‘__builtin_memset’ offset [0, 31] is out of the bounds [0, 0] [-Werror=array-bounds=]
   59 |   return __builtin___memset_chk (__dest, __ch, __len,
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   60 |                                  __glibc_objsize0 (__dest));
      |                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~

If you disable -Warray-bounds only then you get this instead:

In file included from /usr/include/string.h:548,
                 from ../gnulib/lib/string.h:41,
                 from internal.h:31,
                 from get.c:30:
In function ‘memset’,
    inlined from ‘push_frame’ at get.c:1100:5:
/usr/include/bits/string_fortified.h:59:10: error: ‘__builtin_memset’ writing 32 bytes into a region of size 0 overflows the destination [-Werror=stringop-overflow=]
   59 |   return __builtin___memset_chk (__dest, __ch, __len,
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   60 |                                  __glibc_objsize0 (__dest));
      |                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~
In function ‘push_frame’:
cc1: note: destination object is likely at address zero

Signed-off-by: Richard W.M. Jones <[email protected]>

* tests/test-api.c: Fix calloc warning

GCC 14.2.1 gives:

test-api.c: In function ‘testAugPreview’:
test-api.c:867:39: error: ‘calloc’ sizes specified with ‘sizeof’ in the earlier argument and not in the later argument [-Werror=calloc-transposed-args]
  867 |             hosts_txt = calloc(sizeof(char),4096);
      |                                       ^~~~
test-api.c:867:39: note: earlier argument should specify number of elements, later size of each element

Signed-off-by: Richard W.M. Jones <[email protected]>

---------

Signed-off-by: Richard W.M. Jones <[email protected]>
  • Loading branch information
rwmjones authored Aug 18, 2024
1 parent 8a5a267 commit 89d7bc4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
4 changes: 2 additions & 2 deletions acinclude.m4
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ dnl
dnl Taken from libvirt/acinclude.m4
dnl
dnl We've added:
dnl -Wextra -Wshadow -Wcast-align -Wwrite-strings -Waggregate-return -Wstrict-prototypes -Winline -Wredundant-decls
dnl -Wextra -Wshadow -Wcast-align -Wwrite-strings -Waggregate-return -Wstrict-prototypes -Wredundant-decls
dnl We've removed
dnl CFLAGS="$realsave_CFLAGS"
dnl to avoid clobbering user-specified CFLAGS
Expand Down Expand Up @@ -34,7 +34,7 @@ AC_DEFUN([AUGEAS_COMPILE_WARNINGS],[
maximum|error)
try_compiler_flags="-Wall -Wformat -Wformat-security -Wmissing-prototypes -Wnested-externs -Wpointer-arith"
try_compiler_flags="$try_compiler_flags -Wextra -Wshadow -Wcast-align -Wwrite-strings -Waggregate-return"
try_compiler_flags="$try_compiler_flags -Wstrict-prototypes -Winline -Wredundant-decls -Wno-sign-compare"
try_compiler_flags="$try_compiler_flags -Wstrict-prototypes -Wredundant-decls -Wno-sign-compare"
try_compiler_flags="$try_compiler_flags $common_flags"
if test "$enable_compile_warnings" = "error" ; then
try_compiler_flags="$try_compiler_flags -Werror"
Expand Down
24 changes: 12 additions & 12 deletions src/augprint.c
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ static struct group *find_or_create_group(char *head) {
/* First, grow all_groups[] array if required */
if ( num_groups % 32 == 0 ) {
num_groups_newsize = (num_groups)/32*32+32;
all_groups_realloc = reallocarray(all_groups, sizeof(struct group *), num_groups_newsize);
all_groups_realloc = reallocarray(all_groups, num_groups_newsize, sizeof(struct group *));
CHECK_OOM( ! all_groups_realloc, exit_oom, "in find_or_create_group()");

all_groups=all_groups_realloc;
Expand Down Expand Up @@ -523,10 +523,10 @@ static struct tail *find_or_create_tail(struct group *group, struct path_segment
tail = malloc(sizeof(struct tail));
CHECK_OOM( ! tail, exit_oom, "in find_or_create_tail()");

tail->tail_found_map = reallocarray(NULL, sizeof(unsigned int), group->position_array_size);
tail->tail_found_map = reallocarray(NULL, group->position_array_size, sizeof(unsigned int));
CHECK_OOM( ! tail->tail_found_map, exit_oom, "in find_or_create_tail()");

tail->tail_value_found_map = reallocarray(NULL, sizeof(unsigned int), group->position_array_size);
tail->tail_value_found_map = reallocarray(NULL, group->position_array_size, sizeof(unsigned int));
CHECK_OOM( ! tail->tail_value_found_map, exit_oom, "in find_or_create_tail()");


Expand Down Expand Up @@ -584,13 +584,13 @@ static void grow_position_arrays(struct group *group, unsigned int new_max_posit
unsigned int new_size = (new_max_position+1) / 8 * 8 + 8;

/* Grow arrays within struct group */
tails_at_position_realloc = reallocarray(group->tails_at_position, sizeof(struct tail_stub *), new_size);
chosen_tail_realloc = reallocarray(group->chosen_tail, sizeof(struct tail *), new_size);
first_tail_realloc = reallocarray(group->first_tail, sizeof(struct tail_stub *), new_size);
chosen_tail_state_realloc = reallocarray(group->chosen_tail_state, sizeof(chosen_tail_state_t), new_size);
pretty_width_ct_realloc = reallocarray(group->pretty_width_ct, sizeof(unsigned int), new_size);
re_width_ct_realloc = reallocarray(group->re_width_ct, sizeof(unsigned int), new_size);
re_width_ft_realloc = reallocarray(group->re_width_ft, sizeof(unsigned int), new_size);
tails_at_position_realloc = reallocarray(group->tails_at_position, new_size, sizeof(struct tail_stub *));
chosen_tail_realloc = reallocarray(group->chosen_tail, new_size, sizeof(struct tail *));
first_tail_realloc = reallocarray(group->first_tail, new_size, sizeof(struct tail_stub *));
chosen_tail_state_realloc = reallocarray(group->chosen_tail_state, new_size, sizeof(chosen_tail_state_t));
pretty_width_ct_realloc = reallocarray(group->pretty_width_ct, new_size, sizeof(unsigned int));
re_width_ct_realloc = reallocarray(group->re_width_ct, new_size, sizeof(unsigned int));
re_width_ft_realloc = reallocarray(group->re_width_ft, new_size, sizeof(unsigned int));
CHECK_OOM( ! tails_at_position_realloc || ! chosen_tail_realloc || ! chosen_tail_state_realloc ||
! pretty_width_ct_realloc || ! re_width_ct_realloc || ! re_width_ft_realloc ||
! first_tail_realloc, exit_oom, "in grow_position_arrays()");
Expand All @@ -617,8 +617,8 @@ static void grow_position_arrays(struct group *group, unsigned int new_max_posit
for( tail = group->all_tails; tail != NULL; tail=tail->next ) {
unsigned int *tail_found_map_realloc;
unsigned int *tail_value_found_map_realloc;
tail_found_map_realloc = reallocarray(tail->tail_found_map, sizeof(unsigned int), new_size);
tail_value_found_map_realloc = reallocarray(tail->tail_value_found_map, sizeof(unsigned int), new_size);
tail_found_map_realloc = reallocarray(tail->tail_found_map, new_size, sizeof(unsigned int));
tail_value_found_map_realloc = reallocarray(tail->tail_value_found_map, new_size, sizeof(unsigned int));
CHECK_OOM( ! tail_found_map_realloc || ! tail_value_found_map_realloc, exit_oom, "in grow_position_arrays()");

/* initialize array entries between old size to new_size */
Expand Down
7 changes: 7 additions & 0 deletions src/get.c
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,14 @@ static struct frame *push_frame(struct rec_state *state, struct lens *lens) {
state->fused += 1;

struct frame *top = top_frame(state);
/* GCC 14.2.1 cannot analyze this correctly, so it breaks with
* -Werror. Until this is fixed in GCC, disable the warning.
*/
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds"
#pragma GCC diagnostic ignored "-Wstringop-overflow"
MEMZERO(top, 1);
#pragma GCC diagnostic pop
top->lens = lens;
return top;
error:
Expand Down
2 changes: 1 addition & 1 deletion tests/test-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ static void testAugPreview(CuTest *tc) {
if (asprintf(&etc_hosts_fn,"%s/etc/hosts",root) >=0 ) {
hosts_fp = fopen(etc_hosts_fn,"r");
if ( hosts_fp ) {
hosts_txt = calloc(sizeof(char),4096);
hosts_txt = calloc(4096,sizeof(char));
if ( hosts_txt ) {
readsz = fread(hosts_txt,sizeof(char),4096,hosts_fp);
*(hosts_txt+readsz) = '\0';
Expand Down

0 comments on commit 89d7bc4

Please sign in to comment.