From 4894dc5da1ec2009d9d4900eea24fa0a95b40771 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Wed, 17 Jan 2024 16:57:16 -0800 Subject: [PATCH] Improved vector_in --- src/vector.c | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/src/vector.c b/src/vector.c index 79d6bf4..c248475 100644 --- a/src/vector.c +++ b/src/vector.c @@ -181,26 +181,24 @@ vector_in(PG_FUNCTION_ARGS) int32 typmod = PG_GETARG_INT32(2); float x[VECTOR_MAX_DIM]; int dim = 0; - char *pt; + char *pt = lit; char *stringEnd; Vector *result; - char *litcopy = pstrdup(lit); - char *str = litcopy; + bool malformed = false; - while (vector_isspace(*str)) - str++; + while (vector_isspace(*pt)) + pt++; - if (*str != '[') + if (*pt != '[') ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("malformed vector literal: \"%s\"", lit), errdetail("Vector contents must start with \"[\"."))); - str++; - pt = strtok(str, ","); + pt++; stringEnd = pt; - while (pt != NULL && *stringEnd != ']') + while (*pt != '\0' && *pt != ',' && *stringEnd != ']') { if (dim == VECTOR_MAX_DIM) ereport(ERROR, @@ -229,12 +227,22 @@ vector_in(PG_FUNCTION_ARGS) while (vector_isspace(*stringEnd)) stringEnd++; - if (*stringEnd != '\0' && *stringEnd != ']') + if (*stringEnd == '\0') + break; + + if (*stringEnd != ',' && *stringEnd != ']') ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("invalid input syntax for type vector: \"%s\"", lit))); - pt = strtok(NULL, ","); + pt = stringEnd + 1; + + /* TODO Remove in 0.6.0 */ + while (*pt == ',') + { + malformed = true; + pt++; + } } if (stringEnd == NULL || *stringEnd != ']') @@ -255,22 +263,17 @@ vector_in(PG_FUNCTION_ARGS) errmsg("malformed vector literal: \"%s\"", lit), errdetail("Junk after closing right brace."))); - /* Ensure no consecutive delimiters since strtok skips */ - for (pt = lit + 1; *pt != '\0'; pt++) - { - if (pt[-1] == ',' && *pt == ',') - ereport(ERROR, - (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), - errmsg("malformed vector literal: \"%s\"", lit))); - } + /* TODO Remove in 0.6.0 */ + if (malformed) + ereport(ERROR, + (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), + errmsg("malformed vector literal: \"%s\"", lit))); if (dim < 1) ereport(ERROR, (errcode(ERRCODE_DATA_EXCEPTION), errmsg("vector must have at least 1 dimension"))); - pfree(litcopy); - CheckExpectedDim(typmod, dim); result = InitVector(dim);