Coverage Report

Created: 2026-05-30 06:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxml2/xpointer.c
Line
Count
Source
1
/*
2
 * xpointer.c : Code to handle XML Pointer
3
 *
4
 * Base implementation was made accordingly to
5
 * W3C Candidate Recommendation 7 June 2000
6
 * http://www.w3.org/TR/2000/CR-xptr-20000607
7
 *
8
 * Added support for the element() scheme described in:
9
 * W3C Proposed Recommendation 13 November 2002
10
 * http://www.w3.org/TR/2002/PR-xptr-element-20021113/
11
 *
12
 * See Copyright for the status of this software.
13
 *
14
 * Author: Daniel Veillard
15
 */
16
17
/* To avoid EBCDIC trouble when parsing on zOS */
18
#if defined(__MVS__)
19
#pragma convert("ISO8859-1")
20
#endif
21
22
#define IN_LIBXML
23
#include "libxml.h"
24
25
/*
26
 * TODO: better handling of error cases, the full expression should
27
 *       be parsed beforehand instead of a progressive evaluation
28
 * TODO: Access into entities references are not supported now ...
29
 *       need a start to be able to pop out of entities refs since
30
 *       parent is the entity declaration, not the ref.
31
 */
32
33
#include <string.h>
34
#include <libxml/xpointer.h>
35
#include <libxml/xmlmemory.h>
36
#include <libxml/parserInternals.h>
37
#include <libxml/uri.h>
38
#include <libxml/xpath.h>
39
#include <libxml/xpathInternals.h>
40
#include <libxml/xmlerror.h>
41
42
#ifdef LIBXML_XPTR_ENABLED
43
44
/* Add support of the xmlns() xpointer scheme to initialize the namespaces */
45
#define XPTR_XMLNS_SCHEME
46
47
#include "private/error.h"
48
#include "private/xpath.h"
49
50
/************************************************************************
51
 *                  *
52
 *    Some factorized error routines        *
53
 *                  *
54
 ************************************************************************/
55
56
/**
57
 * Handle an XPointer error
58
 *
59
 * @param ctxt  an XPTR evaluation context
60
 * @param code  error code
61
 * @param msg  error message
62
 * @param extra  extra information
63
 */
64
static void LIBXML_ATTR_FORMAT(3,0)
65
xmlXPtrErr(xmlXPathParserContextPtr ctxt, int code,
66
           const char * msg, const xmlChar *extra)
67
0
{
68
0
    xmlStructuredErrorFunc serror = NULL;
69
0
    void *data = NULL;
70
0
    xmlNodePtr node = NULL;
71
0
    int res;
72
73
0
    if (ctxt == NULL)
74
0
        return;
75
    /* Only report the first error */
76
0
    if (ctxt->error != 0)
77
0
        return;
78
79
0
    ctxt->error = code;
80
81
0
    if (ctxt->context != NULL) {
82
0
        xmlErrorPtr err = &ctxt->context->lastError;
83
84
        /* cleanup current last error */
85
0
        xmlResetError(err);
86
87
0
        err->domain = XML_FROM_XPOINTER;
88
0
        err->code = code;
89
0
        err->level = XML_ERR_ERROR;
90
0
        err->str1 = (char *) xmlStrdup(ctxt->base);
91
0
        if (err->str1 == NULL) {
92
0
            xmlXPathPErrMemory(ctxt);
93
0
            return;
94
0
        }
95
0
        err->int1 = ctxt->cur - ctxt->base;
96
0
        err->node = ctxt->context->debugNode;
97
98
0
        serror = ctxt->context->error;
99
0
        data = ctxt->context->userData;
100
0
        node = ctxt->context->debugNode;
101
0
    }
102
103
0
    res = xmlRaiseError(serror, NULL, data, NULL, node,
104
0
                        XML_FROM_XPOINTER, code, XML_ERR_ERROR, NULL, 0,
105
0
                        (const char *) extra, (const char *) ctxt->base,
106
0
                        NULL, ctxt->cur - ctxt->base, 0,
107
0
                        msg, extra);
108
0
    if (res < 0)
109
0
        xmlXPathPErrMemory(ctxt);
110
0
}
111
112
/************************************************************************
113
 *                  *
114
 *    A few helper functions for child sequences    *
115
 *                  *
116
 ************************************************************************/
117
118
/**
119
 * @param cur  the node
120
 * @param no  the child number
121
 * @returns the `no`'th element child of `cur` or NULL
122
 */
