summaryrefslogtreecommitdiff
path: root/src/basic/alloc-util.c
Commit message (Collapse)AuthorAgeFilesLines
* Use dummy allocator to make accesses defined as per standardSiddhesh Poyarekar2022-12-141-0/+4
| | | | | | | | | | | | | | | | | | | | | | systemd uses malloc_usable_size() everywhere to use memory blocks obtained through malloc, but that is abuse since the malloc_usable_size() interface isn't meant for this kind of use, it is for diagnostics only. This is also why systemd behaviour is flaky when built with _FORTIFY_SOURCE. One way to make this more standard (and hence safer) is to, at every malloc_usable_size() call, also 'reallocate' the block so that the compiler can see the larger size. This is done through a dummy reallocator whose only purpose is to tell the compiler about the larger usable size, it doesn't do any actual reallocation. Florian Weimer pointed out that this doesn't solve the problem of an allocator potentially growing usable size at will, which will break the implicit assumption in systemd use that the value returned remains constant as long as the object is valid. The safest way to fix that is for systemd to step away from using malloc_usable_size() like this. Resolves #22801.
* Merge pull request #19662 from yuwata/memdupLennart Poettering2021-05-191-4/+3
|\ | | | | util: make memdup() or friends safer
| * alloc-util: use memcpy_safe() in memdup() or friendsYu Watanabe2021-05-191-4/+3
| |
* | alloc-util: simplify GREEDY_REALLOC() logic by relying on malloc_usable_size()Lennart Poettering2021-05-191-38/+31
|/ | | | | | | | | | | | | | | | | | | | | | | | We recently started making more use of malloc_usable_size() and rely on it (see the string_erase() story). Given that we don't really support sytems where malloc_usable_size() cannot be trusted beyond statistics anyway, let's go fully in and rework GREEDY_REALLOC() on top of it: instead of passing around and maintaining the currenly allocated size everywhere, let's just derive it automatically from malloc_usable_size(). I am mostly after this for the simplicity this brings. It also brings minor efficiency improvements I guess, but things become so much nicer to look at if we can avoid these allocation size variables everywhere. Note that the malloc_usable_size() man page says relying on it wasn't "good programming practice", but I think it does this for reasons that don't apply here: the greedy realloc logic specifically doesn't rely on the returned extra size, beyond the fact that it is equal or larger than what was requested. (This commit was supposed to be a quick patch btw, but apparently we use the greedy realloc stuff quite a bit across the codebase, so this ends up touching *a*lot* of code.)
* license: LGPL-2.1+ -> LGPL-2.1-or-laterYu Watanabe2020-11-091-1/+1
|
* tree-wide: various code-formatting improvementsFrantisek Sumsal2019-09-221-1/+1
| | | | Reported/found by Coccinelle
* alloc-util: reintroduce malloc_usable_size() into greedy_realloc()Lennart Poettering2019-05-021-0/+24
| | | | | | | | This is another attempt at d4b604baeadbb2498e4f2c3e260260eed210f5d6 and #12438 Instead of blindly using the extra allocated space, let's do so only after telling libc about it, via a second realloc(). The second realloc() should be quick, since it never has to copy memory around.
* alloc-util: don't use malloc_usable_size() to determine allocated sizeAaron Barany2019-04-301-2/+1
| | | | | | | | | | | | This reverts commit d4b604baeadbb2498e4f2c3e260260eed210f5d6. When realloc() is called, the extra memory between the originally requested size and the end of malloc_usable_size() isn't copied. (at least with the version of glibc that currently ships on Arch Linux) As a result, some elements get lost and use uninitialized memory, most commonly 0, and can lead to crashes. fixes #12384
* alloc-util: use malloc_usable_size() to determine allocated sizeLennart Poettering2019-03-201-1/+2
| | | | | | It's a glibc-specific API, but supported on FreeBSD and musl too at least, hence fairly common. This way we can reduce our calls to realloc() as much as possible.
* alloc-util: add extra overflow checks to GREEDY_REALLOC()Lennart Poettering2019-03-201-4/+8
|
* alloc-util: extra paranoid overflow checkLennart Poettering2019-03-201-0/+3
|
* util: split out memcmp()/memset() related calls into memory-util.[ch]Lennart Poettering2019-03-131-1/+1
| | | | Just some source rearranging.
* tree-wide: make new/new0/malloc_multiply/reallocarray safe for size 0Zbigniew Jędrzejewski-Szmek2018-12-211-1/+1
| | | | | | | | | | | | | | | | | | | | | | | 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
* tree-wide: remove Lennart's copyright linesLennart Poettering2018-06-141-3/+0
| | | | | | | | | | | These lines are generally out-of-date, incomplete and unnecessary. With SPDX and git repository much more accurate and fine grained information about licensing and authorship is available, hence let's drop the per-file copyright notice. Of course, removing copyright lines of others is problematic, hence this commit only removes my own lines and leaves all others untouched. It might be nicer if sooner or later those could go away too, making git the only and accurate source of authorship information.
* tree-wide: drop 'This file is part of systemd' blurbLennart Poettering2018-06-141-2/+0
| | | | | | | | | | | | | | | | This part of the copyright blurb stems from the GPL use recommendations: https://www.gnu.org/licenses/gpl-howto.en.html The concept appears to originate in times where version control was per file, instead of per tree, and was a way to glue the files together. Ultimately, we nowadays don't live in that world anymore, and this information is entirely useless anyway, as people are very welcome to copy these files into any projects they like, and they shouldn't have to change bits that are part of our copyright header for that. hence, let's just get rid of this old cruft, and shorten our codebase a bit.
* tree-wide: drop license boilerplateZbigniew Jędrzejewski-Szmek2018-04-061-13/+0
| | | | | | | | | | Files which are installed as-is (any .service and other unit files, .conf files, .policy files, etc), are left as is. My assumption is that SPDX identifiers are not yet that well known, so it's better to retain the extended header to avoid any doubt. I also kept any copyright lines. We can probably remove them, but it'd nice to obtain explicit acks from all involved authors before doing that.
* alloc-util: coding style fixYu Watanabe2017-11-281-1/+1
|
* Add SPDX license identifiers to source files under the LGPLZbigniew Jędrzejewski-Szmek2017-11-191-0/+1
| | | | | This follows what the kernel is doing, c.f. https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=5fd54ace4721fc5ce2bb5aef6318fcf17f421460.
* alloc-util: add new helpers memdup_suffix0() and newdup_suffix0()Lennart Poettering2017-07-311-6/+21
| | | | | | | These are similar to memdup() and newdup(), but reserve one extra NUL byte at the end of the new allocation and initialize it. It's useful when copying out data from fixed size character arrays where NUL termination can't be assumed.
* tree-wide: remove Emacs lines from all filesDaniel Mack2016-02-101-2/+0
| | | | | This should be handled fine now by .dir-locals.el, so need to carry that stuff in every file.
* basic: include only what we useThomas Hindoe Paaboel Andersen2015-11-301-0/+4
| | | | | This is a cleaned up result of running iwyu but without forward declarations on src/basic.
* util-lib: split out allocation calls into alloc-util.[ch]Lennart Poettering2015-10-271-0/+81