Changeset: 03e920ce03c0 for MonetDB URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=03e920ce03c0 Modified Files: gdk/ChangeLog gdk/gdk.h gdk/gdk_atoms.c gdk/gdk_calc.c gdk/gdk_storage.c geom/monetdb5/geom.c monetdb5/mal/Tests/tst1200.malC monetdb5/mal/Tests/tst1203.malC monetdb5/mal/Tests/tst1205.malC monetdb5/mal/mal_exception.c monetdb5/mal/mal_parser.c monetdb5/modules/atoms/Tests/inet01.stable.out monetdb5/modules/atoms/blob.c monetdb5/modules/atoms/color.c monetdb5/modules/atoms/identifier.c monetdb5/modules/atoms/inet.c monetdb5/modules/atoms/json.c monetdb5/modules/atoms/mtime.c monetdb5/modules/atoms/url.c monetdb5/modules/atoms/uuid.c monetdb5/modules/atoms/xml.c monetdb5/modules/kernel/bat5.c monetdb5/modules/mal/mal_io.c monetdb5/modules/mal/remote.c monetdb5/modules/mal/tablet.c sql/backends/monet5/UDF/pyapi/conversion.c sql/backends/monet5/UDF/pyapi/type_conversion.c sql/backends/monet5/sql.c sql/backends/monet5/sql_cast.c sql/backends/monet5/sql_cast_impl_int.h sql/backends/monet5/sql_result.c sql/backends/monet5/sql_statistics.c sql/backends/monet5/wlr.c sql/server/sql_atom.c sql/server/sql_parser.y Branch: default Log Message:
The atomtostr and atomfromstr functions now consistently return -1 on failure. Also, bad input (e.g. overflow, bad syntax) is a failure. diffs (truncated from 2945 to 300 lines): diff --git a/gdk/ChangeLog b/gdk/ChangeLog --- a/gdk/ChangeLog +++ b/gdk/ChangeLog @@ -1,3 +1,8 @@ # ChangeLog file for MonetDB # This file is updated with Maddlog +* Thu Sep 14 2017 Sjoerd Mullender <[email protected]> +- The atom tostr and fromstr "methods" now always return -1 on error. + A return value greater than 0 is normal, a return value of 0 is not + normal, but technically not an error. + diff --git a/gdk/gdk.h b/gdk/gdk.h --- a/gdk/gdk.h +++ b/gdk/gdk.h @@ -1825,7 +1825,8 @@ gdk_export BAT *BBPquickdesc(bat b, int * @item The @emph{ATOMfromstr()} parses an atom value from string * `s'. The memory allocation policy is the same as in * @emph{ATOMget()}. The return value is the number of parsed - * characters. + * characters or -1 on failure. Also in case of failure, the output + * parameter buf is a valid pointer or NULL. * * @item The @emph{ATOMprint()} prints an ASCII description of the * atom value pointed to by `val' on file descriptor `fd'. The return diff --git a/gdk/gdk_atoms.c b/gdk/gdk_atoms.c --- a/gdk/gdk_atoms.c +++ b/gdk/gdk_atoms.c @@ -325,12 +325,18 @@ ATOMprint(int t, const void *p, stream * char buf[dblStrlen], *addr = buf; /* use memory from stack */ int sz = dblStrlen, l = (*tostr) (&addr, &sz, p); - res = mnstr_write(s, buf, l, 1); + if (l < 0) + res = -1; + else + res = mnstr_write(s, buf, l, 1); } else { str buf = 0; int sz = 0, l = (*tostr) (&buf, &sz, p); - res = mnstr_write(s, buf, l, 1); + if (l < 0) + res = -1; + else + res = mnstr_write(s, buf, l, 1); GDKfree(buf); } } else { @@ -383,14 +389,16 @@ ATOMdup(int t, const void *p) * as 'dst' and/or a *len==0 is valid; the conversion function will * then alloc some region for you. */ -#define atommem(TYPE, size) \ +#define atommem(size) \ do { \ if (*dst == NULL || *len < (int) (size)) { \ GDKfree(*dst); \ *len = (size); \ - *dst = (TYPE *) GDKmalloc(*len); \ - if (*dst == NULL) \ + *dst = GDKmalloc(*len); \ + if (*dst == NULL) { \ + *len = 0; \ return -1; \ + } \ } \ } while (0) @@ -398,7 +406,7 @@ ATOMdup(int t, const void *p) int \ TYPE##ToStr(char **dst, int *len, const TYPE *src) \ { \ - atommem(char, TYPE##Strlen); \ + atommem(TYPE##Strlen); \ if (*src == TYPE##_nil) { \ return snprintf(*dst, *len, "nil"); \ } \ @@ -429,7 +437,7 @@ voidToStr(str *dst, int *len, void *src) { (void) src; - atommem(char, 4); + atommem(4); return snprintf(*dst, *len, "nil"); } #endif @@ -462,7 +470,7 @@ bitFromStr(const char *src, int *len, bi { const char *p = src; - atommem(bit, sizeof(bit)); + atommem(sizeof(bit)); while (GDKisspace(*p)) p++; @@ -492,7 +500,7 @@ bitFromStr(const char *src, int *len, bi int bitToStr(char **dst, int *len, const bit *src) { - atommem(char, 6); + atommem(6); if (*src == bit_nil) return snprintf(*dst, *len, "nil"); @@ -509,7 +517,7 @@ batFromStr(const char *src, int *len, ba int c; bat bid = 0; - atommem(bat, sizeof(bat)); + atommem(sizeof(bat)); while (GDKisspace(*r)) r++; @@ -543,11 +551,11 @@ batToStr(char **dst, int *len, const bat str s; if (b == bat_nil || (s = BBPname(b)) == NULL || *s == 0) { - atommem(char, 4); + atommem(4); return snprintf(*dst, *len, "nil"); } i = (int) (strlen(s) + 4); - atommem(char, i); + atommem(i); return snprintf(*dst, *len, "<%s>", s); } @@ -657,7 +665,7 @@ numFromStr(const char *src, int *len, vo * embedded spaces are not allowed * the optional LL at the end are only allowed for lng and hge * values */ - atommem(void, sz); + atommem(sz); while (GDKisspace(*p)) p++; if (!num10(*p)) { @@ -668,8 +676,8 @@ numFromStr(const char *src, int *len, vo p += 3; return (int) (p - src); } - /* not a number */ - return 0; + GDKerror("not a number"); + goto bailout; case '-': sign = -1; p++; @@ -679,7 +687,7 @@ numFromStr(const char *src, int *len, vo break; } if (!num10(*p)) { - /* still not a number */ + GDKerror("not a number"); goto bailout; } } @@ -688,7 +696,7 @@ numFromStr(const char *src, int *len, vo if (base > maxdiv[1].maxval || (base == maxdiv[1].maxval && dig > maxmod10)) { /* overflow */ - goto bailout; + goto overflow; } base = 10 * base + dig; p++; @@ -707,13 +715,13 @@ numFromStr(const char *src, int *len, vo exp = exp * 10 + base10(*p); if (exp >= (int) (sizeof(maxdiv) / sizeof(maxdiv[0]))) { /* overflow */ - goto bailout; + goto overflow; } p++; } while (num10(*p)); if (base > maxdiv[exp].maxval) { /* overflow */ - goto bailout; + goto overflow; } base *= maxdiv[exp].scale; } @@ -723,8 +731,7 @@ numFromStr(const char *src, int *len, vo case 1: { bte **dstbte = (bte **) dst; if (base <= GDK_bte_min || base > GDK_bte_max) { - **dstbte = bte_nil; - return 0; + goto overflow; } **dstbte = (bte) base; break; @@ -732,8 +739,7 @@ numFromStr(const char *src, int *len, vo case 2: { sht **dstsht = (sht **) dst; if (base <= GDK_sht_min || base > GDK_sht_max) { - **dstsht = sht_nil; - return 0; + goto overflow; } **dstsht = (sht) base; break; @@ -741,8 +747,7 @@ numFromStr(const char *src, int *len, vo case 4: { int **dstint = (int **) dst; if (base <= GDK_int_min || base > GDK_int_max) { - **dstint = int_nil; - return 0; + goto overflow; } **dstint = (int) base; break; @@ -751,8 +756,7 @@ numFromStr(const char *src, int *len, vo lng **dstlng = (lng **) dst; #ifdef HAVE_HGE if (base <= GDK_lng_min || base > GDK_lng_max) { - **dstlng = lng_nil; - return 0; + goto overflow; } #endif **dstlng = (lng) base; @@ -774,9 +778,14 @@ numFromStr(const char *src, int *len, vo p++; return (int) (p - src); + overflow: + while (num10(*p)) + p++; + GDKerror("overflow: \"%.*s\" does not fit in %s\n", + (int) (p - src), src, ATOMname(tp)); bailout: memcpy(*dst, ATOMnilptr(tp), sz); - return 0; + return -1; } int @@ -859,7 +868,7 @@ atom_io(lng, Lng, lng) int hgeToStr(char **dst, int *len, const hge *src) { - atommem(char, hgeStrlen); + atommem(hgeStrlen); if (*src == hge_nil) { strncpy(*dst, "nil", *len); return 3; @@ -870,7 +879,10 @@ hgeToStr(char **dst, int *len, const hge } else { hge s = *src / HGE_LL18DIGITS; int l = hgeToStr(dst, len, &s); - snprintf(*dst + l, *len - l, HGE_LL018FMT, (lng) HGE_ABS(*src % HGE_LL18DIGITS)); + if (l < 0) + return -1; + snprintf(*dst + l, *len - l, HGE_LL018FMT, + (lng) HGE_ABS(*src % HGE_LL18DIGITS)); return (int) strlen(*dst); } } @@ -883,7 +895,7 @@ ptrFromStr(const char *src, int *len, pt size_t base = 0; const char *p = src; - atommem(ptr, sizeof(ptr)); + atommem(sizeof(ptr)); while (GDKisspace(*p)) p++; @@ -895,13 +907,13 @@ ptrFromStr(const char *src, int *len, pt p += 2; } if (!num16(*p)) { - /* not a number */ - return 0; + GDKerror("not a number\n"); + return -1; } while (num16(*p)) { if (base >= ((size_t) 1 << (8 * sizeof(size_t) - 4))) { - /* overflow */ - return 0; + GDKerror("overflow\n"); + return -1; } base = mult16(base) + base16(*p); p++; @@ -933,7 +945,7 @@ dblFromStr(const char *src, int *len, db double d; /* alloc memory */ - atommem(dbl, sizeof(dbl)); + atommem(sizeof(dbl)); while (GDKisspace(*p)) p++; @@ -960,8 +972,8 @@ dblFromStr(const char *src, int *len, db || !isfinite(d) /* no NaN or Infinte */ #endif ) { - **dst = dbl_nil; /* default return value is nil */ _______________________________________________ checkin-list mailing list [email protected] https://www.monetdb.org/mailman/listinfo/checkin-list
