From 56048d13b73ec6e6a9c59d62e82f41e69a3994bd Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Tue, 25 Mar 2025 18:35:47 +0100 Subject: More safety with pointers using VLA function parameters --- src/solvers/tables.h | 49 ++++++++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 27 deletions(-) (limited to 'src/solvers/tables.h') diff --git a/src/solvers/tables.h b/src/solvers/tables.h index 65e7606..db888b5 100644 --- a/src/solvers/tables.h +++ b/src/solvers/tables.h @@ -1,11 +1,13 @@ -STATIC uint64_t read_unaligned_u64(const char *); -STATIC void write_unaligned_u64(char *, uint64_t); -STATIC int64_t readtableinfo(uint64_t, const char *, tableinfo_t *); -STATIC int64_t readtableinfo_n(uint64_t, const char *, uint8_t, tableinfo_t *); -STATIC int64_t writetableinfo(const tableinfo_t *, uint64_t, char *); +STATIC uint64_t read_unaligned_u64(const char [static sizeof(uint64_t)]); +STATIC void write_unaligned_u64(char [static sizeof(uint64_t)], uint64_t); +STATIC int64_t readtableinfo(size_t n, const char [n], tableinfo_t [static 1]); +STATIC int64_t readtableinfo_n( + size_t n, const char [n], uint8_t, tableinfo_t [static 1]); +STATIC int64_t writetableinfo( + const tableinfo_t [static 1], size_t n, char [n]); STATIC uint64_t -read_unaligned_u64(const char *buf) +read_unaligned_u64(const char buf[static sizeof(uint64_t)]) { uint64_t ret; @@ -15,13 +17,17 @@ read_unaligned_u64(const char *buf) } STATIC void -write_unaligned_u64(char *buf, uint64_t x) +write_unaligned_u64(char buf[static sizeof(uint64_t)], uint64_t x) { memcpy(buf, &x, sizeof(uint64_t)); } STATIC int64_t -readtableinfo(uint64_t buf_size, const char *buf, tableinfo_t *info) +readtableinfo( + size_t buf_size, + const char buf[buf_size], + tableinfo_t info[static 1] +) { size_t i; @@ -37,11 +43,6 @@ readtableinfo(uint64_t buf_size, const char *buf, tableinfo_t *info) return NISSY_ERROR_BUFFER_SIZE; } - if (info == NULL) { - LOG("Error reading table info: info struct is NULL\n"); - return NISSY_ERROR_UNKNOWN; - } - for (i = 0; i < INFO_DISTRIBUTION_LEN; i++) info->distribution[i] = read_unaligned_u64(OFFSET(buf, INFO_OFFSET_DISTRIBUTION + i * sizeof(uint64_t))); @@ -67,10 +68,10 @@ readtableinfo(uint64_t buf_size, const char *buf, tableinfo_t *info) STATIC int64_t readtableinfo_n( - uint64_t buf_size, - const char *buf, + size_t buf_size, + const char buf[buf_size], uint8_t n, - tableinfo_t *info + tableinfo_t info[static 1] ) { int64_t ret; @@ -83,22 +84,16 @@ readtableinfo_n( } STATIC int64_t -writetableinfo(const tableinfo_t *info, uint64_t data_size, char *buf) +writetableinfo( + const tableinfo_t info[static 1], + size_t data_size, + char buf[data_size] +) { size_t i; bool end; char *c; - if (buf == NULL) { - LOG("Error writing table: buffer is NULL\n"); - return NISSY_ERROR_NULL_POINTER; - } - - if (info == NULL) { - LOG("Error writing table info: provided info is NULL\n"); - return NISSY_ERROR_UNKNOWN; - } - if (data_size < info->fullsize) { LOG("Error writing table: buffer size is too small " "(given %" PRId64 " but table requires %" PRId64 ")\n", -- cgit v1.3