123
static xmlNodePtr
124
0
xmlXPtrGetNthChild(xmlNodePtr cur, int no) {
125
0
    int i;
126
0
    if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL))
127
0
  return(cur);
128
0
    cur = cur->children;
129
0
    for (i = 0;i <= no;cur = cur->next) {
130
0
  if (cur == NULL)
131
0
      return(cur);
132
0
  if ((cur->type == XML_ELEMENT_NODE) ||
133
0
      (cur->type == XML_DOCUMENT_NODE) ||
134
0
      (cur->type == XML_HTML_DOCUMENT_NODE)) {
135
0
      i++;
136
0
      if (i == no)
137
0
    break;
138
0
  }
139
0
    }
140
0
    return(cur);
141
0
}
142
143
/************************************************************************
144
 *                  *
145
 *      The parser          *
146
 *                  *
147
 ************************************************************************/
148
149
static void xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name);
150
151
/*
152
 * Macros for accessing the content. Those should be used only by the parser,
153
 * and not exported.
154
 *
155
 * Dirty macros, i.e. one need to make assumption on the context to use them
156
 *
157
 *   CUR     returns the current xmlChar value, i.e. a 8 bit value
158
 *           in ISO-Latin or UTF-8.
159
 *           This should be used internally by the parser
160
 *           only to compare to ASCII values otherwise it would break when
161
 *           running with UTF-8 encoding.
162
 *   NXT(n)  returns the n'th next xmlChar. Same as CUR is should be used only
163
 *           to compare on ASCII based substring.
164
 *   SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
165
 *           strings within the parser.
166
 *   CURRENT Returns the current char value, with the full decoding of
167
 *           UTF-8 if we are using this mode. It returns an int.
168
 *   NEXT    Skip to the next character, this does the proper decoding
169
 *           in UTF-8 mode. It also pop-up unfinished entities on the fly.
170
 *           It returns the pointer to the current xmlChar.
171
 */
172
173
0
#define CUR (*ctxt->cur)
174
#define SKIP(val) ctxt->cur += (val)
175
0
#define NXT(val) ctxt->cur[(val)]
176
177
#define SKIP_BLANKS             \
178
0
    while (IS_BLANK_CH(*(ctxt->cur))) NEXT
179
180
#define CURRENT (*ctxt->cur)
181
0
#define NEXT ((*ctxt->cur) ?  ctxt->cur++: ctxt->cur)
182
183
/*
184
 * @param ctxt  the XPointer Parser context
185
 * @param index  the child number
186
 *
187
 * Move the current node of the nodeset on the stack to the
188
 * given child if found
189
 */
190
static void
191
0
xmlXPtrGetChildNo(xmlXPathParserContextPtr ctxt, int indx) {
192
0
    xmlNodePtr cur = NULL;
193
0
    xmlXPathObjectPtr obj;
194
0
    xmlNodeSetPtr oldset;
195
196
0
    CHECK_TYPE(XPATH_NODESET);
197
0
    obj = xmlXPathValuePop(ctxt);
198
0
    oldset = obj->nodesetval;
199
0
    if ((indx <= 0) || (oldset == NULL) || (oldset->nodeNr != 1)) {
200
0
  xmlXPathFreeObject(obj);
201
0
  xmlXPathValuePush(ctxt, xmlXPathNewNodeSet(NULL));
202
0
  return;
203
0
    }
204
0
    cur = xmlXPtrGetNthChild(oldset->nodeTab[0], indx);
205
0
    if (cur == NULL) {
206
0
  xmlXPathFreeObject(obj);
207
0
  xmlXPathValuePush(ctxt, xmlXPathNewNodeSet(NULL));
208
0
  return;
209
0
    }
210
0
    oldset->nodeTab[0] = cur;
211
0
    xmlXPathValuePush(ctxt, obj);
212
0
}
213
214
/**
215
 * XPtrPart ::= 'xpointer' '(' XPtrExpr ')'
216
 *            | Scheme '(' SchemeSpecificExpr ')'
217
 *
218
 * Scheme   ::=  NCName - 'xpointer' [VC: Non-XPointer schemes]
219
 *
220
 * SchemeSpecificExpr ::= StringWithBalancedParens
221
 *
222
 * StringWithBalancedParens ::=
223
 *              [^()]* ('(' StringWithBalancedParens ')' [^()]*)*
224
 *              [VC: Parenthesis escaping]
225
 *
226
 * XPtrExpr ::= Expr [VC: Parenthesis escaping]
227
 *
228
 * VC: Parenthesis escaping:
229
 *   The end of an XPointer part is signaled by the right parenthesis ")"
230
 *   character that is balanced with the left parenthesis "(" character
231
 *   that began the part. Any unbalanced parenthesis character inside the
232
 *   expression, even within literals, must be escaped with a circumflex (^)
233
 *   character preceding it. If the expression contains any literal
234
 *   occurrences of the circumflex, each must be escaped with an additional
235
 *   circumflex (that is, ^^). If the unescaped parentheses in the expression
236
 *   are not balanced, a syntax error results.
237
 *
238
 * Parse and evaluate an XPtrPart. Basically it generates the unescaped
239
 * string and if the scheme is 'xpointer' it will call the XPath interpreter.
240
 *
241
 * TODO: there is no new scheme registration mechanism
242
 *
243
 * @param ctxt  the XPointer Parser context
244
 * @param name  the preparsed Scheme for the XPtrPart
245
 */
