Skip to content

Commit 78509e3

Browse files
committed
manual commit: fix bignum_to_string buffer lengths
1 parent d568a2c commit 78509e3

File tree

7 files changed

+40
-40
lines changed

7 files changed

+40
-40
lines changed

bn.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ void bignum_from_string(_Ptr<struct bn> n, _Nt_array_ptr<char> str : count(nbyte
124124
}
125125

126126

127-
void bignum_to_string(_Ptr<struct bn> n, char *str : itype(_Array_ptr<char>) count(8191), int nbytes)
127+
void bignum_to_string(_Ptr<struct bn> n, char *str : itype(_Array_ptr<char>) count(nbytes), int nbytes)
128128
{
129129
require(n, "n is null");
130130
require(str, "str is null");

bn.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void bignum_init(_Ptr<struct bn> n);
9191
void bignum_from_int(_Ptr<struct bn> n, DTYPE_TMP i);
9292
int bignum_to_int(_Ptr<struct bn> n);
9393
void bignum_from_string(_Ptr<struct bn> n, _Nt_array_ptr<char> str : count(nbytes), int nbytes);
94-
void bignum_to_string(_Ptr<struct bn> n, char *str : itype(_Array_ptr<char>) count(8191), int maxsize);
94+
void bignum_to_string(_Ptr<struct bn> n, char *str : itype(_Array_ptr<char>) count(maxsize), int maxsize);
9595

9696
/* Basic arithmetic operations: */
9797
void bignum_add(_Ptr<struct bn> a, _Ptr<struct bn> b, _Ptr<struct bn> c); /* c = a + b */

tests/factorial.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ int main(int argc, _Array_ptr<_Nt_array_ptr<char>> argv : count(argc))
6666
{
6767
struct bn num = {};
6868
struct bn result = {};
69-
char buf _Nt_checked[8192];
69+
char buf _Nt_checked[8193];
7070

7171
bignum_from_int(&num, 100);
7272
factorial(&num, &result);
73-
bignum_to_string(&result, buf, sizeof(buf));
73+
bignum_to_string(&result, buf, sizeof(buf)-1);
7474
printf("factorial(100) using bignum = %s\n", buf);
7575

7676
return 0;

tests/golden.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ struct bn sc = {};
193193
struct bn sd = {};
194194
uint32_t ia, ib, ic;
195195
char op;
196-
char buf _Nt_checked[8192];
196+
char buf _Nt_checked[8193];
197197
int npassed = 0;
198198
int test_passed;
199199

@@ -256,13 +256,13 @@ struct bn sd = {};
256256
}
257257
else
258258
{
259-
bignum_to_string(&sa, buf, sizeof(buf));
259+
bignum_to_string(&sa, buf, sizeof(buf)-1);
260260
printf(" a = %s \n", buf);
261-
bignum_to_string(&sb, buf, sizeof(buf));
261+
bignum_to_string(&sb, buf, sizeof(buf)-1);
262262
printf(" b = %s \n", buf);
263-
bignum_to_string(&sc, buf, sizeof(buf));
263+
bignum_to_string(&sc, buf, sizeof(buf)-1);
264264
printf(" c = %s \n", buf);
265-
bignum_to_string(&sd, buf, sizeof(buf));
265+
bignum_to_string(&sd, buf, sizeof(buf)-1);
266266
printf(" d = %s \n", buf);
267267
printf("\n");
268268
}

tests/load_cmp.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515

1616
int main(int argc, _Array_ptr<_Nt_array_ptr<char>> argv : count(argc))
1717
{
18-
char sabuf _Nt_checked[8192];
19-
char sbbuf _Nt_checked[8192];
20-
char scbuf _Nt_checked[8192];
21-
char sdbuf _Nt_checked[8192];
22-
char iabuf _Nt_checked[8192];
23-
char ibbuf _Nt_checked[8192];
24-
char icbuf _Nt_checked[8192];
25-
char idbuf _Nt_checked[8192];
18+
char sabuf _Nt_checked[8193];
19+
char sbbuf _Nt_checked[8193];
20+
char scbuf _Nt_checked[8193];
21+
char sdbuf _Nt_checked[8193];
22+
char iabuf _Nt_checked[8193];
23+
char ibbuf _Nt_checked[8193];
24+
char icbuf _Nt_checked[8193];
25+
char idbuf _Nt_checked[8193];
2626

2727
struct bn sa = {};
2828
struct bn sb = {};
@@ -80,15 +80,15 @@ struct bn id = {};
8080

8181
printf("Verifying to_string function.\n");
8282

83-
bignum_to_string(&sa, sabuf, sizeof(sabuf));
84-
bignum_to_string(&sb, sbbuf, sizeof(sbbuf));
85-
bignum_to_string(&sc, scbuf, sizeof(scbuf));
86-
bignum_to_string(&sd, sdbuf, sizeof(sdbuf));
83+
bignum_to_string(&sa, sabuf, sizeof(sabuf)-1);
84+
bignum_to_string(&sb, sbbuf, sizeof(sbbuf)-1);
85+
bignum_to_string(&sc, scbuf, sizeof(scbuf)-1);
86+
bignum_to_string(&sd, sdbuf, sizeof(sdbuf)-1);
8787

88-
bignum_to_string(&ia, iabuf, sizeof(iabuf));
89-
bignum_to_string(&ib, ibbuf, sizeof(ibbuf));
90-
bignum_to_string(&ic, icbuf, sizeof(icbuf));
91-
bignum_to_string(&id, idbuf, sizeof(idbuf));
88+
bignum_to_string(&ia, iabuf, sizeof(iabuf)-1);
89+
bignum_to_string(&ib, ibbuf, sizeof(ibbuf)-1);
90+
bignum_to_string(&ic, icbuf, sizeof(icbuf)-1);
91+
bignum_to_string(&id, idbuf, sizeof(idbuf)-1);
9292

9393
assert(strcmp(sabuf, iabuf) == 0);
9494
assert(strcmp(sbbuf, ibbuf) == 0);

tests/randomized.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ struct bn b_before = {};
8383

8484
if (!cmp_result)
8585
{
86-
char buf _Nt_checked[8192];
87-
bignum_to_string(&res, buf, sizeof(buf));
86+
char buf _Nt_checked[8193];
87+
bignum_to_string(&res, buf, sizeof(buf)-1);
8888
printf("\ngot %s\n", buf);
8989
printf(" a = %d \n", bignum_to_int(&a));
9090
printf(" b = %d \n", bignum_to_int(&b));

tests/rsa.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void pow_mod_faster(_Ptr<struct bn> a, _Ptr<struct bn> b, _Ptr<struct bn> n, _Pt
7272
static void test_rsa_1(void)
7373
{
7474
/* Testing with very small and simple terms */
75-
char buf _Nt_checked[8192];
75+
char buf _Nt_checked[8193];
7676
struct bn M = {};
7777
struct bn C = {};
7878
struct bn E = {};
@@ -109,7 +109,7 @@ struct bn N = {};
109109
bignum_from_int(&N, n);
110110
pow_mod_faster(&M, &E, &N, &C);
111111
c_result = bignum_to_int(&C);
112-
bignum_to_string(&C, buf, sizeof(buf));
112+
bignum_to_string(&C, buf, sizeof(buf)-1);
113113
printf(" %d ^ %d mod %d = %d \n", m, e, n, c_result);
114114
printf(" %d ^ %d mod %d = %s \n", m, e, n, buf);
115115

@@ -119,7 +119,7 @@ struct bn N = {};
119119
printf(" %d ^ %d mod %d = %d ? \n", c, d, n, m);
120120
pow_mod_faster(&C, &D, &N, &M);
121121
m_result = bignum_to_int(&M);
122-
bignum_to_string(&M, buf, sizeof(buf));
122+
bignum_to_string(&M, buf, sizeof(buf)-1);
123123
printf(" %d ^ %d mod %d = %d \n", c, d, n, m_result);
124124
printf(" %d ^ %d mod %d = %s \n", c, d, n, buf);
125125

@@ -132,7 +132,7 @@ struct bn N = {};
132132

133133
void test_rsa_2(void)
134134
{
135-
char buf _Nt_checked[8192];
135+
char buf _Nt_checked[8193];
136136
struct bn M = {};
137137
struct bn C = {};
138138
struct bn E = {};
@@ -169,7 +169,7 @@ struct bn N = {};
169169
bignum_from_int(&N, n);
170170
pow_mod_faster(&M, &E, &N, &C);
171171
c_result = bignum_to_int(&C);
172-
bignum_to_string(&C, buf, sizeof(buf));
172+
bignum_to_string(&C, buf, sizeof(buf)-1);
173173
printf(" %d ^ %d mod %d = %d \n", m, e, n, c_result);
174174
printf(" %d ^ %d mod %d = %s \n", m, e, n, buf);
175175

@@ -179,7 +179,7 @@ struct bn N = {};
179179
printf(" %d ^ %d mod %d = %d ? \n", c, d, n, m);
180180
pow_mod_faster(&C, &D, &N, &M);
181181
m_result = bignum_to_int(&M);
182-
bignum_to_string(&M, buf, sizeof(buf));
182+
bignum_to_string(&M, buf, sizeof(buf)-1);
183183
printf(" %d ^ %d mod %d = %s \n", c, d, n, buf);
184184
printf(" %d ^ %d mod %d = %d \n", c, d, n, m_result);
185185

@@ -189,7 +189,7 @@ struct bn N = {};
189189

190190
void test_rsa_3(void)
191191
{
192-
char buf _Nt_checked[8192];
192+
char buf _Nt_checked[8193];
193193
struct bn M = {};
194194
struct bn C = {};
195195
struct bn E = {};
@@ -226,7 +226,7 @@ struct bn N = {};
226226
bignum_from_int(&N, n);
227227
pow_mod_faster(&M, &E, &N, &C);
228228
c_result = bignum_to_int(&C);
229-
bignum_to_string(&C, buf, sizeof(buf));
229+
bignum_to_string(&C, buf, sizeof(buf)-1);
230230
printf(" %d ^ %d mod %d = %d \n", m, e, n, c_result);
231231
printf(" %d ^ %d mod %d = %s \n", m, e, n, buf);
232232

@@ -236,7 +236,7 @@ struct bn N = {};
236236
printf(" %d ^ %d mod %d = %d ? \n", c, d, n, m);
237237
pow_mod_faster(&C, &D, &N, &M);
238238
m_result = bignum_to_int(&M);
239-
bignum_to_string(&M, buf, sizeof(buf));
239+
bignum_to_string(&M, buf, sizeof(buf)-1);
240240
printf(" %d ^ %d mod %d = %s \n", c, d, n, buf);
241241
printf(" %d ^ %d mod %d = %d \n", c, d, n, m_result);
242242

@@ -250,7 +250,7 @@ static void test_rsa1024(void)
250250
{
251251
char public _Nt_checked[] = "a15f36fc7f8d188057fc51751962a5977118fa2ad4ced249c039ce36c8d1bd275273f1edd821892fa75680b1ae38749fff9268bf06b3c2af02bbdb52a0d05c2ae2384aa1002391c4b16b87caea8296cfd43757bb51373412e8fe5df2e56370505b692cf8d966e3f16bc62629874a0464a9710e4a0718637a68442e0eb1648ec5";
252252
char private _Nt_checked[] = "3f5cc8956a6bf773e598604faf71097e265d5d55560c038c0bdb66ba222e20ac80f69fc6f93769cb795440e2037b8d67898d6e6d9b6f180169fc6348d5761ac9e81f6b8879529bc07c28dc92609eb8a4d15ac4ba3168a331403c689b1e82f62518c38601d58fd628fcb7009f139fb98e61ef7a23bee4e3d50af709638c24133d";
253-
char buf _Nt_checked[8192];
253+
char buf _Nt_checked[8193];
254254

255255
struct bn n = {}; /* public key */
256256
struct bn d = {}; /* private key */
@@ -276,7 +276,7 @@ static void test_rsa1024(void)
276276
bignum_init(&c);
277277

278278
bignum_from_int(&m, x);
279-
bignum_to_string(&m, buf, sizeof(buf));
279+
bignum_to_string(&m, buf, sizeof(buf)-1);
280280
printf("m = %s \n", buf);
281281

282282
//printf(" Copied %d bytes into m\n", i);
@@ -285,7 +285,7 @@ static void test_rsa1024(void)
285285
pow_mod_faster(&m, &e, &n, &c);
286286
printf(" Done...\n\n");
287287

288-
bignum_to_string(&c, buf, sizeof(buf));
288+
bignum_to_string(&c, buf, sizeof(buf)-1);
289289
printf(" Decrypting cipher text '");
290290
int i = 0;
291291
while (buf[i] != 0)
@@ -302,7 +302,7 @@ static void test_rsa1024(void)
302302
printf(" Done...\n\n");
303303

304304

305-
bignum_to_string(&m, buf, sizeof(buf));
305+
bignum_to_string(&m, buf, sizeof(buf)-1);
306306
printf("m = %s \n", buf);
307307
}
308308

0 commit comments

Comments
 (0)