Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/memononen/nanosvg.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMikko Mononen <memononen@gmail.com>2021-09-03 21:24:42 +0300
committerGitHub <noreply@github.com>2021-09-03 21:24:42 +0300
commitccdb1995134d340a93fb20e3a3d323ccb3838dd0 (patch)
tree8d480f68ee75d05ad0c09bdab731d21fa643e492
parent3cdd4a9d788695699799b37d492e45e2c3625790 (diff)
parent419782d3d16e88cfa0695aa0b86c956352846712 (diff)
Merge pull request #198 from ctrlcctrlv/CVE_2019_1000032
Fix decimal values in color fields (nsvg__parseColorRGB, nsvg__parseColorHex)
-rw-r--r--src/nanosvg.h37
1 files changed, 12 insertions, 25 deletions
diff --git a/src/nanosvg.h b/src/nanosvg.h
index 3511664..f5058b1 100644
--- a/src/nanosvg.h
+++ b/src/nanosvg.h
@@ -1215,35 +1215,22 @@ static const char* nsvg__getNextPathItem(const char* s, char* it)
static unsigned int nsvg__parseColorHex(const char* str)
{
- unsigned int c = 0, r = 0, g = 0, b = 0;
- int n = 0;
- str++; // skip #
- // Calculate number of characters.
- while(str[n] && !nsvg__isspace(str[n]))
- n++;
- if (n == 6) {
- sscanf(str, "%x", &c);
- } else if (n == 3) {
- sscanf(str, "%x", &c);
- c = (c&0xf) | ((c&0xf0) << 4) | ((c&0xf00) << 8);
- c |= c<<4;
- }
- r = (c >> 16) & 0xff;
- g = (c >> 8) & 0xff;
- b = c & 0xff;
- return NSVG_RGB(r,g,b);
+ unsigned int r=0, g=0, b=0;
+ if (sscanf(str, "#%2x%2x%2x", &r, &g, &b) == 3 ) // 2 digit hex
+ return NSVG_RGB(r, g, b);
+ if (sscanf(str, "#%1x%1x%1x", &r, &g, &b) == 3 ) // 1 digit hex, e.g. #abc -> 0xccbbaa
+ return NSVG_RGB(r*17, g*17, b*17); // same effect as (r<<4|r), (g<<4|g), ..
+ return NSVG_RGB(128, 128, 128);
}
static unsigned int nsvg__parseColorRGB(const char* str)
{
- int r = -1, g = -1, b = -1;
- char s1[32]="", s2[32]="";
- sscanf(str + 4, "%d%[%%, \t]%d%[%%, \t]%d", &r, s1, &g, s2, &b);
- if (strchr(s1, '%')) {
- return NSVG_RGB((r*255)/100,(g*255)/100,(b*255)/100);
- } else {
- return NSVG_RGB(r,g,b);
- }
+ unsigned int r=0, g=0, b=0;
+ if (sscanf(str, "rgb(%u, %u, %u)", &r, &g, &b) == 3) // decimal integers
+ return NSVG_RGB(r, g, b);
+ if (sscanf(str, "rgb(%u%%, %u%%, %u%%)", &r, &g, &b) == 3) // decimal integer percentage
+ return NSVG_RGB(r*255/100, g*255/100, b*255/100);
+ return NSVG_RGB(128, 128, 128);
}
typedef struct NSVGNamedColor {