246
247
static void
248
0
xmlXPtrEvalXPtrPart(xmlXPathParserContextPtr ctxt, xmlChar *name) {
249
0
    xmlChar *buffer, *cur;
250
0
    int len;
251
0
    int level;
252
253
0
    if (name == NULL)
254
0
    name = xmlXPathParseName(ctxt);
255
0
    if (name == NULL)
256
0
  XP_ERROR(XPATH_EXPR_ERROR);
257
258
0
    if (CUR != '(') {
259
0
        xmlFree(name);
260
0
  XP_ERROR(XPATH_EXPR_ERROR);
261
0
    }
262
0
    NEXT;
263
0
    level = 1;
264
265
0
    len = xmlStrlen(ctxt->cur);
266
    /* Overflow in xmlStrlen */
267
0
    if (len == 0 && ctxt->cur != NULL && *ctxt->cur != 0) {
268
0
        xmlXPathPErrMemory(ctxt);
269
0
        xmlFree(name);
270
0
        return;
271
0
    }
272
273
0
    len++;
274
0
    buffer = xmlMalloc(len);
275
0
    if (buffer == NULL) {
276
0
        xmlXPathPErrMemory(ctxt);
277
0
        xmlFree(name);
278
0
  return;
279
0
    }
280
281
0
    cur = buffer;
282
0
    while (CUR != 0) {
283
0
  if (CUR == ')') {
284
0
      level--;
285
0
      if (level == 0) {
286
0
    NEXT;
287
0
    break;
288
0
      }
289
0
  } else if (CUR == '(') {
290
0
      level++;
291
0
  } else if (CUR == '^') {
292
0
            if ((NXT(1) == ')') || (NXT(1) == '(') || (NXT(1) == '^')) {
293
0
                NEXT;
294
0
            }
295
0
  }
296
0
        *cur++ = CUR;
297
0
  NEXT;
298
0
    }
299
0
    *cur = 0;
300
301
0
    if ((level != 0) && (CUR == 0)) {
302
0
        xmlFree(name);
303
0
  xmlFree(buffer);
304
0
  XP_ERROR(XPTR_SYNTAX_ERROR);
305
0
    }
306
307
0
    if (xmlStrEqual(name, (xmlChar *) "xpointer") ||
308
0
        xmlStrEqual(name, (xmlChar *) "xpath1")) {
309
0
  const xmlChar *oldBase = ctxt->base;
310
0
  const xmlChar *oldCur = ctxt->cur;
311
312
0
  ctxt->cur = ctxt->base = buffer;
313
  /*
314
   * To evaluate an xpointer scheme element (4.3) we need:
315
   *   context initialized to the root
316
   *   context position initialized to 1
317
   *   context size initialized to 1
318
   */
319
0
  ctxt->context->node = (xmlNodePtr)ctxt->context->doc;
320
0
  ctxt->context->proximityPosition = 1;
321
0
  ctxt->context->contextSize = 1;
322
0
  xmlXPathEvalExpr(ctxt);
323
0
  ctxt->base = oldBase;
324
0
        ctxt->cur = oldCur;
325
0
    } else if (xmlStrEqual(name, (xmlChar *) "element")) {
326
0
  const xmlChar *oldBase = ctxt->base;
327
0
  const xmlChar *oldCur = ctxt->cur;
328
0
  xmlChar *name2;
329
330
0
  ctxt->cur = ctxt->base = buffer;
331
0
  if (buffer[0] == '/') {
332
0
      xmlXPathRoot(ctxt);
333
0
      xmlXPtrEvalChildSeq(ctxt, NULL);
334
0
  } else {
335
0
      name2 = xmlXPathParseName(ctxt);
336
0
      if (name2 == NULL) {
337
0
                ctxt->base = oldBase;
338
0
                ctxt->cur = oldCur;
339
0
    xmlFree(buffer);
340
0
                xmlFree(name);
341
0
    XP_ERROR(XPATH_EXPR_ERROR);
342
0
      }
343
0
      xmlXPtrEvalChildSeq(ctxt, name2);
344
0
  }
345
0
  ctxt->base = oldBase;
346
0
        ctxt->cur = oldCur;
347
0
#ifdef XPTR_XMLNS_SCHEME
348
0
    } else if (xmlStrEqual(name, (xmlChar *) "xmlns")) {
349
0
  const xmlChar *oldBase = ctxt->base;
350
0
  const xmlChar *oldCur = ctxt->cur;
351
0
  xmlChar *prefix;
352
353
0
  ctxt->cur = ctxt->base = buffer;
354
0
        prefix = xmlXPathParseNCName(ctxt);
355
0
  if (prefix == NULL) {
356
0
            ctxt->base = oldBase;
357
0
            ctxt->cur = oldCur;
358
0
      xmlFree(buffer);
359
0
      xmlFree(name);
360
0
      XP_ERROR(XPTR_SYNTAX_ERROR);
361
0
  }
362
0
  SKIP_BLANKS;
363
0
  if (CUR != '=') {
364
0
            ctxt->base = oldBase;
365
0
            ctxt->cur = oldCur;
366
0
      xmlFree(prefix);
367
0
      xmlFree(buffer);
368
0
      xmlFree(name);
369
0
      XP_ERROR(XPTR_SYNTAX_ERROR);
370
0
  }
371
0
  NEXT;
372
0
  SKIP_BLANKS;
373
374
0
  if (xmlXPathRegisterNs(ctxt->context, prefix, ctxt->cur) < 0)
375
0
            xmlXPathPErrMemory(ctxt);
376
0
        ctxt->base = oldBase;
377
0
        ctxt->cur = oldCur;
378
0
  xmlFree(prefix);
379
0
#endif /* XPTR_XMLNS_SCHEME */
380
0
    } else {
381
0
        xmlXPtrErr(ctxt, XML_XPTR_UNKNOWN_SCHEME,
382
0
       "unsupported scheme '%s'\n", name);
383
0
    }
384
0
    xmlFree(buffer);
385
0
    xmlFree(name);
386
0
}
387
388
/**
389
 * FullXPtr ::= XPtrPart (S? XPtrPart)*
390
 *
391
 * As the specs says:
392
 * -----------
393
 * When multiple XPtrParts are provided, they must be evaluated in
394
 * left-to-right order. If evaluation of one part fails, the nexti
395
 * is evaluated. The following conditions cause XPointer part failure:
396
 *
397
 * - An unknown scheme
398
 * - A scheme that does not locate any sub-resource present in the resource
399
 * - A scheme that is not applicable to the media type of the resource
400
 *
401
 * The XPointer application must consume a failed XPointer part and
402
 * attempt to evaluate the next one, if any. The result of the first
403
 * XPointer part whose evaluation succeeds is taken to be the fragment
404
 * located by the XPointer as a whole. If all the parts fail, the result
405
 * for the XPointer as a whole is a sub-resource error.
406
 * -----------
407
 *
408
 * Parse and evaluate a Full XPtr i.e. possibly a cascade of XPath based
409
 * expressions or other schemes.
410
 *
411
 * @param ctxt  the XPointer Parser context
412
 * @param name  the preparsed Scheme for the first XPtrPart
413
 */
