deparse: Fix string handling in expand_jsonval_strlit()
authorIan Barwick <ian@2ndquadrant.com>
Thu, 27 Nov 2014 05:04:57 +0000 (14:04 +0900)
committerIan Barwick <ian@2ndquadrant.com>
Thu, 27 Nov 2014 05:12:45 +0000 (14:12 +0900)
Per comment in jsonb.h: "Not necessarily null-terminated"

src/backend/utils/adt/ddl_json.c

index 5b515729dd5ee93d9ab6b37b76e934c311ef60d3..7ec13f7dbc6c8357b6ce115a58401a8b6fbc7128 100644 (file)
@@ -425,13 +425,14 @@ expand_jsonval_strlit(StringInfo buf, JsonbValue *jsonval)
    static const char dqsuffixes[] = "_XYZZYX_";
    int         dqnextchar = 0;
 
-   str = jsonval->val.string.val;
+   str = pnstrdup(jsonval->val.string.val, jsonval->val.string.len);
 
    /* easy case: if there are no ' and no \, just use a single quote */
    if (strchr(str, '\'') == NULL &&
        strchr(str, '\\') == NULL)
    {
        appendStringInfo(buf, "'%s'", str);
+       pfree(str);
        return;
    }
 
@@ -449,6 +450,7 @@ expand_jsonval_strlit(StringInfo buf, JsonbValue *jsonval)
    /* And finally produce the quoted literal into the output StringInfo */
    appendStringInfo(buf, "%s%s%s", dqdelim.data, str, dqdelim.data);
    pfree(dqdelim.data);
+   pfree(str);
 }
 
 /*