ref: 503c3b53649cc74218b281c35ff423d1fbdf83bf
parent: 992be3fd9bd5fd62d2955bb494e14ce5e47c73d4
author: Rangi <[email protected]>
date: Fri Apr 16 18:19:37 EDT 2021
Revert "Fix interpolation/STRFMT overflow issues" This reverts commit 992be3fd9bd5fd62d2955bb494e14ce5e47c73d4.
--- a/include/asm/format.h
+++ b/include/asm/format.h
@@ -28,9 +28,9 @@
bool prefix;
bool alignLeft;
bool padZero;
- size_t width;
+ uint8_t width;
bool hasFrac;
- size_t fracWidth;
+ uint8_t fracWidth;
int type;
bool valid;
};
--- a/src/asm/format.c
+++ b/src/asm/format.c
@@ -147,18 +147,14 @@
size_t len = strlen(value);
size_t totalLen = fmt->width > len ? fmt->width : len;
- size_t padLen = fmt->width > len ? fmt->width - len : 0;
- if (totalLen + 1 > bufLen) { /* bufLen includes terminator */
+ if (totalLen + 1 > bufLen) /* bufLen includes terminator */
error("Formatted string value too long\n");
- totalLen = bufLen - 1;
- if (len > totalLen)
- len = totalLen;
- padLen = totalLen - len;
- }
+ size_t padLen = fmt->width > len ? fmt->width - len : 0;
+
if (fmt->alignLeft) {
- memcpy(buf, value, len < bufLen ? len : bufLen);
+ strncpy(buf, value, len < bufLen ? len : bufLen);
for (size_t i = 0; i < totalLen && len + i < bufLen; i++)
buf[len + i] = ' ';
} else {
@@ -165,7 +161,7 @@
for (size_t i = 0; i < padLen && i < bufLen; i++)
buf[i] = ' ';
if (bufLen > padLen)
- memcpy(buf + padLen, value, bufLen - padLen - 1);
+ strncpy(buf + padLen, value, bufLen - padLen - 1);
}
buf[totalLen] = '\0';
@@ -225,18 +221,12 @@
/* Special case for fixed-point */
/* Default fractional width (C's is 6 for "%f"; here 5 is enough) */
- size_t fracWidth = fmt->hasFrac ? fmt->fracWidth : 5;
+ uint8_t fracWidth = fmt->hasFrac ? fmt->fracWidth : 5;
if (fracWidth) {
- if (fracWidth > 255) {
- error("Fractional width %zu too long, limiting to 255\n",
- fracWidth);
- fracWidth = 255;
- }
-
char spec[16]; /* Max "%" + 5-char PRIu32 + ".%0255.f" + terminator */
- snprintf(spec, sizeof(spec), "%%" PRIu32 ".%%0%zu.f", fracWidth);
+ snprintf(spec, sizeof(spec), "%%" PRIu32 ".%%0%d.f", fracWidth);
snprintf(valueBuf, sizeof(valueBuf), spec, value >> 16,
(value % 65536) / 65536.0 * pow(10, fracWidth) + 0.5);
} else {
@@ -262,17 +252,11 @@
numLen++;
size_t totalLen = fmt->width > numLen ? fmt->width : numLen;
- size_t padLen = fmt->width > numLen ? fmt->width - numLen : 0;
- if (totalLen + 1 > bufLen) { /* bufLen includes terminator */
+ if (totalLen + 1 > bufLen) /* bufLen includes terminator */
error("Formatted numeric value too long\n");
- totalLen = bufLen - 1;
- if (numLen > totalLen) {
- len = totalLen - (numLen - len);
- numLen = totalLen;
- }
- padLen = totalLen - numLen;
- }
+
+ size_t padLen = fmt->width > numLen ? fmt->width - numLen : 0;
if (fmt->alignLeft) {
size_t pos = 0;
--- a/src/asm/rgbasm.5
+++ b/src/asm/rgbasm.5
@@ -337,7 +337,7 @@
\[en]
.Ql 9 .
If specified, prints this many digits of a fixed-point fraction.
-Defaults to 5 digits, maximum 255 digits.
+Defaults to 5 digits.
.It Ql <type> Ta Specifies the type of value.
.El
.Pp
--- a/test/asm/format-truncation.asm
+++ /dev/null
@@ -1,15 +1,0 @@
-num equ 42
-fix equ 123.456
-str equs "hello"
-
-println "{#0260x:num}"
-println "{#-260x:num}"
-println "{0280.260f:fix}"
-println "{260s:str}"
-println "{-260s:str}"
-
-println "<{#0260x:num}>"
-println "<{#-260x:num}>"
-println "<{0280.260f:fix}>"
-println "<{260s:str}>"
-println "<{-260s:str}>"
--- a/test/asm/format-truncation.err
+++ /dev/null
@@ -1,35 +1,0 @@
-ERROR: format-truncation.asm(5):
- Formatted numeric value too long
-ERROR: format-truncation.asm(6):
- Formatted numeric value too long
-ERROR: format-truncation.asm(7):
- Fractional width 260 too long, limiting to 255
-ERROR: format-truncation.asm(7):
- Formatted numeric value too long
-ERROR: format-truncation.asm(8):
- Formatted string value too long
-ERROR: format-truncation.asm(9):
- Formatted string value too long
-ERROR: format-truncation.asm(11):
- Formatted numeric value too long
-warning: format-truncation.asm(11): [-Wlong-string]
- String constant too long
-ERROR: format-truncation.asm(12):
- Formatted numeric value too long
-warning: format-truncation.asm(12): [-Wlong-string]
- String constant too long
-ERROR: format-truncation.asm(13):
- Fractional width 260 too long, limiting to 255
-ERROR: format-truncation.asm(13):
- Formatted numeric value too long
-warning: format-truncation.asm(13): [-Wlong-string]
- String constant too long
-ERROR: format-truncation.asm(14):
- Formatted string value too long
-warning: format-truncation.asm(14): [-Wlong-string]
- String constant too long
-ERROR: format-truncation.asm(15):
- Formatted string value too long
-warning: format-truncation.asm(15): [-Wlong-string]
- String constant too long
-error: Assembly aborted (12 errors)!
--- a/test/asm/format-truncation.out
+++ /dev/null
@@ -1,10 +1,0 @@
-$0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002a
-$2a
-123.45599365234375001369732334415661667551799560000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
- hello
-hello
-<$0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002
-<$2a
-<123.4559936523437500136973233441566166755179956000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-< hell
-<hello
--- a/test/asm/interpolation-overflow.asm
+++ /dev/null
@@ -1,4 +1,0 @@
-; It seems that \1 was the easiest way to notice the memory corruption that
-; resulted from this overflow
-x = 0
-{.99999999f:x}\1
--- a/test/asm/interpolation-overflow.err
+++ /dev/null
@@ -1,9 +1,0 @@
-ERROR: interpolation-overflow.asm(4):
- Fractional width 99999999 too long, limiting to 255
-ERROR: interpolation-overflow.asm(4):
- Formatted numeric value too long
-warning: interpolation-overflow.asm(4): [-Wlarge-constant]
- Precision of fixed-point constant is too large
-while expanding symbol "0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
-FATAL: interpolation-overflow.asm(4):
- Macro argument '\1' not defined