414
static void
415
0
xmlXPtrEvalFullXPtr(xmlXPathParserContextPtr ctxt, xmlChar *name) {
416
0
    if (name == NULL)
417
0
    name = xmlXPathParseName(ctxt);
418
0
    if (name == NULL)
419
0
  XP_ERROR(XPATH_EXPR_ERROR);
420
0
    while (name != NULL) {
421
0
  ctxt->error = XPATH_EXPRESSION_OK;
422
0
  xmlXPtrEvalXPtrPart(ctxt, name);
423
424
  /* in case of syntax error, break here */
425
0
  if ((ctxt->error != XPATH_EXPRESSION_OK) &&
426
0
            (ctxt->error != XML_XPTR_UNKNOWN_SCHEME))
427
0
      return;
428
429
  /*
430
   * If the returned value is a non-empty nodeset
431
   * or location set, return here.
432
   */
433
0
  if (ctxt->value != NULL) {
434
0
      xmlXPathObjectPtr obj = ctxt->value;
435
436
0
      switch (obj->type) {
437
0
    case XPATH_NODESET: {
438
0
        xmlNodeSetPtr loc = ctxt->value->nodesetval;
439
0
        if ((loc != NULL) && (loc->nodeNr > 0))
440
0
      return;
441
0
        break;
442
0
    }
443
0
    default:
444
0
        break;
445
0
      }
446
447
      /*
448
       * Evaluating to improper values is equivalent to
449
       * a sub-resource error, clean-up the stack
450
       */
451
0
      do {
452
0
    obj = xmlXPathValuePop(ctxt);
453
0
    if (obj != NULL) {
454
0
        xmlXPathFreeObject(obj);
455
0
    }
456
0
      } while (obj != NULL);
457
0
  }
458
459
  /*
460
   * Is there another XPointer part.
461
   */
462
0
  SKIP_BLANKS;
463
0
  name = xmlXPathParseName(ctxt);
464
0
    }
465
0
}
466
467
/**
468
 *  ChildSeq ::= '/1' ('/' [0-9]*)*
469
 *             | Name ('/' [0-9]*)+
470
 *
471
 * Parse and evaluate a Child Sequence. This routine also handle the
472
 * case of a Bare Name used to get a document ID.
473
 *
474
 * @param ctxt  the XPointer Parser context
475
 * @param name  a possible ID name of the child sequence
476
 */
