Merge pull request #198 from ctrlcctrlv/CVE_2019_1000032

Fix decimal values in color fields (nsvg__parseColorRGB, nsvg__parseColorHex)
This commit is contained in:
Mikko Mononen 2021-09-03 21:24:42 +03:00 committed by GitHub
commit ccdb199513
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1215,35 +1215,22 @@ static const char* nsvg__getNextPathItem(const char* s, char* it)
static unsigned int nsvg__parseColorHex(const char* str) static unsigned int nsvg__parseColorHex(const char* str)
{ {
unsigned int c = 0, r = 0, g = 0, b = 0; unsigned int r=0, g=0, b=0;
int n = 0; if (sscanf(str, "#%2x%2x%2x", &r, &g, &b) == 3 ) // 2 digit hex
str++; // skip # return NSVG_RGB(r, g, b);
// Calculate number of characters. if (sscanf(str, "#%1x%1x%1x", &r, &g, &b) == 3 ) // 1 digit hex, e.g. #abc -> 0xccbbaa
while(str[n] && !nsvg__isspace(str[n])) return NSVG_RGB(r*17, g*17, b*17); // same effect as (r<<4|r), (g<<4|g), ..
n++; return NSVG_RGB(128, 128, 128);
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);
} }
static unsigned int nsvg__parseColorRGB(const char* str) static unsigned int nsvg__parseColorRGB(const char* str)
{ {
int r = -1, g = -1, b = -1; unsigned int r=0, g=0, b=0;
char s1[32]="", s2[32]=""; if (sscanf(str, "rgb(%u, %u, %u)", &r, &g, &b) == 3) // decimal integers
sscanf(str + 4, "%d%[%%, \t]%d%[%%, \t]%d", &r, s1, &g, s2, &b); return NSVG_RGB(r, g, b);
if (strchr(s1, '%')) { 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(r*255/100, g*255/100, b*255/100);
} else { return NSVG_RGB(128, 128, 128);
return NSVG_RGB(r,g,b);
}
} }
typedef struct NSVGNamedColor { typedef struct NSVGNamedColor {