From: joncruz Date: Thu, 8 Oct 2009 08:44:05 +0000 (+0000) Subject: Fixed signed/unsigned problem with precision calc. Fixes bug #399604. X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=3375512f3173e051906785f0d0fd5d74320efd17;p=inkscape.git Fixed signed/unsigned problem with precision calc. Fixes bug #399604. --- diff --git a/src/svg/path-string.cpp b/src/svg/path-string.cpp index 0baaf521e..a850d7c10 100644 --- a/src/svg/path-string.cpp +++ b/src/svg/path-string.cpp @@ -129,7 +129,7 @@ void Inkscape::SVG::PathString::State::appendNumber(double v, int precision, int size_t const oldsize = str.size(); str.append(reserve, (char)0); char* begin_of_num = const_cast(str.data()+oldsize); // Slightly evil, I know (but std::string should be storing its data in one big block of memory, so...) - size_t added = sp_svg_number_write_de(begin_of_num, v, precision, minexp); + size_t added = sp_svg_number_write_de(begin_of_num, reserve, v, precision, minexp); str.resize(oldsize+added); // remove any trailing characters } diff --git a/src/svg/svg-affine.cpp b/src/svg/svg-affine.cpp index 1ff9776e5..91a9fa7e5 100644 --- a/src/svg/svg-affine.cpp +++ b/src/svg/svg-affine.cpp @@ -177,9 +177,9 @@ sp_svg_transform_write(Geom::Matrix const &transform) unsigned p = 0; strcpy (c + p, "scale("); p += 6; - p += sp_svg_number_write_de (c + p, transform[0], prec, min_exp); + p += sp_svg_number_write_de( c + p, sizeof(c) - p, transform[0], prec, min_exp ); c[p++] = ','; - p += sp_svg_number_write_de (c + p, transform[3], prec, min_exp); + p += sp_svg_number_write_de( c + p, sizeof(c) - p, transform[3], prec, min_exp ); c[p++] = ')'; c[p] = '\000'; g_assert( p <= sizeof(c) ); @@ -192,9 +192,9 @@ sp_svg_transform_write(Geom::Matrix const &transform) unsigned p = 0; strcpy (c + p, "translate("); p += 10; - p += sp_svg_number_write_de (c + p, transform[4], prec, min_exp); + p += sp_svg_number_write_de( c + p, sizeof(c) - p, transform[4], prec, min_exp ); c[p++] = ','; - p += sp_svg_number_write_de (c + p, transform[5], prec, min_exp); + p += sp_svg_number_write_de( c + p, sizeof(c) - p, transform[5], prec, min_exp ); c[p++] = ')'; c[p] = '\000'; g_assert( p <= sizeof(c) ); @@ -204,17 +204,17 @@ sp_svg_transform_write(Geom::Matrix const &transform) unsigned p = 0; strcpy (c + p, "matrix("); p += 7; - p += sp_svg_number_write_de (c + p, transform[0], prec, min_exp); + p += sp_svg_number_write_de( c + p, sizeof(c) - p, transform[0], prec, min_exp ); c[p++] = ','; - p += sp_svg_number_write_de (c + p, transform[1], prec, min_exp); + p += sp_svg_number_write_de( c + p, sizeof(c) - p, transform[1], prec, min_exp ); c[p++] = ','; - p += sp_svg_number_write_de (c + p, transform[2], prec, min_exp); + p += sp_svg_number_write_de( c + p, sizeof(c) - p, transform[2], prec, min_exp ); c[p++] = ','; - p += sp_svg_number_write_de (c + p, transform[3], prec, min_exp); + p += sp_svg_number_write_de( c + p, sizeof(c) - p, transform[3], prec, min_exp ); c[p++] = ','; - p += sp_svg_number_write_de (c + p, transform[4], prec, min_exp); + p += sp_svg_number_write_de( c + p, sizeof(c) - p, transform[4], prec, min_exp ); c[p++] = ','; - p += sp_svg_number_write_de (c + p, transform[5], prec, min_exp); + p += sp_svg_number_write_de( c + p, sizeof(c) - p, transform[5], prec, min_exp ); c[p++] = ')'; c[p] = '\000'; g_assert( p <= sizeof(c) ); @@ -226,17 +226,17 @@ sp_svg_transform_write(Geom::Matrix const &transform) unsigned p = 0; strcpy (c + p, "matrix("); p += 7; - p += sp_svg_number_write_de (c + p, transform[0], prec, min_exp); + p += sp_svg_number_write_de( c + p, sizeof(c) - p, transform[0], prec, min_exp ); c[p++] = ','; - p += sp_svg_number_write_de (c + p, transform[1], prec, min_exp); + p += sp_svg_number_write_de( c + p, sizeof(c) - p, transform[1], prec, min_exp ); c[p++] = ','; - p += sp_svg_number_write_de (c + p, transform[2], prec, min_exp); + p += sp_svg_number_write_de( c + p, sizeof(c) - p, transform[2], prec, min_exp ); c[p++] = ','; - p += sp_svg_number_write_de (c + p, transform[3], prec, min_exp); + p += sp_svg_number_write_de( c + p, sizeof(c) - p, transform[3], prec, min_exp ); c[p++] = ','; - p += sp_svg_number_write_de (c + p, transform[4], prec, min_exp); + p += sp_svg_number_write_de( c + p, sizeof(c) - p, transform[4], prec, min_exp ); c[p++] = ','; - p += sp_svg_number_write_de (c + p, transform[5], prec, min_exp); + p += sp_svg_number_write_de( c + p, sizeof(c) - p, transform[5], prec, min_exp ); c[p++] = ')'; c[p] = '\000'; g_assert( p <= sizeof(c) ); diff --git a/src/svg/svg-length.cpp b/src/svg/svg-length.cpp index 1d41f9871..942f74d46 100644 --- a/src/svg/svg-length.cpp +++ b/src/svg/svg-length.cpp @@ -43,7 +43,7 @@ unsigned int sp_svg_number_read_f(gchar const *str, float *val) if ((gchar const *) e == str) { return 0; } - + *val = v; return 1; } @@ -59,7 +59,7 @@ unsigned int sp_svg_number_read_d(gchar const *str, double *val) if ((gchar const *) e == str) { return 0; } - + *val = v; return 1; } @@ -72,14 +72,14 @@ static unsigned int sp_svg_number_write_ui(gchar *buf, unsigned int val) c[16u - (++i)] = '0' + (val % 10u); val /= 10u; } while (val > 0u); - + memcpy(buf, &c[16u - i], i); buf[i] = 0; - + return i; } -static unsigned int sp_svg_number_write_i(gchar *buf, int val) +static unsigned int sp_svg_number_write_i(gchar *buf, int bufLen, int val) { int p = 0; unsigned int uval; @@ -91,11 +91,11 @@ static unsigned int sp_svg_number_write_i(gchar *buf, int val) } p += sp_svg_number_write_ui(buf+p, uval); - + return p; } -static unsigned sp_svg_number_write_d(gchar *buf, double val, unsigned int tprec, unsigned int fprec) +static unsigned sp_svg_number_write_d(gchar *buf, int bufLen, double val, unsigned int tprec, unsigned int fprec) { /* Process sign */ int i = 0; @@ -103,15 +103,15 @@ static unsigned sp_svg_number_write_d(gchar *buf, double val, unsigned int tprec buf[i++] = '-'; val = fabs(val); } - + /* Determine number of integral digits */ int idigits = 0; if (val >= 1.0) { idigits = (int) floor(log10(val)) + 1; } - + /* Determine the actual number of fractional digits */ - fprec = MAX(fprec, tprec - idigits); + fprec = MAX(static_cast(fprec), static_cast(tprec) - idigits); /* Round value */ val += 0.5 / pow(10.0, fprec); /* Extract integral and fractional parts */ @@ -146,7 +146,7 @@ static unsigned sp_svg_number_write_d(gchar *buf, double val, unsigned int tprec return end_i; } -unsigned int sp_svg_number_write_de(gchar *buf, double val, unsigned int tprec, int min_exp) +unsigned int sp_svg_number_write_de(gchar *buf, int bufLen, double val, unsigned int tprec, int min_exp) { int eval = (int)floor(log10(fabs(val))); if (val == 0.0 || eval < min_exp) { @@ -158,12 +158,12 @@ unsigned int sp_svg_number_write_de(gchar *buf, double val, unsigned int tprec, (unsigned int)eval+1; unsigned int maxnumdigitsWithExp = tprec + ( eval<0 ? 4 : 3 ); // It's not necessary to take larger exponents into account, because then maxnumdigitsWithoutExp is DEFINITELY larger if (maxnumdigitsWithoutExp <= maxnumdigitsWithExp) { - return sp_svg_number_write_d(buf, val, tprec, 0); + return sp_svg_number_write_d(buf, bufLen, val, tprec, 0); } else { val = eval < 0 ? val * pow(10.0, -eval) : val / pow(10.0, eval); - int p = sp_svg_number_write_d(buf, val, tprec, 0); + int p = sp_svg_number_write_d(buf, bufLen, val, tprec, 0); buf[p++] = 'e'; - p += sp_svg_number_write_i(buf + p, eval); + p += sp_svg_number_write_i(buf + p, bufLen - p, eval); return p; } } @@ -255,7 +255,7 @@ std::vector sp_svg_length_list_read(gchar const *str) float computed; char *next = (char *) str; std::vector list; - + while (sp_svg_length_read_lff(next, &unit, &value, &computed, &next)) { SVGLength length; @@ -266,9 +266,9 @@ std::vector sp_svg_length_list_read(gchar const *str) (*next == ',' || *next == ' ' || *next == '\n' || *next == '\r' || *next == '\t')) { // the list can be comma- or space-separated, but we will be generous and accept // a mix, including newlines and tabs - next++; + next++; } - + if (!next || !*next) { break; } @@ -291,7 +291,7 @@ static unsigned sp_svg_length_read_lff(gchar const *str, SVGLength::Unit *unit, if (e == str) { return 0; } - + if (!e[0]) { /* Unitless */ if (unit) { diff --git a/src/svg/svg.h b/src/svg/svg.h index 0eab21226..0b2c3ae53 100644 --- a/src/svg/svg.h +++ b/src/svg/svg.h @@ -1,5 +1,5 @@ -#ifndef __SP_SVG_H__ -#define __SP_SVG_H__ +#ifndef SEEN_SP_SVG_H +#define SEEN_SP_SVG_H /* * SVG data parser @@ -28,14 +28,14 @@ * - no valid end character checking * Return FALSE and let val untouched on error */ - -unsigned int sp_svg_number_read_f (const gchar *str, float *val); -unsigned int sp_svg_number_read_d (const gchar *str, double *val); + +unsigned int sp_svg_number_read_f( const gchar *str, float *val ); +unsigned int sp_svg_number_read_d( const gchar *str, double *val ); /* * No buffer overflow checking is done, so better wrap them if needed */ -unsigned int sp_svg_number_write_de (gchar *buf, double val, unsigned int tprec, int min_exp); +unsigned int sp_svg_number_write_de( gchar *buf, int bufLen, double val, unsigned int tprec, int min_exp ); /* Length */ @@ -48,9 +48,9 @@ unsigned int sp_svg_number_write_de (gchar *buf, double val, unsigned int tprec, * Any return value pointer can be NULL */ -unsigned int sp_svg_length_read_computed_absolute (const gchar *str, float *length); -std::vector sp_svg_length_list_read (const gchar *str); -unsigned int sp_svg_length_read_ldd (const gchar *str, SVGLength::Unit *unit, double *value, double *computed); +unsigned int sp_svg_length_read_computed_absolute( const gchar *str, float *length ); +std::vector sp_svg_length_list_read( const gchar *str ); +unsigned int sp_svg_length_read_ldd( const gchar *str, SVGLength::Unit *unit, double *value, double *computed ); std::string sp_svg_length_write_with_units(SVGLength const &length); @@ -59,15 +59,15 @@ bool sp_svg_transform_read(gchar const *str, Geom::Matrix *transform); gchar *sp_svg_transform_write(Geom::Matrix const &transform); gchar *sp_svg_transform_write(Geom::Matrix const *transform); -double sp_svg_read_percentage (const char * str, double def); +double sp_svg_read_percentage( const char * str, double def ); /* NB! As paths can be long, we use here dynamic string */ -Geom::PathVector sp_svg_read_pathv (char const * str); -gchar * sp_svg_write_path (Geom::PathVector const &p); -gchar * sp_svg_write_path (Geom::Path const &p); +Geom::PathVector sp_svg_read_pathv( char const * str ); +gchar * sp_svg_write_path( Geom::PathVector const &p ); +gchar * sp_svg_write_path( Geom::Path const &p ); -#endif +#endif // SEEN_SP_SVG_H /* Local Variables: