aboutsummaryrefslogtreecommitdiffstats
path: root/libmount/src
diff options
context:
space:
mode:
authorThomas Weißschuh <thomas@t-8ch.de>2023-09-22 11:56:42 +0200
committerThomas Weißschuh <thomas@t-8ch.de>2023-09-22 11:56:42 +0200
commit64d6d400f601dd20f65d66539fdecf536e4d52b5 (patch)
tree140b19901b6b6eca466b9a135b129890230ccd68 /libmount/src
parent1e0ad14b3ac08d855cda6de346a65f9b834e00db (diff)
downloadutil-linux-64d6d400f601dd20f65d66539fdecf536e4d52b5.tar.gz
treewide: use (x)reallocarray() when applicable
reallocarray() prevents overflow of the multiplication. It also avoids issues with operator precedence like in libmount/src/context.c: pids = realloc(cxt->children, sizeof(pid_t) * cxt->nchildren + 1); This only allocated one additional byte, and not enough space for another child. Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
Diffstat (limited to 'libmount/src')
-rw-r--r--libmount/src/cache.c2
-rw-r--r--libmount/src/context.c2
-rw-r--r--libmount/src/utils.c2
3 files changed, 3 insertions, 3 deletions
diff --git a/libmount/src/cache.c b/libmount/src/cache.c
index 2505919c99..459441a8b1 100644
--- a/libmount/src/cache.c
+++ b/libmount/src/cache.c
@@ -202,7 +202,7 @@ static int cache_add_entry(struct libmnt_cache *cache, char *key,
if (cache->nents == cache->nallocs) {
size_t sz = cache->nallocs + MNT_CACHE_CHUNKSZ;
- e = realloc(cache->ents, sz * sizeof(struct mnt_cache_entry));
+ e = reallocarray(cache->ents, sz, sizeof(struct mnt_cache_entry));
if (!e)
return -ENOMEM;
cache->ents = e;
diff --git a/libmount/src/context.c b/libmount/src/context.c
index 0cd320190e..7efbd6193d 100644
--- a/libmount/src/context.c
+++ b/libmount/src/context.c
@@ -2850,7 +2850,7 @@ static int mnt_context_add_child(struct libmnt_context *cxt, pid_t pid)
if (!cxt)
return -EINVAL;
- pids = realloc(cxt->children, sizeof(pid_t) * cxt->nchildren + 1);
+ pids = reallocarray(cxt->children, cxt->nchildren + 1, sizeof(pid_t));
if (!pids)
return -ENOMEM;
diff --git a/libmount/src/utils.c b/libmount/src/utils.c
index 3817b39271..c20c949a84 100644
--- a/libmount/src/utils.c
+++ b/libmount/src/utils.c
@@ -526,7 +526,7 @@ static int add_filesystem(char ***filesystems, char *name)
if (n == 0 || !((n + 1) % MYCHUNK)) {
size_t items = ((n + 1 + MYCHUNK) / MYCHUNK) * MYCHUNK;
- char **x = realloc(*filesystems, items * sizeof(char *));
+ char **x = reallocarray(*filesystems, items, sizeof(char *));
if (!x)
goto err;