aboutsummaryrefslogtreecommitdiff
path: root/src/solvers/tables.h
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2024-10-12 16:23:05 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2024-10-12 16:23:05 +0200
commit182e2d45678d0487be71370c37b88daca3d2c54b (patch)
tree3987ecd9bade082a7b41901c2376daa73401e548 /src/solvers/tables.h
parentda8fdd4955fd24666643915a6728678e9965a0d3 (diff)
downloadnissy-core-182e2d45678d0487be71370c37b88daca3d2c54b.tar.gz
nissy-core-182e2d45678d0487be71370c37b88daca3d2c54b.zip
Make gendata and co safer by checking buffer size
Diffstat (limited to 'src/solvers/tables.h')
-rw-r--r--src/solvers/tables.h84
1 files changed, 52 insertions, 32 deletions
diff --git a/src/solvers/tables.h b/src/solvers/tables.h
index d73fa31..060b018 100644
--- a/src/solvers/tables.h
+++ b/src/solvers/tables.h
@@ -1,14 +1,14 @@
1/* Type definitions and macros are in a separate file for easier testing */ 1/* Type definitions and macros are in a separate file for easier testing */
2#include "tables_types_macros.h" 2#include "tables_types_macros.h"
3 3
4STATIC uint64_t read_unaligned_u64(const void *); 4STATIC uint64_t read_unaligned_u64(const char *);
5STATIC void write_unaligned_u64(void *, uint64_t); 5STATIC void write_unaligned_u64(char *, uint64_t);
6STATIC bool readtableinfo(const void *, tableinfo_t *); 6STATIC int64_t readtableinfo(uint64_t, const char *, tableinfo_t *);
7STATIC bool readtableinfo_n(const void *, uint8_t, tableinfo_t *); 7STATIC int64_t readtableinfo_n(uint64_t, const char *, uint8_t, tableinfo_t *);
8STATIC bool writetableinfo(const tableinfo_t *, void *); 8STATIC int64_t writetableinfo(const tableinfo_t *, uint64_t, char *);
9 9
10STATIC uint64_t 10STATIC uint64_t
11read_unaligned_u64(const void *buf) 11read_unaligned_u64(const char *buf)
12{ 12{
13 uint64_t ret; 13 uint64_t ret;
14 14
@@ -18,24 +18,30 @@ read_unaligned_u64(const void *buf)
18} 18}
19 19
20STATIC void 20STATIC void
21write_unaligned_u64(void *buf, uint64_t x) 21write_unaligned_u64(char *buf, uint64_t x)
22{ 22{
23 memcpy(buf, &x, sizeof(uint64_t)); 23 memcpy(buf, &x, sizeof(uint64_t));
24} 24}
25 25
26STATIC bool 26STATIC int64_t
27readtableinfo(const void *buf, tableinfo_t *info) 27readtableinfo(uint64_t buf_size, const char *buf, tableinfo_t *info)
28{ 28{
29 size_t i; 29 size_t i;
30 30
31 if (buf == NULL) { 31 if (buf == NULL) {
32 LOG("Error reading table: buffer is NULL\n"); 32 LOG("Error reading table: buffer is NULL\n");
33 return false; 33 return NISSY_ERROR_NULL_POINTER;
34 }
35
36 if (buf_size < INFOSIZE) {
37 LOG("Error reading table: buffer size is too small "
38 "(smaller than INFOSIZE = %" PRId64 ")\n", INFOSIZE);
39 return NISSY_ERROR_BUFFER_SIZE;
34 } 40 }
35 41
36 if (info == NULL) { 42 if (info == NULL) {
37 LOG("Error reading table info: info struct is NULL\n"); 43 LOG("Error reading table info: info struct is NULL\n");
38 return false; 44 return NISSY_ERROR_UNKNOWN;
39 } 45 }
40 46
41 for (i = 0; i < INFO_DISTRIBUTION_LEN; i++) 47 for (i = 0; i < INFO_DISTRIBUTION_LEN; i++)
@@ -53,39 +59,53 @@ readtableinfo(const void *buf, tableinfo_t *info)
53 memcpy(info->solver, OFFSET(buf, INFO_OFFSET_SOLVER), 59 memcpy(info->solver, OFFSET(buf, INFO_OFFSET_SOLVER),
54 INFO_SOLVER_STRLEN); 60 INFO_SOLVER_STRLEN);
55 61
56 info->h48h = *OFFSET(buf, INFO_OFFSET_H48H); 62 info->h48h = *(uint8_t *)OFFSET(buf, INFO_OFFSET_H48H);
57 info->bits = *OFFSET(buf, INFO_OFFSET_BITS); 63 info->bits = *(uint8_t *)OFFSET(buf, INFO_OFFSET_BITS);
58 info->base = *OFFSET(buf, INFO_OFFSET_BASE); 64 info->base = *(uint8_t *)OFFSET(buf, INFO_OFFSET_BASE);
59 info->maxvalue = *OFFSET(buf, INFO_OFFSET_MAXVALUE); 65 info->maxvalue = *(uint8_t *)OFFSET(buf, INFO_OFFSET_MAXVALUE);
60 66
61 return true; 67 return NISSY_OK;
62} 68}
63 69
64STATIC bool 70STATIC int64_t
65readtableinfo_n(const void *buf, uint8_t n, tableinfo_t *info) 71readtableinfo_n(
72 uint64_t buf_size,
73 const char *buf,
74 uint8_t n,
75 tableinfo_t *info
76)
66{ 77{
67 for ( ; n > 0; n--, buf = (char *)buf + info->next) 78 int64_t ret;
68 if (!readtableinfo(buf, info)) 79
69 return false; 80 for (; n > 0; n--, buf = buf + info->next, buf_size -= info->next)
81 if ((ret = readtableinfo(buf_size, buf, info)) != 0)
82 return ret;
70 83
71 return true; 84 return NISSY_OK;
72} 85}
73 86
74STATIC bool 87STATIC int64_t
75writetableinfo(const tableinfo_t *info, void *buf) 88writetableinfo(const tableinfo_t *info, uint64_t data_size, char *buf)
76{ 89{
77 size_t i; 90 size_t i;
78 bool end; 91 bool end;
79 uint8_t *c; 92 char *c;
80 93
81 if (buf == NULL) { 94 if (buf == NULL) {
82 LOG("Error writing table: buffer is NULL\n"); 95 LOG("Error writing table: buffer is NULL\n");
83 return false; 96 return NISSY_ERROR_NULL_POINTER;
84 } 97 }
85 98
86 if (info == NULL) { 99 if (info == NULL) {
87 LOG("Error writing table info: provided info is NULL\n"); 100 LOG("Error writing table info: provided info is NULL\n");
88 return false; 101 return NISSY_ERROR_UNKNOWN;
102 }
103
104 if (data_size < info->fullsize) {
105 LOG("Error writing table: buffer size is too small "
106 "(given %" PRId64 " but table requires %" PRId64 ")\n",
107 data_size, info->fullsize);
108 return NISSY_ERROR_BUFFER_SIZE;
89 } 109 }
90 110
91 for (i = 0; i < INFO_DISTRIBUTION_LEN; i++) 111 for (i = 0; i < INFO_DISTRIBUTION_LEN; i++)
@@ -112,10 +132,10 @@ writetableinfo(const tableinfo_t *info, void *buf)
112 *c = 0; 132 *c = 0;
113 } 133 }
114 134
115 *OFFSET(buf, INFO_OFFSET_H48H) = info->h48h; 135 *(uint8_t *)OFFSET(buf, INFO_OFFSET_H48H) = info->h48h;
116 *OFFSET(buf, INFO_OFFSET_BITS) = info->bits; 136 *(uint8_t *)OFFSET(buf, INFO_OFFSET_BITS) = info->bits;
117 *OFFSET(buf, INFO_OFFSET_BASE) = info->base; 137 *(uint8_t *)OFFSET(buf, INFO_OFFSET_BASE) = info->base;
118 *OFFSET(buf, INFO_OFFSET_MAXVALUE) = info->maxvalue; 138 *(uint8_t *)OFFSET(buf, INFO_OFFSET_MAXVALUE) = info->maxvalue;
119 139
120 return true; 140 return NISSY_OK;
121} 141}

Generated with cgit - Back to sebastiano.tronto.net