use size_t for mp_radix_size
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
diff --git a/demo/shared.c b/demo/shared.c
index a4ac833..834b3a9 100644
--- a/demo/shared.c
+++ b/demo/shared.c
@@ -3,19 +3,19 @@
void ndraw(const mp_int *a, const char *name)
{
char *buf;
- int size;
+ size_t size;
mp_radix_size(a, 10, &size);
- buf = (char *)malloc((size_t) size);
+ buf = (char *)malloc(size);
if (buf == NULL) {
- fprintf(stderr, "\nndraw: malloc(%d) failed\n", size);
+ fprintf(stderr, "\nndraw: malloc(%zu) failed\n", size);
exit(EXIT_FAILURE);
}
printf("%s: ", name);
- mp_to_decimal(a, buf, (size_t) size);
+ mp_to_decimal(a, buf, size);
printf("%s\n", buf);
- mp_to_hex(a, buf, (size_t) size);
+ mp_to_hex(a, buf, size);
printf("0x%s\n", buf);
free(buf);
diff --git a/demo/test.c b/demo/test.c
index acb81c5..838f0ed 100644
--- a/demo/test.c
+++ b/demo/test.c
@@ -2275,9 +2275,10 @@ static int test_mp_radix_size(void)
{
mp_err err;
mp_int a;
- int radix, size;
+ int radix;
+ size_t size;
/* *INDENT-OFF* */
- int results[65] = {
+ size_t results[65] = {
0, 0, 1627, 1027, 814, 702, 630, 581, 543,
514, 491, 471, 455, 441, 428, 418, 408, 399,
391, 384, 378, 372, 366, 361, 356, 352, 347,
@@ -2302,7 +2303,7 @@ static int test_mp_radix_size(void)
goto LBL_ERR;
}
if (size != results[radix]) {
- fprintf(stderr, "mp_radix_size: result for base %d was %d instead of %d\n",
+ fprintf(stderr, "mp_radix_size: result for base %d was %zu instead of %zu\n",
radix, size, results[radix]);
goto LBL_ERR;
}
@@ -2311,7 +2312,7 @@ static int test_mp_radix_size(void)
goto LBL_ERR;
}
if (size != (results[radix] + 1)) {
- fprintf(stderr, "mp_radix_size: result for base %d was %d instead of %d\n",
+ fprintf(stderr, "mp_radix_size: result for base %d was %zu instead of %zu\n",
radix, size, results[radix]);
goto LBL_ERR;
}
diff --git a/mp_fwrite.c b/mp_fwrite.c
index f9d3ab0..be78f7f 100644
--- a/mp_fwrite.c
+++ b/mp_fwrite.c
@@ -8,24 +8,19 @@ mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream)
{
char *buf;
mp_err err;
- int len;
- size_t written;
+ size_t len, written;
/* TODO: this function is not in this PR */
- if (MP_HAS(MP_RADIX_SIZE_OVERESTIMATE)) {
- /* if ((err = mp_radix_size_overestimate(&t, base, &len)) != MP_OKAY) goto LBL_ERR; */
- } else {
- if ((err = mp_radix_size(a, radix, &len)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_radix_size(a, radix, &len)) != MP_OKAY) {
+ return err;
}
- buf = (char *) MP_MALLOC((size_t)len);
+ buf = (char *) MP_MALLOC(len);
if (buf == NULL) {
return MP_MEM;
}
- if ((err = mp_to_radix(a, buf, (size_t)len, &written, radix)) != MP_OKAY) {
+ if ((err = mp_to_radix(a, buf, len, &written, radix)) != MP_OKAY) {
goto LBL_ERR;
}
@@ -37,7 +32,7 @@ mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream)
LBL_ERR:
- MP_FREE_BUFFER(buf, (size_t)len);
+ MP_FREE_BUFFER(buf, len);
return err;
}
#endif
diff --git a/mp_radix_size.c b/mp_radix_size.c
index e9cfb9b..6c3e582 100644
--- a/mp_radix_size.c
+++ b/mp_radix_size.c
@@ -4,7 +4,7 @@
/* SPDX-License-Identifier: Unlicense */
/* returns size of ASCII representation */
-mp_err mp_radix_size(const mp_int *a, int radix, int *size)
+mp_err mp_radix_size(const mp_int *a, int radix, size_t *size)
{
mp_err err;
mp_int a_;
@@ -26,10 +26,8 @@ mp_err mp_radix_size(const mp_int *a, int radix, int *size)
goto LBL_ERR;
}
- *size = (int)b;
-
/* mp_ilogb truncates to zero, hence we need one extra put on top and one for `\0`. */
- *size += 2 + ((a->sign == MP_NEG) ? 1 : 0);
+ *size = (size_t)b + 2U + ((a->sign == MP_NEG) ? 1U : 0U);
LBL_ERR:
return err;
diff --git a/tommath.h b/tommath.h
index 0aac377..a401be4 100644
--- a/tommath.h
+++ b/tommath.h
@@ -588,7 +588,7 @@ mp_err mp_to_sbin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *wr
mp_err mp_read_radix(mp_int *a, const char *str, int radix) MP_WUR;
mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *written, int radix) MP_WUR;
-mp_err mp_radix_size(const mp_int *a, int radix, int *size) MP_WUR;
+mp_err mp_radix_size(const mp_int *a, int radix, size_t *size) MP_WUR;
#ifndef MP_NO_FILE
mp_err mp_fread(mp_int *a, int radix, FILE *stream) MP_WUR;