Skip to content

Commit

Permalink
libselinux: store_stem(): do not free possible non-heap object
Browse files Browse the repository at this point in the history
GCC 11 complains:

In file included from label_file.c:24:
In function ‘store_stem’,
    inlined from ‘load_mmap’ at label_file.c:277:12,
    inlined from ‘process_file’ at label_file.c:551:5:
label_file.h:289:25: error: ‘free’ called on pointer ‘*mmap_area.next_addr’ with nonzero offset 4 [-Werror=free-nonheap-object]
  289 |                         free(buf);
      |                         ^~~~~~~~~

Free the pointer on failure at the caller instead of inside `store_stem()`.

Signed-off-by: Christian Göttsche <[email protected]>
  • Loading branch information
cgzones authored and bachradsusi committed May 18, 2021
1 parent db69a3d commit 6e5d16a
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions libselinux/src/label_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ static inline int store_stem(struct saved_data *data, char *buf, int stem_len)
tmp_arr = realloc(data->stem_arr,
sizeof(*tmp_arr) * alloc_stems);
if (!tmp_arr) {
free(buf);
return -1;
}
data->alloc_stems = alloc_stems;
Expand All @@ -308,6 +307,7 @@ static inline int find_stem_from_spec(struct saved_data *data, const char *buf)
int stem_len = get_stem_from_spec(buf);
int stemid;
char *stem;
int r;

if (!stem_len)
return -1;
Expand All @@ -321,7 +321,11 @@ static inline int find_stem_from_spec(struct saved_data *data, const char *buf)
if (!stem)
return -1;

return store_stem(data, stem, stem_len);
r = store_stem(data, stem, stem_len);
if (r < 0)
free(stem);

return r;
}

/* This will always check for buffer over-runs and either read the next entry
Expand Down

0 comments on commit 6e5d16a

Please sign in to comment.