diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-11-25 13:13:05 +0100 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-11-25 13:20:08 +0100 |
commit | fc9a644002dc501a5c224e5cc1a7dfba3ca2d1d8 (patch) | |
tree | 6fb1acf211a1bf9005236d16d22f03f8fac746d4 /lib/sqfs | |
parent | 2d303a7f0a6076bbf5739bae4f0fa443d0da5203 (diff) |
Cleanup: move overflow safe alloc code into libsquashfs
There were only a hand full of instances outside libsquashfs that used
the alloc code. In most cases, the thing allocated hat its size derived
from something already in memory anyway, so it is safe to assume its
size fits into a size_t.
At the same time, the opencoded Windows path conversion functions are
all unified into a single helper function.
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/sqfs')
-rw-r--r-- | lib/sqfs/Makemodule.am | 1 | ||||
-rw-r--r-- | lib/sqfs/alloc.c | 37 | ||||
-rw-r--r-- | lib/sqfs/comp/internal.h | 2 | ||||
-rw-r--r-- | lib/sqfs/data_reader.c | 2 | ||||
-rw-r--r-- | lib/sqfs/data_writer/internal.h | 2 | ||||
-rw-r--r-- | lib/sqfs/dir_reader.c | 2 | ||||
-rw-r--r-- | lib/sqfs/dir_writer.c | 2 | ||||
-rw-r--r-- | lib/sqfs/meta_reader.c | 2 | ||||
-rw-r--r-- | lib/sqfs/meta_writer.c | 2 | ||||
-rw-r--r-- | lib/sqfs/read_inode.c | 2 | ||||
-rw-r--r-- | lib/sqfs/read_super.c | 2 | ||||
-rw-r--r-- | lib/sqfs/read_table.c | 2 | ||||
-rw-r--r-- | lib/sqfs/read_tree.c | 2 | ||||
-rw-r--r-- | lib/sqfs/str_table.c | 2 | ||||
-rw-r--r-- | lib/sqfs/util.h | 31 | ||||
-rw-r--r-- | lib/sqfs/write_table.c | 2 | ||||
-rw-r--r-- | lib/sqfs/xattr_reader.c | 2 | ||||
-rw-r--r-- | lib/sqfs/xattr_writer.c | 2 |
18 files changed, 84 insertions, 15 deletions
diff --git a/lib/sqfs/Makemodule.am b/lib/sqfs/Makemodule.am index be405cf..59cdbcb 100644 --- a/lib/sqfs/Makemodule.am +++ b/lib/sqfs/Makemodule.am @@ -24,6 +24,7 @@ libsquashfs_la_SOURCES += lib/sqfs/data_writer/internal.h lib/sqfs/data_reader.c libsquashfs_la_SOURCES += lib/sqfs/data_writer/common.c libsquashfs_la_SOURCES += lib/sqfs/data_writer/fileapi.c libsquashfs_la_SOURCES += lib/sqfs/str_table.c lib/sqfs/str_table.h +libsquashfs_la_SOURCES += lib/sqfs/alloc.c lib/sqfs/util.h libsquashfs_la_CPPFLAGS = $(AM_CPPFLAGS) libsquashfs_la_LDFLAGS = $(AM_LDFLAGS) libsquashfs_la_CFLAGS = $(AM_CFLAGS) $(PTHREAD_CFLAGS) $(ZLIB_CFLAGS) diff --git a/lib/sqfs/alloc.c b/lib/sqfs/alloc.c new file mode 100644 index 0000000..e8305d8 --- /dev/null +++ b/lib/sqfs/alloc.c @@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +/* + * alloc.c + * + * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at> + */ +#include "config.h" + +#include "util.h" + +#include <stdlib.h> +#include <errno.h> + +void *alloc_flex(size_t base_size, size_t item_size, size_t nmemb) +{ + size_t size; + + if (SZ_MUL_OV(nmemb, item_size, &size) || + SZ_ADD_OV(base_size, size, &size)) { + errno = EOVERFLOW; + return NULL; + } + + return calloc(1, size); +} + +void *alloc_array(size_t item_size, size_t nmemb) +{ + size_t size; + + if (SZ_MUL_OV(nmemb, item_size, &size)) { + errno = EOVERFLOW; + return NULL; + } + + return calloc(1, size); +} diff --git a/lib/sqfs/comp/internal.h b/lib/sqfs/comp/internal.h index dabf1a6..c687d41 100644 --- a/lib/sqfs/comp/internal.h +++ b/lib/sqfs/comp/internal.h @@ -14,7 +14,7 @@ #include "sqfs/error.h" #include "sqfs/block.h" #include "sqfs/io.h" -#include "util/util.h" +#include "../util.h" SQFS_INTERNAL int sqfs_generic_write_options(sqfs_file_t *file, const void *data, diff --git a/lib/sqfs/data_reader.c b/lib/sqfs/data_reader.c index b105096..6a0db35 100644 --- a/lib/sqfs/data_reader.c +++ b/lib/sqfs/data_reader.c @@ -14,7 +14,7 @@ #include "sqfs/table.h" #include "sqfs/inode.h" #include "sqfs/io.h" -#include "util/util.h" +#include "util.h" #include <stdlib.h> #include <string.h> diff --git a/lib/sqfs/data_writer/internal.h b/lib/sqfs/data_writer/internal.h index 9c64b6c..d6e26cd 100644 --- a/lib/sqfs/data_writer/internal.h +++ b/lib/sqfs/data_writer/internal.h @@ -16,7 +16,7 @@ #include "sqfs/error.h" #include "sqfs/block.h" #include "sqfs/io.h" -#include "util/util.h" +#include "../util.h" #include <string.h> #include <stdlib.h> diff --git a/lib/sqfs/dir_reader.c b/lib/sqfs/dir_reader.c index bae24d5..bda7d7a 100644 --- a/lib/sqfs/dir_reader.c +++ b/lib/sqfs/dir_reader.c @@ -14,7 +14,7 @@ #include "sqfs/inode.h" #include "sqfs/error.h" #include "sqfs/dir.h" -#include "util/util.h" +#include "util.h" #include <string.h> #include <stdlib.h> diff --git a/lib/sqfs/dir_writer.c b/lib/sqfs/dir_writer.c index 673c103..bdd5dd1 100644 --- a/lib/sqfs/dir_writer.c +++ b/lib/sqfs/dir_writer.c @@ -13,7 +13,7 @@ #include "sqfs/error.h" #include "sqfs/block.h" #include "sqfs/dir.h" -#include "util/util.h" +#include "util.h" #include <stdlib.h> #include <string.h> diff --git a/lib/sqfs/meta_reader.c b/lib/sqfs/meta_reader.c index 0b8b33a..19e856a 100644 --- a/lib/sqfs/meta_reader.c +++ b/lib/sqfs/meta_reader.c @@ -12,7 +12,7 @@ #include "sqfs/error.h" #include "sqfs/block.h" #include "sqfs/io.h" -#include "util/util.h" +#include "util.h" #include <stdlib.h> #include <string.h> diff --git a/lib/sqfs/meta_writer.c b/lib/sqfs/meta_writer.c index cdd34e8..acb2235 100644 --- a/lib/sqfs/meta_writer.c +++ b/lib/sqfs/meta_writer.c @@ -12,7 +12,7 @@ #include "sqfs/error.h" #include "sqfs/block.h" #include "sqfs/io.h" -#include "util/util.h" +#include "util.h" #include <string.h> #include <stdlib.h> diff --git a/lib/sqfs/read_inode.c b/lib/sqfs/read_inode.c index fa3ec31..11d7a1c 100644 --- a/lib/sqfs/read_inode.c +++ b/lib/sqfs/read_inode.c @@ -12,7 +12,7 @@ #include "sqfs/super.h" #include "sqfs/inode.h" #include "sqfs/dir.h" -#include "util/util.h" +#include "util.h" #include <stdlib.h> #include <string.h> diff --git a/lib/sqfs/read_super.c b/lib/sqfs/read_super.c index f5cc988..8b3f019 100644 --- a/lib/sqfs/read_super.c +++ b/lib/sqfs/read_super.c @@ -10,7 +10,7 @@ #include "sqfs/super.h" #include "sqfs/error.h" #include "sqfs/io.h" -#include "util/util.h" +#include "util.h" #include <string.h> diff --git a/lib/sqfs/read_table.c b/lib/sqfs/read_table.c index 747c8bc..096ac1a 100644 --- a/lib/sqfs/read_table.c +++ b/lib/sqfs/read_table.c @@ -12,7 +12,7 @@ #include "sqfs/table.h" #include "sqfs/block.h" #include "sqfs/io.h" -#include "util/util.h" +#include "util.h" #include <stdlib.h> diff --git a/lib/sqfs/read_tree.c b/lib/sqfs/read_tree.c index eaf7c16..5464595 100644 --- a/lib/sqfs/read_tree.c +++ b/lib/sqfs/read_tree.c @@ -15,7 +15,7 @@ #include "sqfs/inode.h" #include "sqfs/error.h" #include "sqfs/dir.h" -#include "util/util.h" +#include "util.h" #include <string.h> #include <stdlib.h> diff --git a/lib/sqfs/str_table.c b/lib/sqfs/str_table.c index 1ec0ef7..c0a364f 100644 --- a/lib/sqfs/str_table.c +++ b/lib/sqfs/str_table.c @@ -11,8 +11,8 @@ #include <string.h> #include "sqfs/error.h" -#include "util/util.h" #include "str_table.h" +#include "util.h" /* R5 hash function (borrowed from reiserfs) */ static sqfs_u32 strhash(const char *s) diff --git a/lib/sqfs/util.h b/lib/sqfs/util.h new file mode 100644 index 0000000..a379c0d --- /dev/null +++ b/lib/sqfs/util.h @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +/* + * util.h + * + * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at> + */ +#ifndef SQFS_UTIL_H +#define SQFS_UTIL_H + +#include "config.h" +#include "sqfs/predef.h" +#include "compat.h" + +#include <stddef.h> + +/* + Helper for allocating data structures with flexible array members. + + 'base_size' is the size of the struct itself, 'item_size' the size of a + single array element and 'nmemb' the number of elements. + + Iternally checks for arithmetic overflows when allocating the combined thing. + */ +SQFS_INTERNAL +void *alloc_flex(size_t base_size, size_t item_size, size_t nmemb); + +/* Basically the same as calloc, but *ALWAYS* does overflow checking */ +SQFS_INTERNAL +void *alloc_array(size_t item_size, size_t nmemb); + +#endif /* SQFS_UTIL_H */ diff --git a/lib/sqfs/write_table.c b/lib/sqfs/write_table.c index b8946a5..1931b83 100644 --- a/lib/sqfs/write_table.c +++ b/lib/sqfs/write_table.c @@ -13,7 +13,7 @@ #include "sqfs/table.h" #include "sqfs/block.h" #include "sqfs/io.h" -#include "util/util.h" +#include "util.h" #include <stdlib.h> diff --git a/lib/sqfs/xattr_reader.c b/lib/sqfs/xattr_reader.c index 250b4bd..d5b003b 100644 --- a/lib/sqfs/xattr_reader.c +++ b/lib/sqfs/xattr_reader.c @@ -14,7 +14,7 @@ #include "sqfs/error.h" #include "sqfs/block.h" #include "sqfs/io.h" -#include "util/util.h" +#include "util.h" #include <stdlib.h> #include <string.h> diff --git a/lib/sqfs/xattr_writer.c b/lib/sqfs/xattr_writer.c index 2e6a074..047e411 100644 --- a/lib/sqfs/xattr_writer.c +++ b/lib/sqfs/xattr_writer.c @@ -15,8 +15,8 @@ #include "sqfs/block.h" #include "sqfs/io.h" -#include "util/util.h" #include "str_table.h" +#include "util.h" #include <stdlib.h> #include <string.h> |