Skip to content

Commit d568a2c

Browse files
committed
manual commit: migrate bignum_from_string
1 parent 4a5569a commit d568a2c

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

bn.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ void bignum_from_string(_Ptr<struct bn> n, _Nt_array_ptr<char> str : count(nbyte
114114
while (i >= 0)
115115
{
116116
tmp = 0;
117-
sscanf(&str[i], SSCANF_FORMAT_STR, &tmp);
117+
_Nt_array_ptr<char> read_pos =
118+
_Dynamic_bounds_cast<_Nt_array_ptr<char>>(str + i, count(0));
119+
_Unchecked { sscanf(read_pos, SSCANF_FORMAT_STR, &tmp); }
118120
n->array[j] = tmp;
119121
i -= (2 * WORD_SIZE); /* step WORD_SIZE hex-byte(s) back in the string. */
120122
j += 1; /* step one element forward in the array. */

tests/randomized.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
#include <string.h>
55
#include "bn.h"
66

7+
void bignum_from_string_using_strlen(_Ptr<struct bn> n, _Nt_array_ptr<char> str) {
8+
int len = strlen(str);
9+
_Nt_array_ptr<char> str2 : count(len) =
10+
_Assume_bounds_cast<_Nt_array_ptr<char>>(str, count(len));
11+
bignum_from_string(n, str2, len);
12+
}
13+
714
enum { ADD, SUB, MUL, DIV, AND, OR, XOR, POW, MOD, RSHFT, LSHFT, ISQRT };
815

916
int main(int argc, _Array_ptr<_Nt_array_ptr<char>> argv : count(argc))
@@ -35,9 +42,9 @@ struct bn res = {};
3542
bignum_init(&b);
3643
bignum_init(&c);
3744
bignum_init(&res);
38-
bignum_from_string(&a, argv[2], strlen(argv[2]));
39-
bignum_from_string(&b, argv[3], strlen(argv[3]));
40-
bignum_from_string(&c, argv[4], strlen(argv[4]));
45+
bignum_from_string_using_strlen(&a, argv[2]);
46+
bignum_from_string_using_strlen(&b, argv[3]);
47+
bignum_from_string_using_strlen(&c, argv[4]);
4148

4249
struct bn a_before = {};
4350
struct bn b_before = {};

0 commit comments

Comments
 (0)