]> git.ipfire.org Git - thirdparty/systemd.git/commit
tree-wide: make new/new0/malloc_multiply/reallocarray safe for size 0
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 21 Dec 2018 09:21:41 +0000 (10:21 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 21 Dec 2018 15:39:34 +0000 (16:39 +0100)
commit830464c3e44c9a9c06dc613555663a3e7389d8b9
tree4157d80c047a6aae028658501691c0a3cc5a2f83
parent5f9026027da6e7286ae7d420829d300276e5957b
tree-wide: make new/new0/malloc_multiply/reallocarray safe for size 0

All underlying glibc calls are free to return NULL if the size argument
is 0. We most often call those functions with a fixed argument, or at least
something which obviously cannot be zero, but it's too easy to forget.

E.g. coverity complains about "rows = new0(JsonVariant*, n_rows-1);" in
format-table.c There is an assert that n_rows > 0, so we could hit this
corner case here. Let's simplify callers and make those functions "safe".

CID #1397035.

The compiler is mostly able to optimize this away:
$ size build{,-opt}/src/shared/libsystemd-shared-239.so
(before)
   text    data     bss     dec     hex filename
2643329  580940    3112 3227381  313ef5 build/src/shared/libsystemd-shared-239.so     (-O0 -g)
2170013  578588    3089 2751690  29fcca build-opt/src/shared/libsystemd-shared-239.so (-03 -flto -g)
(after)
   text    data     bss     dec     hex filename
2644017  580940    3112 3228069  3141a5 build/src/shared/libsystemd-shared-239.so
2170765  578588    3057 2752410  29ff9a build-opt/src/shared/libsystemd-shared-239.so
src/basic/alloc-util.c
src/basic/alloc-util.h