aboutsummaryrefslogtreecommitdiff
path: root/src/solvers/tables.h
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2025-03-25 18:35:47 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2025-03-25 18:35:47 +0100
commit56048d13b73ec6e6a9c59d62e82f41e69a3994bd (patch)
tree49fc5d50e739a399897425756c08a73ba5924ddb /src/solvers/tables.h
parent0f4931c8de298b0f97aba1757c9538f48adb30c6 (diff)
downloadnissy-core-56048d13b73ec6e6a9c59d62e82f41e69a3994bd.tar.gz
nissy-core-56048d13b73ec6e6a9c59d62e82f41e69a3994bd.zip
More safety with pointers using VLA function parameters
Diffstat (limited to 'src/solvers/tables.h')
-rw-r--r--src/solvers/tables.h49
1 files changed, 22 insertions, 27 deletions
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 @@
1STATIC uint64_t read_unaligned_u64(const char *); 1STATIC uint64_t read_unaligned_u64(const char [static sizeof(uint64_t)]);
2STATIC void write_unaligned_u64(char *, uint64_t); 2STATIC void write_unaligned_u64(char [static sizeof(uint64_t)], uint64_t);
3STATIC int64_t readtableinfo(uint64_t, const char *, tableinfo_t *); 3STATIC int64_t readtableinfo(size_t n, const char [n], tableinfo_t [static 1]);
4STATIC int64_t readtableinfo_n(uint64_t, const char *, uint8_t, tableinfo_t *); 4STATIC int64_t readtableinfo_n(
5STATIC int64_t writetableinfo(const tableinfo_t *, uint64_t, char *); 5 size_t n, const char [n], uint8_t, tableinfo_t [static 1]);
6STATIC int64_t writetableinfo(
7 const tableinfo_t [static 1], size_t n, char [n]);
6 8
7STATIC uint64_t 9STATIC uint64_t
8read_unaligned_u64(const char *buf) 10read_unaligned_u64(const char buf[static sizeof(uint64_t)])
9{ 11{
10 uint64_t ret; 12 uint64_t ret;
11 13
@@ -15,13 +17,17 @@ read_unaligned_u64(const char *buf)
15} 17}
16 18
17STATIC void 19STATIC void
18write_unaligned_u64(char *buf, uint64_t x) 20write_unaligned_u64(char buf[static sizeof(uint64_t)], uint64_t x)
19{ 21{
20 memcpy(buf, &x, sizeof(uint64_t)); 22 memcpy(buf, &x, sizeof(uint64_t));
21} 23}
22 24
23STATIC int64_t 25STATIC int64_t
24readtableinfo(uint64_t buf_size, const char *buf, tableinfo_t *info) 26readtableinfo(
27 size_t buf_size,
28 const char buf[buf_size],
29 tableinfo_t info[static 1]
30)
25{ 31{
26 size_t i; 32 size_t i;
27 33
@@ -37,11 +43,6 @@ readtableinfo(uint64_t buf_size, const char *buf, tableinfo_t *info)
37 return NISSY_ERROR_BUFFER_SIZE; 43 return NISSY_ERROR_BUFFER_SIZE;
38 } 44 }
39 45
40 if (info == NULL) {
41 LOG("Error reading table info: info struct is NULL\n");
42 return NISSY_ERROR_UNKNOWN;
43 }
44
45 for (i = 0; i < INFO_DISTRIBUTION_LEN; i++) 46 for (i = 0; i < INFO_DISTRIBUTION_LEN; i++)
46 info->distribution[i] = read_unaligned_u64(OFFSET(buf, 47 info->distribution[i] = read_unaligned_u64(OFFSET(buf,
47 INFO_OFFSET_DISTRIBUTION + i * sizeof(uint64_t))); 48 INFO_OFFSET_DISTRIBUTION + i * sizeof(uint64_t)));
@@ -67,10 +68,10 @@ readtableinfo(uint64_t buf_size, const char *buf, tableinfo_t *info)
67 68
68STATIC int64_t 69STATIC int64_t
69readtableinfo_n( 70readtableinfo_n(
70 uint64_t buf_size, 71 size_t buf_size,
71 const char *buf, 72 const char buf[buf_size],
72 uint8_t n, 73 uint8_t n,
73 tableinfo_t *info 74 tableinfo_t info[static 1]
74) 75)
75{ 76{
76 int64_t ret; 77 int64_t ret;
@@ -83,22 +84,16 @@ readtableinfo_n(
83} 84}
84 85
85STATIC int64_t 86STATIC int64_t
86writetableinfo(const tableinfo_t *info, uint64_t data_size, char *buf) 87writetableinfo(
88 const tableinfo_t info[static 1],
89 size_t data_size,
90 char buf[data_size]
91)
87{ 92{
88 size_t i; 93 size_t i;
89 bool end; 94 bool end;
90 char *c; 95 char *c;
91 96
92 if (buf == NULL) {
93 LOG("Error writing table: buffer is NULL\n");
94 return NISSY_ERROR_NULL_POINTER;
95 }
96
97 if (info == NULL) {
98 LOG("Error writing table info: provided info is NULL\n");
99 return NISSY_ERROR_UNKNOWN;
100 }
101
102 if (data_size < info->fullsize) { 97 if (data_size < info->fullsize) {
103 LOG("Error writing table: buffer size is too small " 98 LOG("Error writing table: buffer size is too small "
104 "(given %" PRId64 " but table requires %" PRId64 ")\n", 99 "(given %" PRId64 " but table requires %" PRId64 ")\n",

Generated with cgit - Back to sebastiano.tronto.net