477
static void
478
0
xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name) {
479
    /*
480
     * XPointer don't allow by syntax to address in multirooted trees
481
     * this might prove useful in some cases, warn about it.
482
     */
483
0
    if ((name == NULL) && (CUR == '/') && (NXT(1) != '1')) {
484
0
        xmlXPtrErr(ctxt, XML_XPTR_CHILDSEQ_START,
485
0
       "warning: ChildSeq not starting by /1\n", NULL);
486
0
    }
487
488
0
    if (name != NULL) {
489
0
  xmlXPathValuePush(ctxt, xmlXPathNewString(name));
490
0
  xmlFree(name);
491
0
  xmlXPathIdFunction(ctxt, 1);
492
0
  CHECK_ERROR;
493
0
    }
494
495
0
    while (CUR == '/') {
496
0
  int child = 0, overflow = 0;
497
0
  NEXT;
498
499
0
  while ((CUR >= '0') && (CUR <= '9')) {
500
0
            int d = CUR - '0';
501
0
            if (child > INT_MAX / 10)
502
0
                overflow = 1;
503
0
            else
504
0
                child *= 10;
505
0
            if (child > INT_MAX - d)
506
0
                overflow = 1;
507
0
            else
508
0
                child += d;
509
0
      NEXT;
510
0
  }
511
0
        if (overflow)
512
0
            child = 0;
513
0
  xmlXPtrGetChildNo(ctxt, child);
514
0
    }
515
0
}
516
517
518
/**
519
 *  XPointer ::= Name
520
 *             | ChildSeq
521
 *             | FullXPtr
522
 *
523
 * Parse and evaluate an XPointer
524
 *
525
 * @param ctxt  the XPointer Parser context
526
 */
527
static void
528
0
xmlXPtrEvalXPointer(xmlXPathParserContextPtr ctxt) {
529
0
    if (ctxt->valueTab == NULL) {
530
  /* Allocate the value stack */
531
0
  ctxt->valueTab = (xmlXPathObjectPtr *)
532
0
       xmlMalloc(10 * sizeof(xmlXPathObjectPtr));
533
0
  if (ctxt->valueTab == NULL) {
534
0
      xmlXPathPErrMemory(ctxt);
535
0
      return;
536
0
  }
537
0
  ctxt->valueNr = 0;
538
0
  ctxt->valueMax = 10;
539
0
  ctxt->value = NULL;
540
0
    }
541
0
    SKIP_BLANKS;
542
0
    if (CUR == '/') {
543
0
  xmlXPathRoot(ctxt);
544
0
        xmlXPtrEvalChildSeq(ctxt, NULL);
545
0
    } else {
546
0
  xmlChar *name;
547
548
0
  name = xmlXPathParseName(ctxt);
549
0
  if (name == NULL)
550
0
      XP_ERROR(XPATH_EXPR_ERROR);
551
0
  if (CUR == '(') {
552
0
      xmlXPtrEvalFullXPtr(ctxt, name);
553
      /* Short evaluation */
554
0
      return;
555
0
  } else {
556
      /* this handle both Bare Names and Child Sequences */
557
0
      xmlXPtrEvalChildSeq(ctxt, name);
558
0
  }
559
0
    }
560
0
    SKIP_BLANKS;
561
0
    if (CUR != 0)
562
0
  XP_ERROR(XPATH_EXPR_ERROR);
563
0
}
564
565
566
/************************************************************************
567
 *                  *
568
 *      General routines        *
569
 *                  *
570
 ************************************************************************/
