projects
/
systemd
/
.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
| inline |
side by side
(parent:
b907b90
)
string-util: make strgrowpad0() a bit safer
author
Lennart Poettering
<lennart@poettering.net>
Mon, 25 Sep 2023 17:06:06 +0000
(19:06 +0200)
committer
Lennart Poettering
<lennart@poettering.net>
Mon, 25 Sep 2023 17:10:37 +0000
(19:10 +0200)
Let#s make sure we never shorten the allocation leaving an invalid
string (i.e. a memory allocation without a trailing NUL) around.
src/basic/string-util.c
patch
|
blob
|
history
diff --git
a/src/basic/string-util.c
b/src/basic/string-util.c
index 854cf963acb1edcacd0a01d8de8ca8f9660cbcac..7329bfacdf05b91ca4ad2c0f8b320644163fc6b2 100644
(file)
--- a/
src/basic/string-util.c
+++ b/
src/basic/string-util.c
@@
-627,14
+627,23
@@
char* strshorten(char *s, size_t l) {
}
int strgrowpad0(char **s, size_t l) {
+ size_t sz;
+
assert(s);
+ if (*s) {
+ sz = strlen(*s) + 1;
+ if (sz >= l) /* never shrink */
+ return 0;
+ } else
+ sz = 0;
+
char *q = realloc(*s, l);
if (!q)
return -ENOMEM;
+
*s = q;
- size_t sz = strlen(*s);
memzero(*s + sz, l - sz);
return 0;
}