summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-08-23 01:33:50 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-08-23 02:09:51 +0200
commit7c028e224978e1d5a4f207cc42b9eb58d81897dd (patch)
treed7165e3b9d1a7041cd79712526ad472c21135bdc /lib
parent0a5383ccdf8e87d2259d02a9ff44420b3bc3f58d (diff)
Some simple search/replace cases for allocation
This commit exchanges some malloc(x + y * z) patterns that can be found with a simple git grep and are obvious for the new wrappers. Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib')
-rw-r--r--lib/comp/block_processor.c2
-rw-r--r--lib/comp/block_processor_parallel.c11
-rw-r--r--lib/comp/create_block.c2
-rw-r--r--lib/comp/internal.h1
-rw-r--r--lib/comp/lzo.c4
-rw-r--r--lib/fstree/fstree_from_dir.c4
-rw-r--r--lib/fstree/gen_inode_table.c4
-rw-r--r--lib/fstree/mknode.c1
-rw-r--r--lib/sqfs/data_reader.c8
-rw-r--r--lib/sqfs/data_writer.c2
-rw-r--r--lib/sqfs/read_inode.c5
-rw-r--r--lib/sqfs/write_export_table.c1
-rw-r--r--lib/sqfs/write_xattr.c2
-rw-r--r--lib/util/str_table.c3
14 files changed, 29 insertions, 21 deletions
diff --git a/lib/comp/block_processor.c b/lib/comp/block_processor.c
index 5938b3a..06dc384 100644
--- a/lib/comp/block_processor.c
+++ b/lib/comp/block_processor.c
@@ -29,7 +29,7 @@ block_processor_t *block_processor_create(size_t max_block_size,
void *user,
block_cb callback)
{
- block_processor_t *proc = calloc(1, sizeof(*proc) + max_block_size);
+ block_processor_t *proc = alloc_flex(sizeof(*proc), 1, max_block_size);
(void)num_workers;
if (proc == NULL) {
diff --git a/lib/comp/block_processor_parallel.c b/lib/comp/block_processor_parallel.c
index 985c900..b58ad5c 100644
--- a/lib/comp/block_processor_parallel.c
+++ b/lib/comp/block_processor_parallel.c
@@ -119,15 +119,13 @@ block_processor_t *block_processor_create(size_t max_block_size,
{
block_processor_t *proc;
unsigned int i;
- size_t size;
int ret;
if (num_workers < 1)
num_workers = 1;
- size = sizeof(proc->workers[0]) * num_workers;
-
- proc = calloc(1, sizeof(*proc) + size);
+ proc = alloc_flex(sizeof(*proc),
+ sizeof(proc->workers[0]), num_workers);
if (proc == NULL) {
perror("Creating block processor");
return NULL;
@@ -153,10 +151,9 @@ block_processor_t *block_processor_create(size_t max_block_size,
goto fail_cond;
}
- size = sizeof(compress_worker_t) + max_block_size;
-
for (i = 0; i < num_workers; ++i) {
- proc->workers[i] = calloc(1, size);
+ proc->workers[i] = alloc_flex(sizeof(compress_worker_t),
+ 1, max_block_size);
if (proc->workers[i] == NULL) {
perror("Creating block worker data");
diff --git a/lib/comp/create_block.c b/lib/comp/create_block.c
index e410091..90344bb 100644
--- a/lib/comp/create_block.c
+++ b/lib/comp/create_block.c
@@ -16,7 +16,7 @@
block_t *create_block(const char *filename, int fd, size_t size,
void *user, uint32_t flags)
{
- block_t *blk = calloc(1, sizeof(*blk) + size);
+ block_t *blk = alloc_flex(sizeof(*blk), 1, size);
if (blk == NULL) {
perror(filename);
diff --git a/lib/comp/internal.h b/lib/comp/internal.h
index 6bcd8fc..986266d 100644
--- a/lib/comp/internal.h
+++ b/lib/comp/internal.h
@@ -10,6 +10,7 @@
#include "config.h"
#include "compress.h"
+#include "util.h"
int generic_write_options(int fd, const void *data, size_t size);
diff --git a/lib/comp/lzo.c b/lib/comp/lzo.c
index 66c2f59..09ef75c 100644
--- a/lib/comp/lzo.c
+++ b/lib/comp/lzo.c
@@ -182,7 +182,7 @@ static compressor_t *lzo_create_copy(compressor_t *cmp)
lzo_compressor_t *other = (lzo_compressor_t *)cmp;
lzo_compressor_t *lzo;
- lzo = calloc(1, sizeof(*lzo) + lzo_algs[other->algorithm].bufsize);
+ lzo = alloc_flex(sizeof(*lzo), 1, lzo_algs[other->algorithm].bufsize);
if (lzo == NULL) {
perror("creating additional lzo compressor");
@@ -282,7 +282,7 @@ compressor_t *create_lzo_compressor(bool compress, size_t block_size,
if (options != NULL && process_options(options, &alg, &level) != 0)
return NULL;
- lzo = calloc(1, sizeof(*lzo) + lzo_algs[alg].bufsize);
+ lzo = alloc_flex(sizeof(*lzo), 1, lzo_algs[alg].bufsize);
base = (compressor_t *)lzo;
if (lzo == NULL) {
diff --git a/lib/fstree/fstree_from_dir.c b/lib/fstree/fstree_from_dir.c
index ae8cac9..9aab5df 100644
--- a/lib/fstree/fstree_from_dir.c
+++ b/lib/fstree/fstree_from_dir.c
@@ -90,7 +90,7 @@ static int populate_xattr(fstree_t *fs, tree_node_t *node)
goto fail;
if (vallen > 0) {
- value = calloc(1, vallen + 1);
+ value = alloc_string(vallen);
if (value == NULL) {
perror("xattr value buffer");
goto fail;
@@ -163,7 +163,7 @@ static int populate_dir(fstree_t *fs, tree_node_t *root, dev_t devstart,
continue;
if (S_ISLNK(sb.st_mode)) {
- extra = calloc(1, sb.st_size + 1);
+ extra = alloc_string(sb.st_size);
if (extra == NULL)
goto fail_rdlink;
diff --git a/lib/fstree/gen_inode_table.c b/lib/fstree/gen_inode_table.c
index 98294fa..04f68da 100644
--- a/lib/fstree/gen_inode_table.c
+++ b/lib/fstree/gen_inode_table.c
@@ -7,6 +7,7 @@
#include "config.h"
#include "fstree.h"
+#include "util.h"
#include <stdlib.h>
#include <stdio.h>
@@ -60,7 +61,8 @@ int fstree_gen_inode_table(fstree_t *fs)
size_t inum = 2;
fs->inode_tbl_size = count_nodes(fs->root) + 2;
- fs->inode_table = calloc(sizeof(tree_node_t *), fs->inode_tbl_size);
+ fs->inode_table = alloc_array(sizeof(tree_node_t *),
+ fs->inode_tbl_size);
if (fs->inode_table == NULL) {
perror("allocating inode table");
diff --git a/lib/fstree/mknode.c b/lib/fstree/mknode.c
index 954771f..9132458 100644
--- a/lib/fstree/mknode.c
+++ b/lib/fstree/mknode.c
@@ -7,6 +7,7 @@
#include "config.h"
#include "fstree.h"
+#include "util.h"
#include <string.h>
#include <stdlib.h>
diff --git a/lib/sqfs/data_reader.c b/lib/sqfs/data_reader.c
index 27a8302..9d24745 100644
--- a/lib/sqfs/data_reader.c
+++ b/lib/sqfs/data_reader.c
@@ -107,7 +107,7 @@ static int precache_fragment_block(data_reader_t *data, size_t idx)
data_reader_t *data_reader_create(int fd, sqfs_super_t *super,
compressor_t *cmp)
{
- data_reader_t *data = calloc(1, sizeof(*data) + 3 * super->block_size);
+ data_reader_t *data = alloc_flex(sizeof(*data), super->block_size, 3);
size_t i, size;
if (data == NULL) {
@@ -136,7 +136,11 @@ data_reader_t *data_reader_create(int fd, sqfs_super_t *super,
return NULL;
}
- size = sizeof(data->frag[0]) * data->num_fragments;
+ if (SZ_MUL_OV(sizeof(data->frag[0]), data->num_fragments, &size)) {
+ fputs("Too many fragments: overflow\n", stderr);
+ free(data);
+ return NULL;
+ }
data->frag = sqfs_read_table(fd, cmp, size,
super->fragment_table_start);
diff --git a/lib/sqfs/data_writer.c b/lib/sqfs/data_writer.c
index b52e897..d4b402b 100644
--- a/lib/sqfs/data_writer.c
+++ b/lib/sqfs/data_writer.c
@@ -416,7 +416,7 @@ int write_data_from_fd_condensed(data_writer_t *data, file_info_t *fi,
diff = fi->size - offset;
}
- blk = calloc(1, sizeof(*blk) + diff);
+ blk = alloc_flex(sizeof(*blk), 1, diff);
blk->size = diff;
blk->index = i++;
blk->user = fi;
diff --git a/lib/sqfs/read_inode.c b/lib/sqfs/read_inode.c
index d13198e..79c5a55 100644
--- a/lib/sqfs/read_inode.c
+++ b/lib/sqfs/read_inode.c
@@ -7,6 +7,7 @@
#include "config.h"
#include "meta_reader.h"
+#include "util.h"
#include <sys/stat.h>
#include <stdlib.h>
@@ -89,7 +90,7 @@ static sqfs_inode_generic_t *read_inode_file(meta_reader_t *ir,
count = get_block_count(file.file_size, block_size,
file.fragment_index, file.fragment_offset);
- out = calloc(1, sizeof(*out) + count * sizeof(uint32_t));
+ out = alloc_flex(sizeof(*out), sizeof(uint32_t), count);
if (out == NULL) {
perror("reading extended file inode");
return NULL;
@@ -132,7 +133,7 @@ static sqfs_inode_generic_t *read_inode_file_ext(meta_reader_t *ir,
count = get_block_count(file.file_size, block_size,
file.fragment_idx, file.fragment_offset);
- out = calloc(1, sizeof(*out) + count * sizeof(uint32_t));
+ out = alloc_flex(sizeof(*out), sizeof(uint32_t), count);
if (out == NULL) {
perror("reading extended file inode");
return NULL;
diff --git a/lib/sqfs/write_export_table.c b/lib/sqfs/write_export_table.c
index a4ba1b7..f81632a 100644
--- a/lib/sqfs/write_export_table.c
+++ b/lib/sqfs/write_export_table.c
@@ -7,6 +7,7 @@
#include "config.h"
#include "highlevel.h"
+#include "util.h"
#include <stdlib.h>
#include <stdio.h>
diff --git a/lib/sqfs/write_xattr.c b/lib/sqfs/write_xattr.c
index 195de0d..7214ea7 100644
--- a/lib/sqfs/write_xattr.c
+++ b/lib/sqfs/write_xattr.c
@@ -152,7 +152,7 @@ static uint64_t *create_ool_locations_table(fstree_t *fs)
uint64_t *table;
size_t i;
- table = malloc(sizeof(uint64_t) * fs->xattr_values.num_strings);
+ table = alloc_array(sizeof(uint64_t), fs->xattr_values.num_strings);
if (table == NULL) {
perror("allocating Xattr OOL locations table");
diff --git a/lib/util/str_table.c b/lib/util/str_table.c
index ae3a4f2..ab07738 100644
--- a/lib/util/str_table.c
+++ b/lib/util/str_table.c
@@ -12,6 +12,7 @@
#include <stdio.h>
#include "str_table.h"
+#include "util.h"
/* R5 hash function (borrowed from reiserfs) */
static uint32_t strhash(const char *s)
@@ -54,7 +55,7 @@ int str_table_init(str_table_t *table, size_t size)
{
memset(table, 0, sizeof(*table));
- table->buckets = calloc(size, sizeof(table->buckets[0]));
+ table->buckets = alloc_array(size, sizeof(table->buckets[0]));
table->num_buckets = size;
if (table->buckets == NULL) {