[PATCH vkd3d 1/6] vkd3d-shader/hlsl: Handle over/underflow when parsing integer literals.

Matteo Bruni mbruni at codeweavers.com
Tue May 10 13:21:27 CDT 2022


Signed-off-by: Matteo Bruni <mbruni at codeweavers.com>
---
This fixes arithmetic-int.shader_test on 32-bit Linux for
me.

 libs/vkd3d-shader/hlsl.l | 38 +++++++++++++++++++++++++++++++++++---
 1 file changed, 35 insertions(+), 3 deletions(-)

diff --git a/libs/vkd3d-shader/hlsl.l b/libs/vkd3d-shader/hlsl.l
index 267c8c30..2c398bc8 100644
--- a/libs/vkd3d-shader/hlsl.l
+++ b/libs/vkd3d-shader/hlsl.l
@@ -31,6 +31,38 @@ static void update_location(struct hlsl_ctx *ctx, YYLTYPE *loc);
 
 #define YY_USER_ACTION update_location(yyget_extra(yyscanner), yyget_lloc(yyscanner));
 
+static int char_to_int(char c)
+{
+    if ('0' <= c && c <= '9')
+        return c - '0';
+    if ('A' <= c && c <= 'F')
+        return c - 'A' + 10;
+    if ('a' <= c && c <= 'f')
+        return c - 'a' + 10;
+    return -1;
+}
+
+static uint32_t parse_integer(const char *s)
+{
+    uint32_t base = 10, ret = 0;
+    int digit;
+
+    if (*s == '0')
+    {
+        base = 8;
+        ++s;
+        if (*s == 'x' || *s == 'X')
+        {
+            base = 16;
+            ++s;
+        }
+    }
+
+    while ((digit = char_to_int(*s++)) >= 0)
+        ret = ret * base + (uint32_t)digit;
+    return ret;
+}
+
 %}
 
 %option bison-bridge
@@ -197,15 +229,15 @@ row_major               {return KW_ROW_MAJOR;           }
                             return C_FLOAT;
                         }
 0x[0-9a-fA-F]+          {
-                            sscanf(yytext, "0x%x", &yylval->intval);
+                            yylval->intval = parse_integer(yytext);
                             return C_INTEGER;
                         }
 0[0-7]+                 {
-                            sscanf(yytext, "0%o", &yylval->intval);
+                            yylval->intval = parse_integer(yytext);
                             return C_INTEGER;
                         }
 [0-9]+                  {
-                            yylval->intval = (atoi(yytext));
+                            yylval->intval = parse_integer(yytext);
                             return C_INTEGER;
                         }
 
-- 
2.35.1




More information about the wine-devel mailing list