*** pgsql/src/backend/utils/adt/xml.c 2009/04/08 21:51:38 1.86 --- pgsql/src/backend/utils/adt/xml.c 2009/05/12 20:17:40 1.87 *************** *** 7,13 **** * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * ! * $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.85 2009/03/27 18:56:57 tgl Exp $ * *------------------------------------------------------------------------- */ --- 7,13 ---- * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * ! * $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.86 2009/04/08 21:51:38 petere Exp $ * *------------------------------------------------------------------------- */ *************** xml_out_internal(xmltype *x, pg_enc targ *** 236,243 **** } appendStringInfoString(&buf, str + len); - if (version) - xmlFree(version); pfree(str); return buf.data; --- 236,241 ---- *************** xmlconcat(List *args) *** 455,461 **** if (!version) global_version_no_value = true; else if (!global_version) ! global_version = xmlStrdup(version); else if (xmlStrcmp(version, global_version) != 0) global_version_no_value = true; --- 453,459 ---- if (!version) global_version_no_value = true; else if (!global_version) ! global_version = version; else if (xmlStrcmp(version, global_version) != 0) global_version_no_value = true; *************** xml_init(void) *** 918,923 **** --- 916,939 ---- || xmlIsCombiningQ(c) \ || xmlIsExtender_ch(c)) + /* pnstrdup, but deal with xmlChar not char; len is measured in xmlChars */ + static xmlChar * + xml_pnstrdup(const xmlChar *str, size_t len) + { + xmlChar *result; + + result = (xmlChar *) palloc((len + 1) * sizeof(xmlChar)); + memcpy(result, str, len * sizeof(xmlChar)); + result[len] = 0; + return result; + } + + /* + * str is the null-terminated input string. Remaining arguments are + * output arguments; each can be NULL if value is not wanted. + * version and encoding are returned as locally-palloc'd strings. + * Result is 0 if OK, an error code if not. + */ static int parse_xml_decl(const xmlChar * str, size_t *lenp, xmlChar ** version, xmlChar ** encoding, int *standalone) *************** parse_xml_decl(const xmlChar * str, size *** 930,935 **** --- 946,952 ---- xml_init(); + /* Initialize output arguments to "not present" */ if (version) *version = NULL; if (encoding) *************** parse_xml_decl(const xmlChar * str, size *** 971,977 **** return XML_ERR_VERSION_MISSING; if (version) ! *version = xmlStrndup(p + 1, q - p - 1); p = q + 1; } else --- 988,994 ---- return XML_ERR_VERSION_MISSING; if (version) ! *version = xml_pnstrdup(p + 1, q - p - 1); p = q + 1; } else *************** parse_xml_decl(const xmlChar * str, size *** 999,1005 **** return XML_ERR_MISSING_ENCODING; if (encoding) ! *encoding = xmlStrndup(p + 1, q - p - 1); p = q + 1; } else --- 1016,1022 ---- return XML_ERR_MISSING_ENCODING; if (encoding) ! *encoding = xml_pnstrdup(p + 1, q - p - 1); p = q + 1; } else *************** xml_parse(text *data, XmlOptionType xmlo *** 1172,1179 **** xmlChar *version = NULL; int standalone = -1; - doc = xmlNewDoc(NULL); - res_code = parse_xml_decl(utf8string, &count, &version, NULL, &standalone); if (res_code != 0) --- 1189,1194 ---- *************** xml_parse(text *data, XmlOptionType xmlo *** 1181,1195 **** "invalid XML content: invalid XML declaration", res_code); res_code = xmlParseBalancedChunkMemory(doc, NULL, NULL, 0, utf8string + count, NULL); if (res_code != 0) xml_ereport(ERROR, ERRCODE_INVALID_XML_CONTENT, "invalid XML content"); - - doc->version = xmlStrdup(version); - doc->encoding = xmlStrdup((xmlChar *) "UTF-8"); - doc->standalone = standalone; } xmlFreeParserCtxt(ctxt); --- 1196,1211 ---- "invalid XML content: invalid XML declaration", res_code); + doc = xmlNewDoc(version); + Assert(doc->encoding == NULL); + doc->encoding = xmlStrdup((const xmlChar *) "UTF-8"); + doc->standalone = standalone; + res_code = xmlParseBalancedChunkMemory(doc, NULL, NULL, 0, utf8string + count, NULL); if (res_code != 0) xml_ereport(ERROR, ERRCODE_INVALID_XML_CONTENT, "invalid XML content"); } xmlFreeParserCtxt(ctxt);