summaryrefslogtreecommitdiff
path: root/src/basic/alloc-util.h
Commit message (Collapse)AuthorAgeFilesLines
* alloc-util: drop _alloc_ decorator from memdup_suffix0()Lennart Poettering2019-07-131-1/+1
| | | | Fixes: https://github.com/systemd/systemd/pull/13034#issuecomment-510801671
* alloc-util: drop _alloc_(2, 3) decorator from memdup_suffix0_multiply()Lennart Poettering2019-07-121-1/+3
| | | | | | | This decorator tells compilers that the memory we return is shorter than it actually is, thus triggering misleading bad memory access complaints. Fixes: #13026
* alloc-util: typo fixLennart Poettering2019-03-201-2/+2
|
* Add wrapper for __msan_unpoinson() to reduce #ifdefferyZbigniew Jędrzejewski-Szmek2019-02-251-0/+10
| | | | | | This isn't really necessary for the subsequent commit, but I expect that we'll need to unpoison more often once we turn on msan in CI, so I think think this change makes sense in the long run.
* alloc-util: whenever any of our alloca() wrappers is used to allocate overly ↵Lennart Poettering2019-01-261-17/+32
| | | | | | | | | | | large memory blocks, hit an assert() Of course, this should never happen, but let's better be safe than sorry, and abort rather than continue when a too large memory block is allocated, simply asa safety precaution. An early abort is better than continuing with a likely memory corruption later.
* tree-wide: make new/new0/malloc_multiply/reallocarray safe for size 0Zbigniew Jędrzejewski-Szmek2018-12-211-3/+3
| | | | | | | | | | | | | | | | | | | | | | | 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
* util: define free_func_tYu Watanabe2018-12-021-0/+2
|
* alloc-util: add alloca() counterparts for memdup() and memdup_suffix0()Lennart Poettering2018-10-151-0/+15
|
* Revert "alloc-util: return NULL if 0-sized allocation is requested"Yu Watanabe2018-10-131-4/+1
| | | | This reverts commit c05107767b589e9aac9711eb385738887f86eb77.
* alloc-util: return NULL if 0-sized allocation is requestedDavid Tardon2018-10-121-1/+4
| | | | | That would almost certainly be an error (e.g., an overflow in computing _need_), so it's better to fail.
* tree-wide: remove Lennart's copyright linesLennart Poettering2018-06-141-4/+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.
* alloca: add an overflow check tooLennart Poettering2018-04-271-2/+10
| | | | | | | | | Of course, alloca() shouldn't be used with anything that can grow without bounds anyway, but let's better safe than sorry, and catch this early. Since alloca() is not supposed to return an error we trigger an assert() instead, which is still better than heap trickery.
* 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.
* macro: introduce TAKE_PTR() macroLennart Poettering2018-03-221-0/+9
| | | | | | | | | | | | | | | | This macro will read a pointer of any type, return it, and set the pointer to NULL. This is useful as an explicit concept of passing ownership of a memory area between pointers. This takes inspiration from Rust: https://doc.rust-lang.org/std/option/enum.Option.html#method.take and was suggested by Alan Jenkins (@sourcejedi). It drops ~160 lines of code from our codebase, which makes me like it. Also, I think it clarifies passing of ownership, and thus helps readability a bit (at least for the initiated who know the new macro)
* tree-wide: use reallocarray instead of our home-grown realloc_multiply (#8279)Zbigniew Jędrzejewski-Szmek2018-02-261-1/+3
| | | | | | | There isn't much difference, but in general we prefer to use the standard functions. glibc provides reallocarray since version 2.26. I moved explicit_bzero is configure test to the bottom, so that the two stdlib functions are at the bottom.
* 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-0/+10
| | | | | | | 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: introduce free_and_replace helperZbigniew Jędrzejewski-Szmek2016-10-161-0/+8
| | | | | | It's a common pattern, so add a helper for it. A macro is necessary because a function that takes a pointer to a pointer would be type specific, similarly to cleanup functions. Seems better to use a macro.
* alloc-util: cleanupsAlexander Kuleshov2016-02-191-9/+13
| | | | | | | | | | | | | | | This patch contains a set of little cleanups for alloc-util.h: 1. The malloc_multiply(), realloc_multiply() and memdup_multiply() functions check allocation related parameters on overflow. Let's move them to the separate size_multiply_overflow() function for simplicity, code duplication prevention and possible reuse in future. 2. use SIZE_MAX from stdlib instead of ((size_t) - 1) to be more clear. 3. The 'a'/'b' variables are renamed to 'size' and 'need' to be more clear.'
* 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/+1
| | | | | 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/+108