571
572
/**
573
 * Create a new XPointer context
574
 *
575
 * @deprecated Same as xmlXPathNewContext.
576
 *
577
 * @param doc  the XML document
578
 * @param here  unused
579
 * @param origin  unused
580
 * @returns the xmlXPathContext just allocated.
581
 */
582
xmlXPathContext *
583
0
xmlXPtrNewContext(xmlDoc *doc, xmlNode *here, xmlNode *origin) {
584
0
    xmlXPathContextPtr ret;
585
0
    (void) here;
586
0
    (void) origin;
587
588
0
    ret = xmlXPathNewContext(doc);
589
0
    if (ret == NULL)
590
0
  return(ret);
591
592
0
    return(ret);
593
0
}
594
595
/**
596
 * Evaluate an XPointer expression.
597
 *
598
 * This function can only return nodesets. The caller has to
599
 * free the object.
600
 *
601
 * @param str  an XPointer expression
602
 * @param ctx  an XPath context
603
 * @returns the xmlXPathObject resulting from the evaluation or NULL
604
 * in case of error.
605
 */
606
xmlXPathObject *
607
0
xmlXPtrEval(const xmlChar *str, xmlXPathContext *ctx) {
608
0
    xmlXPathParserContextPtr ctxt;
609
0
    xmlXPathObjectPtr res = NULL, tmp;
610
0
    xmlXPathObjectPtr init = NULL;
611
0
    int stack = 0;
612
613
0
    xmlInitParser();
614
615
0
    if ((ctx == NULL) || (str == NULL))
616
0
  return(NULL);
617
618
0
    xmlResetError(&ctx->lastError);
619
620
0
    ctxt = xmlXPathNewParserContext(str, ctx);
621
0
    if (ctxt == NULL) {
622
0
        xmlXPathErrMemory(ctx);
623
0
  return(NULL);
624
0
    }
625
0
    xmlXPtrEvalXPointer(ctxt);
626
0
    if (ctx->lastError.code != XML_ERR_OK)
627
0
        goto error;
628
629
0
    if ((ctxt->value != NULL) &&
630
0
  (ctxt->value->type != XPATH_NODESET)) {
631
0
        xmlXPtrErr(ctxt, XML_XPTR_EVAL_FAILED,
632
0
    "xmlXPtrEval: evaluation failed to return a node set\n",
633
0
       NULL);
634
0
    } else {
635
0
  res = xmlXPathValuePop(ctxt);
636
0
    }
637
638
0
    do {
639
0
        tmp = xmlXPathValuePop(ctxt);
640
0
  if (tmp != NULL) {
641
0
      if (tmp != init) {
642
0
    if (tmp->type == XPATH_NODESET) {
643
        /*
644
         * Evaluation may push a root nodeset which is unused
645
         */
646
0
        xmlNodeSetPtr set;
647
0
        set = tmp->nodesetval;
648
0
        if ((set == NULL) || (set->nodeNr != 1) ||
649
0
      (set->nodeTab[0] != (xmlNodePtr) ctx->doc))
650
0
      stack++;
651
0
    } else
652
0
        stack++;
653
0
      }
654
0
      xmlXPathFreeObject(tmp);
655
0
        }
656
0
    } while (tmp != NULL);
657
0
    if (stack != 0) {
658
0
        xmlXPtrErr(ctxt, XML_XPTR_EXTRA_OBJECTS,
659
0
       "xmlXPtrEval: object(s) left on the eval stack\n",
660
0
       NULL);
661
0
    }
662
0
    if (ctx->lastError.code != XML_ERR_OK) {
663
0
  xmlXPathFreeObject(res);
664
0
  res = NULL;
665
0
    }
666
667
0
error:
668
0
    xmlXPathFreeParserContext(ctxt);
669
0
    return(res);
670
0
}
671
672
#endif
673