File 0005-Add-error-handling-to-color-parsing.patch of Package libqt5-qtsvg.29651

From 521c3f85efb79ed8fd911657721c264332ea4d86 Mon Sep 17 00:00:00 2001
From: Allan Sandfeld Jensen <allan.jensen@qt.io>
Date: Tue, 23 Jun 2020 17:47:03 +0200
Subject: [PATCH 05/21] Add error handling to color parsing

Also fixes undefined shift of negative values.

Fixes oss-fuzz 23644

Change-Id: I08c998ebf2217cb8dc50fcb805603e01e67ad64b
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
(cherry picked from commit 9a0d4ff631003a84205c61bd7a6ef843207f1675)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit 0a5f8b8744347cca1ceb0f3c384e6788c3ccdbc4)
---
 src/svg/qsvghandler.cpp | 41 ++++++++++++++++++++++-------------------
 1 file changed, 22 insertions(+), 19 deletions(-)

diff --git a/src/svg/qsvghandler.cpp b/src/svg/qsvghandler.cpp
index 2029a72..14f7905 100644
--- a/src/svg/qsvghandler.cpp
+++ b/src/svg/qsvghandler.cpp
@@ -104,7 +104,7 @@ static inline QByteArray msgCouldNotResolveProperty(const QString &id, const QXm
 
 // ======== duplicated from qcolor_p
 
-static inline int qsvg_h2i(char hex)
+static inline int qsvg_h2i(char hex, bool *ok = nullptr)
 {
     if (hex >= '0' && hex <= '9')
         return hex - '0';
@@ -112,18 +112,20 @@ static inline int qsvg_h2i(char hex)
         return hex - 'a' + 10;
     if (hex >= 'A' && hex <= 'F')
         return hex - 'A' + 10;
+    if (ok)
+        *ok = false;
     return -1;
 }
 
-static inline int qsvg_hex2int(const char *s)
+static inline int qsvg_hex2int(const char *s, bool *ok = nullptr)
 {
-    return (qsvg_h2i(s[0]) << 4) | qsvg_h2i(s[1]);
+    return (qsvg_h2i(s[0], ok) * 16) | qsvg_h2i(s[1], ok);
 }
 
-static inline int qsvg_hex2int(char s)
+static inline int qsvg_hex2int(char s, bool *ok = nullptr)
 {
-    int h = qsvg_h2i(s);
-    return (h << 4) | h;
+    int h = qsvg_h2i(s, ok);
+    return (h * 16) | h;
 }
 
 bool qsvg_get_hex_rgb(const char *name, QRgb *rgb)
@@ -133,26 +135,27 @@ bool qsvg_get_hex_rgb(const char *name, QRgb *rgb)
     name++;
     int len = qstrlen(name);
     int r, g, b;
+    bool ok = true;
     if (len == 12) {
-        r = qsvg_hex2int(name);
-        g = qsvg_hex2int(name + 4);
-        b = qsvg_hex2int(name + 8);
+        r = qsvg_hex2int(name, &ok);
+        g = qsvg_hex2int(name + 4, &ok);
+        b = qsvg_hex2int(name + 8, &ok);
     } else if (len == 9) {
-        r = qsvg_hex2int(name);
-        g = qsvg_hex2int(name + 3);
-        b = qsvg_hex2int(name + 6);
+        r = qsvg_hex2int(name, &ok);
+        g = qsvg_hex2int(name + 3, &ok);
+        b = qsvg_hex2int(name + 6, &ok);
     } else if (len == 6) {
-        r = qsvg_hex2int(name);
-        g = qsvg_hex2int(name + 2);
-        b = qsvg_hex2int(name + 4);
+        r = qsvg_hex2int(name, &ok);
+        g = qsvg_hex2int(name + 2, &ok);
+        b = qsvg_hex2int(name + 4, &ok);
     } else if (len == 3) {
-        r = qsvg_hex2int(name[0]);
-        g = qsvg_hex2int(name[1]);
-        b = qsvg_hex2int(name[2]);
+        r = qsvg_hex2int(name[0], &ok);
+        g = qsvg_hex2int(name[1], &ok);
+        b = qsvg_hex2int(name[2], &ok);
     } else {
         r = g = b = -1;
     }
-    if ((uint)r > 255 || (uint)g > 255 || (uint)b > 255) {
+    if ((uint)r > 255 || (uint)g > 255 || (uint)b > 255 || !ok) {
         *rgb = 0;
         return false;
     }
-- 
2.20.1

openSUSE Build Service is sponsored by