Fixed null pointer access in unit conversion

This commit is contained in:
Mikko Mononen 2014-02-02 18:25:53 +02:00
parent 47864e21bc
commit 0189176ec4

View File

@ -1150,43 +1150,55 @@ static unsigned int nsvg__parseColor(const char* str)
static float nsvg__convertToPixels(struct NSVGparser* p, float val, const char* units, int dir) static float nsvg__convertToPixels(struct NSVGparser* p, float val, const char* units, int dir)
{ {
struct NSVGattrib* attr; struct NSVGattrib* attr;
// Convert units to pixels.
if (units[0] == '\0') { if (p != NULL) {
return val; // Convert units to pixels.
} else if (units[0] == 'p' && units[1] == 'x') { if (units[0] == '\0') {
return val; return val;
} else if (units[0] == 'p' && units[1] == 't') { } else if (units[0] == 'p' && units[1] == 'x') {
return val / 72.0f * p->dpi; return val;
} else if (units[0] == 'p' && units[1] == 'c') { } else if (units[0] == 'p' && units[1] == 't') {
return val / 6.0f * p->dpi; return val / 72.0f * p->dpi;
} else if (units[0] == 'm' && units[1] == 'm') { } else if (units[0] == 'p' && units[1] == 'c') {
return val / 25.4f * p->dpi; return val / 6.0f * p->dpi;
} else if (units[0] == 'c' && units[1] == 'm') { } else if (units[0] == 'm' && units[1] == 'm') {
return val / 2.54f * p->dpi; return val / 25.4f * p->dpi;
} else if (units[0] == 'i' && units[1] == 'n') { } else if (units[0] == 'c' && units[1] == 'm') {
return val * p->dpi; return val / 2.54f * p->dpi;
} else if (units[0] == '%') { } else if (units[0] == 'i' && units[1] == 'n') {
if (p != NULL) { return val * p->dpi;
attr = nsvg__getAttr(p); } else if (units[0] == '%') {
if (dir == 0) if (p != NULL) {
return (val/100.0f) * nsvg__actualWidth(p); attr = nsvg__getAttr(p);
else if (dir == 1) if (dir == 0)
return (val/100.0f) * nsvg__actualHeight(p); return (val/100.0f) * nsvg__actualWidth(p);
else if (dir == 2) else if (dir == 1)
return (val/100.0f) * nsvg__actualLength(p); return (val/100.0f) * nsvg__actualHeight(p);
} else { else if (dir == 2)
return (val/100.0f) * nsvg__actualLength(p);
} else {
return (val/100.0f);
}
} else if (units[0] == 'e' && units[1] == 'm') {
if (p != NULL) {
attr = nsvg__getAttr(p);
return val * attr->fontSize;
}
} else if (units[0] == 'e' && units[1] == 'x') {
if (p != NULL) {
attr = nsvg__getAttr(p);
return val * attr->fontSize * 0.52f; // x-height of Helvetica.
}
}
} else {
// Convert units to pixels.
if (units[0] == '\0') {
return val;
} else if (units[0] == 'p' && units[1] == 'x') {
return val;
} else if (units[0] == '%') {
return (val/100.0f); return (val/100.0f);
} }
} else if (units[0] == 'e' && units[1] == 'm') {
if (p != NULL) {
attr = nsvg__getAttr(p);
return val * attr->fontSize;
}
} else if (units[0] == 'e' && units[1] == 'x') {
if (p != NULL) {
attr = nsvg__getAttr(p);
return val * attr->fontSize * 0.52f; // x-height of Helvetica.
}
} }
return val; return val;
} }
@ -2386,7 +2398,7 @@ static void nsvg__scaleToViewbox(struct NSVGparser* p, const char* units)
ty = -p->viewMiny; ty = -p->viewMiny;
sx = p->viewWidth > 0 ? p->image->width / p->viewWidth : 0; sx = p->viewWidth > 0 ? p->image->width / p->viewWidth : 0;
sy = p->viewHeight > 0 ? p->image->height / p->viewHeight : 0; sy = p->viewHeight > 0 ? p->image->height / p->viewHeight : 0;
us = 1.0f / nsvg__convertToPixels(NULL, 1.0f, units, 0); us = 1.0f / nsvg__convertToPixels(p, 1.0f, units, 0);
// Fix aspect ratio // Fix aspect ratio
if (p->alignType == NSVG_ALIGN_MEET) { if (p->alignType == NSVG_ALIGN_MEET) {