Skip to content

Commit 82c56f6

Browse files
committed
manual commit: migrate bignum_to_string to snprintf
1 parent 78509e3 commit 82c56f6

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

bn.c

Lines changed: 3 additions & 3 deletions
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(nbytes), int nbytes)
127+
void bignum_to_string(_Ptr<struct bn> n, _Nt_array_ptr<char> str : count(nbytes), int nbytes)
128128
{
129129
require(n, "n is null");
130130
require(str, "str is null");
@@ -135,9 +135,9 @@ void bignum_to_string(_Ptr<struct bn> n, char *str : itype(_Array_ptr<char>) cou
135135
int i = 0; /* index into string representation. */
136136

137137
/* reading last array-element "MSB" first -> big endian */
138-
while ((j >= 0) && (nbytes > (i + 1)))
138+
while ((j >= 0) && (nbytes >= i + 2 * WORD_SIZE))
139139
{
140-
sprintf(&str[i], SPRINTF_FORMAT_STR, n->array[j]);
140+
snprintf(&str[i], nbytes - i + 1, SPRINTF_FORMAT_STR, n->array[j]);
141141
i += (2 * WORD_SIZE); /* step WORD_SIZE hex-byte(s) forward in the string. */
142142
j -= 1; /* step one element back in the array. */
143143
}

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(maxsize), int maxsize);
94+
void bignum_to_string(_Ptr<struct bn> n, _Nt_array_ptr<char> str : 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 */

0 commit comments

Comments
 (0)