metric: don't allow scales that cause overflow #611

pull/632/head
nick black 4 years ago
parent c01d0b227f
commit 30f049d2e6
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC

@ -16,6 +16,9 @@ const char *ncmetric(uintmax_t val, uintmax_t decimal, char *buf, int omitdec,
if(decimal == 0 || mult == 0){
return NULL;
}
if(decimal > UINTMAX_MAX / 10){
return NULL;
}
dv = mult;
if(decimal <= val || val == 0){
// FIXME verify that input < 2^89, wish we had static_assert() :/

@ -387,15 +387,13 @@ TEST_CASE("Metric") {
impericize_ncmetric(1, 1000000000000000000ull, buf, 0, 1000, '\0');
CHECK(!strcmp("1.00a", buf));
/* FIXME
impericize_ncmetric(99, 1000000000000000000ull, buf, 0, 1000, '\0');
fprintf(stderr, "GOLD: %s buf: %s\n", "99.00a", buf);
CHECK(!strcmp("99.00a", buf));
impericize_ncmetric(19, 10000000000000000ull, buf, 0, 1000, '\0');
CHECK(!strcmp("1.89f", buf));
impericize_ncmetric(99, 10000000000000000ull, buf, 0, 1000, '\0');
CHECK(!strcmp("9.89f", buf));
impericize_ncmetric(100, 1000000000000000000ull, buf, 0, 1000, '\0');
fprintf(stderr, "GOLD: %s buf: %s\n", "100.00a", buf);
CHECK(!strcmp("100.00a", buf));
*/
impericize_ncmetric(100, 10000000000000000ull, buf, 0, 1000, '\0');
CHECK(!strcmp("10.00f", buf));
impericize_ncmetric(1, 10, buf, 0, 1000, '\0');
CHECK(!strcmp("100.00m", buf));
@ -417,7 +415,6 @@ fprintf(stderr, "GOLD: %s buf: %s\n", "100.00a", buf);
CHECK(!strcmp("100.00p", buf));
}
/*
SUBCASE("NegativePowersOfTen") {
char gold[PREFIXSTRLEN + 1];
char buf[PREFIXSTRLEN + 1];
@ -425,10 +422,9 @@ fprintf(stderr, "GOLD: %s buf: %s\n", "100.00a", buf);
uintmax_t val = 1;
size_t i = 0;
do{
qprefix(val, 1000000000000000000ull, buf, '\0');
const int sidx = i / 3 + 2;
REQUIRE(qprefix(val, 1000000000000000ull, buf, '\0'));
const int sidx = i / 3 + 3;
snprintf(gold, sizeof(gold), "%ju%s00%lc", goldval, decisep, smallsuffixes[sidx]);
fprintf(stderr, "GOLD: %s buf: %s val: %ju\n", gold, buf, val);
CHECK(!strcmp(gold, buf));
if(UINTMAX_MAX / val < 10){
break;
@ -437,10 +433,9 @@ fprintf(stderr, "GOLD: %s buf: %s val: %ju\n", gold, buf, val);
if((goldval *= 10) == 1000){
goldval = 1;
}
}while(++i < sizeof(smallsuffixes) / sizeof(*smallsuffixes) * 3);
}while(++i < (wcslen(smallsuffixes) - 3) * 3);
// If we ran through all our suffixes, that's a problem
CHECK(sizeof(smallsuffixes) / sizeof(*smallsuffixes) * 3 > i);
CHECK(sizeof(smallsuffixes) / sizeof(*smallsuffixes) * 3 > i);
}
*/
}

Loading…
Cancel
Save