summaryrefslogtreecommitdiff
path: root/xpath.c
diff options
context:
space:
mode:
authorMike Hommey <mh@glandium.org>2004-11-11 12:53:54 +0000
committerMike Hommey <mh@glandium.org>2004-11-11 12:53:54 +0000
commitf51dd67f3a3f472af0620391eb588eeca4533689 (patch)
tree9184c396c489196608427d5fa35814e86a1e479f /xpath.c
parent9705f1a5e858108d21a0128556f42b25d16833cd (diff)
downloadlibxml2-f51dd67f3a3f472af0620391eb588eeca4533689.tar.gz
Load /tmp/tmp.n9GTkp/libxml2-2.6.16 intoupstream/2.6.16
packages/libxml2/branches/upstream/current.
Diffstat (limited to 'xpath.c')
-rw-r--r--xpath.c132
1 files changed, 83 insertions, 49 deletions
diff --git a/xpath.c b/xpath.c
index 89a8e28..fad8c26 100644
--- a/xpath.c
+++ b/xpath.c
@@ -207,7 +207,8 @@ static const char *xmlXPathErrorMessages[] = {
"Sub resource error\n",
"Undefined namespace prefix\n",
"Encoding error\n",
- "Char out of XML range\n"
+ "Char out of XML range\n",
+ "Invalid or inclomplete context\n"
};
@@ -774,6 +775,8 @@ xmlXPathDebugDumpObject(FILE *output, xmlXPathObjectPtr cur, int depth) {
int i;
char shift[100];
+ if (output == NULL) return;
+
for (i = 0;((i < depth) && (i < 25));i++)
shift[2 * i] = shift[2 * i + 1] = ' ';
shift[2 * i] = shift[2 * i + 1] = 0;
@@ -1062,6 +1065,8 @@ xmlXPathDebugDumpCompExpr(FILE *output, xmlXPathCompExprPtr comp,
int i;
char shift[100];
+ if ((output == NULL) || (comp == NULL)) return;
+
for (i = 0;((i < depth) && (i < 25));i++)
shift[2 * i] = shift[2 * i + 1] = ' ';
shift[2 * i] = shift[2 * i + 1] = 0;
@@ -1098,7 +1103,7 @@ valuePop(xmlXPathParserContextPtr ctxt)
{
xmlXPathObjectPtr ret;
- if (ctxt->valueNr <= 0)
+ if ((ctxt == NULL) || (ctxt->valueNr <= 0))
return (0);
ctxt->valueNr--;
if (ctxt->valueNr > 0)
@@ -1121,6 +1126,7 @@ valuePop(xmlXPathParserContextPtr ctxt)
extern int
valuePush(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr value)
{
+ if ((ctxt == NULL) || (value == NULL)) return(-1);
if (ctxt->valueNr >= ctxt->valueMax) {
xmlXPathObjectPtr *tmp;
@@ -1234,6 +1240,7 @@ xmlXPathPopNodeSet (xmlXPathParserContextPtr ctxt) {
xmlXPathObjectPtr obj;
xmlNodeSetPtr ret;
+ if (ctxt == NULL) return(NULL);
if (ctxt->value == NULL) {
xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND);
return(NULL);
@@ -1267,7 +1274,7 @@ xmlXPathPopExternal (xmlXPathParserContextPtr ctxt) {
xmlXPathObjectPtr obj;
void * ret;
- if (ctxt->value == NULL) {
+ if ((ctxt == NULL) || (ctxt->value == NULL)) {
xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND);
return(NULL);
}
@@ -1802,6 +1809,7 @@ int
xmlXPathNodeSetContains (xmlNodeSetPtr cur, xmlNodePtr val) {
int i;
+ if ((cur == NULL) || (val == NULL)) return(0);
if (val->type == XML_NAMESPACE_DECL) {
for (i = 0; i < cur->nodeNr; i++) {
if (cur->nodeTab[i]->type == XML_NAMESPACE_DECL) {
@@ -1837,7 +1845,9 @@ void
xmlXPathNodeSetAddNs(xmlNodeSetPtr cur, xmlNodePtr node, xmlNsPtr ns) {
int i;
- if ((ns == NULL) || (node == NULL) || (ns->type != XML_NAMESPACE_DECL) ||
+
+ if ((cur == NULL) || (ns == NULL) || (node == NULL) ||
+ (ns->type != XML_NAMESPACE_DECL) ||
(node->type != XML_ELEMENT_NODE))
return;
@@ -1892,7 +1902,7 @@ void
xmlXPathNodeSetAdd(xmlNodeSetPtr cur, xmlNodePtr val) {
int i;
- if (val == NULL) return;
+ if ((cur == NULL) || (val == NULL)) return;
#if 0
if ((val->type == XML_ELEMENT_NODE) && (val->name[0] == ' '))
@@ -1950,7 +1960,7 @@ xmlXPathNodeSetAdd(xmlNodeSetPtr cur, xmlNodePtr val) {
*/
void
xmlXPathNodeSetAddUnique(xmlNodeSetPtr cur, xmlNodePtr val) {
- if (val == NULL) return;
+ if ((cur == NULL) || (val == NULL)) return;
#if 0
if ((val->type == XML_ELEMENT_NODE) && (val->name[0] == ' '))
@@ -3928,21 +3938,11 @@ xmlXPathFreeContext(xmlXPathContextPtr ctxt) {
#define CHECK_CONTEXT(ctxt) \
- if (ctxt == NULL) { \
- xmlGenericError(xmlGenericErrorContext, \
- "%s:%d Internal error: no context\n", \
- __FILE__, __LINE__); \
- } \
- else if (ctxt->doc == NULL) { \
- xmlGenericError(xmlGenericErrorContext, \
- "%s:%d Internal error: no document\n", \
- __FILE__, __LINE__); \
- } \
- else if (ctxt->doc->children == NULL) { \
- xmlGenericError(xmlGenericErrorContext, \
- "%s:%d Internal error: document without root\n", \
- __FILE__, __LINE__); \
- } \
+ if ((ctxt == NULL) || (ctxt->doc == NULL) || \
+ (ctxt->doc->children == NULL)) { \
+ xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_INVALID_CTXT); \
+ return(NULL); \
+ }
/**
@@ -4868,6 +4868,7 @@ xmlXPathEqualValues(xmlXPathParserContextPtr ctxt) {
xmlXPathObjectPtr arg1, arg2, argtmp;
int ret = 0;
+ if ((ctxt == NULL) || (ctxt->context == NULL)) return(0);
arg2 = valuePop(ctxt);
arg1 = valuePop(ctxt);
if ((arg1 == NULL) || (arg2 == NULL)) {
@@ -4951,6 +4952,7 @@ xmlXPathNotEqualValues(xmlXPathParserContextPtr ctxt) {
xmlXPathObjectPtr arg1, arg2, argtmp;
int ret = 0;
+ if ((ctxt == NULL) || (ctxt->context == NULL)) return(0);
arg2 = valuePop(ctxt);
arg1 = valuePop(ctxt);
if ((arg1 == NULL) || (arg2 == NULL)) {
@@ -5050,6 +5052,7 @@ xmlXPathCompareValues(xmlXPathParserContextPtr ctxt, int inf, int strict) {
int ret = 0, arg1i = 0, arg2i = 0;
xmlXPathObjectPtr arg1, arg2;
+ if ((ctxt == NULL) || (ctxt->context == NULL)) return(0);
arg2 = valuePop(ctxt);
arg1 = valuePop(ctxt);
if ((arg1 == NULL) || (arg2 == NULL)) {
@@ -5161,6 +5164,7 @@ xmlXPathCompareValues(xmlXPathParserContextPtr ctxt, int inf, int strict) {
*/
void
xmlXPathValueFlipSign(xmlXPathParserContextPtr ctxt) {
+ if ((ctxt == NULL) || (ctxt->context == NULL)) return;
CAST_TO_NUMBER;
CHECK_TYPE(XPATH_NUMBER);
if (xmlXPathIsNaN(ctxt->value->floatval))
@@ -5348,6 +5352,7 @@ typedef xmlNodePtr (*xmlXPathTraversalFunction)
*/
xmlNodePtr
xmlXPathNextSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
+ if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
if (cur == NULL)
return(ctxt->context->node);
return(NULL);
@@ -5365,6 +5370,7 @@ xmlXPathNextSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
*/
xmlNodePtr
xmlXPathNextChild(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
+ if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
if (cur == NULL) {
if (ctxt->context->node == NULL) return(NULL);
switch (ctxt->context->node->type) {
@@ -5416,6 +5422,7 @@ xmlXPathNextChild(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
*/
xmlNodePtr
xmlXPathNextDescendant(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
+ if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
if (cur == NULL) {
if (ctxt->context->node == NULL)
return(NULL);
@@ -5478,6 +5485,7 @@ xmlXPathNextDescendant(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
*/
xmlNodePtr
xmlXPathNextDescendantOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
+ if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
if (cur == NULL) {
if (ctxt->context->node == NULL)
return(NULL);
@@ -5502,6 +5510,7 @@ xmlXPathNextDescendantOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
*/
xmlNodePtr
xmlXPathNextParent(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
+ if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
/*
* the parent of an attribute or namespace node is the element
* to which the attribute or namespace node is attached
@@ -5574,6 +5583,7 @@ xmlXPathNextParent(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
*/
xmlNodePtr
xmlXPathNextAncestor(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
+ if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
/*
* the parent of an attribute or namespace node is the element
* to which the attribute or namespace node is attached
@@ -5697,6 +5707,7 @@ xmlXPathNextAncestor(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
*/
xmlNodePtr
xmlXPathNextAncestorOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
+ if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
if (cur == NULL)
return(ctxt->context->node);
return(xmlXPathNextAncestor(ctxt, cur));
@@ -5715,6 +5726,7 @@ xmlXPathNextAncestorOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
*/
xmlNodePtr
xmlXPathNextFollowingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
+ if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) ||
(ctxt->context->node->type == XML_NAMESPACE_DECL))
return(NULL);
@@ -5739,6 +5751,7 @@ xmlXPathNextFollowingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
*/
xmlNodePtr
xmlXPathNextPrecedingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
+ if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) ||
(ctxt->context->node->type == XML_NAMESPACE_DECL))
return(NULL);
@@ -5769,6 +5782,7 @@ xmlXPathNextPrecedingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
*/
xmlNodePtr
xmlXPathNextFollowing(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
+ if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
if (cur != NULL && cur->children != NULL)
return cur->children ;
if (cur == NULL) cur = ctxt->context->node;
@@ -5824,6 +5838,7 @@ xmlXPathIsAncestor(xmlNodePtr ancestor, xmlNodePtr node) {
xmlNodePtr
xmlXPathNextPreceding(xmlXPathParserContextPtr ctxt, xmlNodePtr cur)
{
+ if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
if (cur == NULL)
cur = ctxt->context->node;
if (cur == NULL)
@@ -5864,6 +5879,7 @@ static xmlNodePtr
xmlXPathNextPrecedingInternal(xmlXPathParserContextPtr ctxt,
xmlNodePtr cur)
{
+ if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
if (cur == NULL) {
cur = ctxt->context->node;
if (cur == NULL)
@@ -5906,6 +5922,7 @@ xmlXPathNextPrecedingInternal(xmlXPathParserContextPtr ctxt,
*/
xmlNodePtr
xmlXPathNextNamespace(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
+ if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
if (ctxt->context->node->type != XML_ELEMENT_NODE) return(NULL);
if (ctxt->context->tmpNsList == NULL && cur != (xmlNodePtr) xmlXPathXMLNamespace) {
if (ctxt->context->tmpNsList != NULL)
@@ -5942,6 +5959,7 @@ xmlXPathNextNamespace(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
*/
xmlNodePtr
xmlXPathNextAttribute(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
+ if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
if (ctxt->context->node == NULL)
return(NULL);
if (ctxt->context->node->type != XML_ELEMENT_NODE)
@@ -5977,6 +5995,7 @@ xmlXPathNextAttribute(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
*/
void
xmlXPathRoot(xmlXPathParserContextPtr ctxt) {
+ if ((ctxt == NULL) || (ctxt->context == NULL)) return;
ctxt->context->node = (xmlNodePtr) ctxt->context->doc;
valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
}
@@ -6213,6 +6232,8 @@ void
xmlXPathLocalNameFunction(xmlXPathParserContextPtr ctxt, int nargs) {
xmlXPathObjectPtr cur;
+ if (ctxt == NULL) return;
+
if (nargs == 0) {
valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
nargs = 1;
@@ -6268,6 +6289,8 @@ void
xmlXPathNamespaceURIFunction(xmlXPathParserContextPtr ctxt, int nargs) {
xmlXPathObjectPtr cur;
+ if (ctxt == NULL) return;
+
if (nargs == 0) {
valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
nargs = 1;
@@ -6417,6 +6440,7 @@ void
xmlXPathStringFunction(xmlXPathParserContextPtr ctxt, int nargs) {
xmlXPathObjectPtr cur;
+ if (ctxt == NULL) return;
if (nargs == 0) {
valuePush(ctxt,
xmlXPathWrapString(
@@ -6448,6 +6472,8 @@ xmlXPathStringLengthFunction(xmlXPathParserContextPtr ctxt, int nargs) {
xmlXPathObjectPtr cur;
if (nargs == 0) {
+ if ((ctxt == NULL) || (ctxt->context == NULL))
+ return;
if (ctxt->context->node == NULL) {
valuePush(ctxt, xmlXPathNewFloat(0));
} else {
@@ -6481,6 +6507,7 @@ xmlXPathConcatFunction(xmlXPathParserContextPtr ctxt, int nargs) {
xmlXPathObjectPtr cur, newobj;
xmlChar *tmp;
+ if (ctxt == NULL) return;
if (nargs < 2) {
CHECK_ARITY(2);
}
@@ -6812,6 +6839,7 @@ xmlXPathNormalizeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
xmlBufferPtr target;
xmlChar blank;
+ if (ctxt == NULL) return;
if (nargs == 0) {
/* Use current context node */
valuePush(ctxt,
@@ -6883,7 +6911,7 @@ xmlXPathTranslateFunction(xmlXPathParserContextPtr ctxt, int nargs) {
xmlBufferPtr target;
int offset, max;
xmlChar ch;
- xmlChar *point;
+ const xmlChar *point;
xmlChar *cptr;
CHECK_ARITY(3);
@@ -7067,6 +7095,7 @@ xmlXPathNumberFunction(xmlXPathParserContextPtr ctxt, int nargs) {
xmlXPathObjectPtr cur;
double res;
+ if (ctxt == NULL) return;
if (nargs == 0) {
if (ctxt->context->node == NULL) {
valuePush(ctxt, xmlXPathNewFloat(0.0));
@@ -7344,6 +7373,7 @@ xmlXPathParseNCName(xmlXPathParserContextPtr ctxt) {
xmlChar *ret;
int count = 0;
+ if ((ctxt == NULL) || (ctxt->cur == NULL)) return(NULL);
/*
* Accelerator for simple ASCII names
*/
@@ -7424,6 +7454,7 @@ xmlXPathParseName(xmlXPathParserContextPtr ctxt) {
xmlChar *ret;
int count = 0;
+ if ((ctxt == NULL) || (ctxt->cur == NULL)) return(NULL);
/*
* Accelerator for simple ASCII names
*/
@@ -8012,35 +8043,33 @@ xmlXPathCompFilterExpr(xmlXPathParserContextPtr ctxt) {
static xmlChar *
xmlXPathScanName(xmlXPathParserContextPtr ctxt) {
- xmlChar buf[XML_MAX_NAMELEN];
- int len = 0;
+ int len = 0, l;
+ int c;
+ const xmlChar *cur;
+ xmlChar *ret;
- SKIP_BLANKS;
- if (!IS_ASCII_LETTER(CUR) && (CUR != '_') &&
- (CUR != ':')) {
+ cur = ctxt->cur;
+
+ c = CUR_CHAR(l);
+ if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */
+ (!IS_LETTER(c) && (c != '_') &&
+ (c != ':'))) {
return(NULL);
}
- while ((IS_ASCII_LETTER(NXT(len))) || (IS_ASCII_DIGIT(NXT(len))) ||
- (NXT(len) == '.') || (NXT(len) == '-') ||
- (NXT(len) == '_') || (NXT(len) == ':') ||
- (IS_COMBINING_CH(NXT(len))) ||
- (IS_EXTENDER_CH(NXT(len)))) {
- buf[len] = NXT(len);
- len++;
- if (len >= XML_MAX_NAMELEN) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlScanName: reached XML_MAX_NAMELEN limit\n");
- while ((IS_ASCII_LETTER(NXT(len))) || (IS_ASCII_DIGIT(NXT(len))) ||
- (NXT(len) == '.') || (NXT(len) == '-') ||
- (NXT(len) == '_') || (NXT(len) == ':') ||
- (IS_COMBINING_CH(NXT(len))) ||
- (IS_EXTENDER_CH(NXT(len))))
- len++;
- break;
- }
+ while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */
+ ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
+ (c == '.') || (c == '-') ||
+ (c == '_') || (c == ':') ||
+ (IS_COMBINING(c)) ||
+ (IS_EXTENDER(c)))) {
+ len += l;
+ NEXTL(l);
+ c = CUR_CHAR(l);
}
- return(xmlStrndup(buf, len));
+ ret = xmlStrndup(cur, ctxt->cur - cur);
+ ctxt->cur = cur;
+ return(ret);
}
/**
@@ -10408,10 +10437,14 @@ xmlXPathCompOpEval(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op)
case XPATH_OP_ARG:
bakd = ctxt->context->doc;
bak = ctxt->context->node;
+ pp = ctxt->context->proximityPosition;
+ cs = ctxt->context->contextSize;
if (op->ch1 != -1)
total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
- ctxt->context->doc = bakd;
+ ctxt->context->contextSize = cs;
+ ctxt->context->proximityPosition = pp;
ctxt->context->node = bak;
+ ctxt->context->doc = bakd;
CHECK_ERROR0;
if (op->ch2 != -1) {
total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
@@ -10917,7 +10950,7 @@ xmlXPathRunEval(xmlXPathParserContextPtr ctxt) {
*/
int
xmlXPathEvalPredicate(xmlXPathContextPtr ctxt, xmlXPathObjectPtr res) {
- if (res == NULL) return(0);
+ if ((ctxt == NULL) || (res == NULL)) return(0);
switch (res->type) {
case XPATH_BOOLEAN:
return(res->boolval);
@@ -10956,7 +10989,7 @@ xmlXPathEvalPredicate(xmlXPathContextPtr ctxt, xmlXPathObjectPtr res) {
int
xmlXPathEvaluatePredicateResult(xmlXPathParserContextPtr ctxt,
xmlXPathObjectPtr res) {
- if (res == NULL) return(0);
+ if ((ctxt == NULL) || (res == NULL)) return(0);
switch (res->type) {
case XPATH_BOOLEAN:
return(res->boolval);
@@ -11139,6 +11172,7 @@ xmlXPathCompiledEval(xmlXPathCompExprPtr comp, xmlXPathContextPtr ctx) {
*/
void
xmlXPathEvalExpr(xmlXPathParserContextPtr ctxt) {
+ if (ctxt == NULL) return;
xmlXPathCompileExpr(ctxt);
CHECK_ERROR;
xmlXPathRunEval(ctxt);