Skip to content

lib/gis: improve diagnostic capabilities for compilers and/or static analyzers#7172

Merged
nilason merged 5 commits intoOSGeo:mainfrom
nilason:fix_malloc_attribs
Mar 13, 2026
Merged

lib/gis: improve diagnostic capabilities for compilers and/or static analyzers#7172
nilason merged 5 commits intoOSGeo:mainfrom
nilason:fix_malloc_attribs

Conversation

@nilason
Copy link
Contributor

@nilason nilason commented Mar 11, 2026

@nilason nilason added this to the 8.6.0 milestone Mar 11, 2026
Co-authored-by: Nicklas Larsson <n_larsson@yahoo.com>
@nilason
Copy link
Contributor Author

nilason commented Mar 11, 2026

GCC runner fails, as proof this works as expected:

gcc  -std=gnu17 -fPIC -Wall -Wextra -isystem/usr/include/gdal -Wpedantic -Werror  -I/home/runner/work/grass/grass/dist.x86_64-pc-linux-gnu/include -I/home/runner/work/grass/grass/dist.x86_64-pc-linux-gnu/include     -I/usr/include/gdal -I/usr/include -DPACKAGE=\""grasslibs"\"   -I/home/runner/work/grass/grass/dist.x86_64-pc-linux-gnu/include -I/home/runner/work/grass/grass/dist.x86_64-pc-linux-gnu/include -DRELDIR=\"lib/vector/Vlib\" -o OBJ.x86_64-pc-linux-gnu/map.o -c map.c
map.c: In function ‘Vect_rename’:
map.c:352:5: error: ‘free’ called on pointer returned from a mismatched allocation function [-Werror=mismatched-dealloc]
  352 |     free(fields);
      |     ^~~~~~~~~~~~
In file included from /home/runner/work/grass/grass/dist.x86_64-pc-linux-gnu/include/grass/gis.h:734,
                 from /home/runner/work/grass/grass/dist.x86_64-pc-linux-gnu/include/grass/vector.h:3,
                 from map.c:28:
/home/runner/work/grass/grass/dist.x86_64-pc-linux-gnu/include/grass/defs/gis.h:131:25: note: returned from ‘G__malloc’
  131 | #define G_malloc(n)     G__malloc(RELDIR "/" __FILE__, __LINE__, (n))
      |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
map.c:288:21: note: in expansion of macro ‘G_malloc’
  288 |     fields = (int *)G_malloc(n * sizeof(int));
      |                     ^~~~~~~~
cc1: all warnings being treated as errors

echoix pushed a commit that referenced this pull request Mar 12, 2026
Fix `-Wmismatched-dealloc` and `-Wuse-after-free` warnings, which appear with #7172.
@nilason
Copy link
Contributor Author

nilason commented Mar 12, 2026

Added returns_nonnull to G_malloc, G_calloc, G_realloc as they always return a non-null value (and call G_fatal_error if failed).

@nilason nilason merged commit 09587a9 into OSGeo:main Mar 13, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants