Coverage Report

Created: 2026-12-01 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxslt/libxslt/attrvt.c
Line
Count
Source
1
/*
2
 * attrvt.c: Implementation of the XSL Transformation 1.0 engine
3
 *           attribute value template handling part.
4
 *
5
 * References:
6
 *   http://www.w3.org/TR/1999/REC-xslt-19991116
7
 *
8
 *   Michael Kay "XSLT Programmer's Reference" pp 637-643
9
 *   Writing Multiple Output Files
10
 *
11
 * See Copyright for the status of this software.
12
 *
13
 * daniel@veillard.com
14
 */
15
16
#define IN_LIBXSLT
17
#include "libxslt.h"
18
19
#include <string.h>
20
21
#include <libxml/xmlmemory.h>
22
#include <libxml/tree.h>
23
#include <libxml/xpath.h>
24
#include <libxml/xpathInternals.h>
25
#include "xslt.h"
26
#include "xsltutils.h"
27
#include "xsltInternals.h"
28
#include "templates.h"
29
30
#ifdef WITH_XSLT_DEBUG
31
#define WITH_XSLT_DEBUG_AVT
32
#endif
33
34
20.5k
#define MAX_AVT_SEG 10
35
36
typedef struct _xsltAttrVT xsltAttrVT;
37
typedef xsltAttrVT *xsltAttrVTPtr;
38
struct _xsltAttrVT {
39
    struct _xsltAttrVT *next; /* next xsltAttrVT */
40
    int nb_seg;   /* Number of segments */
41
    int max_seg;  /* max capacity before re-alloc needed */
42
    int strstart; /* is the start a string */
43
    /*
44
     * the namespaces in scope
45
     */
46
    xmlNsPtr *nsList;
47
    int nsNr;
48
    /*
49
     * the content is an alternate of string and xmlXPathCompExprPtr
50
     */
51
#if __STDC_VERSION__ >= 199901L
52
    /* Using a C99 flexible array member avoids false positives under UBSan */
53
    void *segments[];
54
#else
55
    void *segments[1];
56
#endif
57
};
58
59
/**
60
 * xsltNewAttrVT:
61
 * @style:  a XSLT process context
62
 *
63
 * Build a new xsltAttrVT structure
64
 *
65
 * Returns the structure or NULL in case of error
66
 */
67
static xsltAttrVTPtr
68
8.49k
xsltNewAttrVT(xsltStylesheetPtr style) {
69
8.49k
    xsltAttrVTPtr cur;
70
8.49k
    size_t size = sizeof(xsltAttrVT) + MAX_AVT_SEG * sizeof(void*);
71
72
8.49k
    cur = (xsltAttrVTPtr) xmlMalloc(size);
73
8.49k
    if (cur == NULL) {
74
266
  xsltTransformError(NULL, style, NULL,
75
266
    "xsltNewAttrVTPtr : malloc failed\n");
76
266
  if (style != NULL) style->errors++;
77
266
  return(NULL);
78
266
    }
79
8.23k
    memset(cur, 0, size);
80
81
8.23k
    cur->nb_seg = 0;
82
8.23k
    cur->max_seg = MAX_AVT_SEG;
83
8.23k
    cur->strstart = 0;
84
8.23k
    cur->next = style->attVTs;
85
    /*
86
     * Note: this pointer may be changed by a re-alloc within xsltCompileAttr,
87
     * so that code may change the stylesheet pointer also!
88
     */
89
8.23k
    style->attVTs = (xsltAttrVTPtr) cur;
90
91
8.23k
    return(cur);
92
8.49k
}
93
94
/**
95
 * xsltFreeAttrVT:
96
 * @avt: pointer to an xsltAttrVT structure
97
 *
98
 * Free up the memory associated to the attribute value template
99
 */
100
static void
101
8.23k
xsltFreeAttrVT(xsltAttrVTPtr avt) {
102
8.23k
    int i;
103
104
8.23k
    if (avt == NULL) return;
105
106
8.23k
    if (avt->strstart == 1) {
107
17.3k
  for (i = 0;i < avt->nb_seg; i += 2)
108
12.6k
      if (avt->segments[i] != NULL)
109
10.0k
    xmlFree((xmlChar *) avt->segments[i]);
110
13.6k
  for (i = 1;i < avt->nb_seg; i += 2)
111
9.00k
      xmlXPathFreeCompExpr((xmlXPathCompExprPtr) avt->segments[i]);
112
4.65k
    } else {
113
6.76k
  for (i = 0;i < avt->nb_seg; i += 2)
114
3.18k
      xmlXPathFreeCompExpr((xmlXPathCompExprPtr) avt->segments[i]);
115
6.13k
  for (i = 1;i < avt->nb_seg; i += 2)
116
2.56k
      if (avt->segments[i] != NULL)
117
1.36k
    xmlFree((xmlChar *) avt->segments[i]);
118
3.57k
    }
119
8.23k
    if (avt->nsList != NULL)
120
8.22k
        xmlFree(avt->nsList);
121
8.23k
    xmlFree(avt);
122
8.23k
}
123
124
/**
125
 * xsltFreeAVTList:
126
 * @avt: pointer to an list of AVT structures
127
 *
128
 * Free up the memory associated to the attribute value templates
129
 */
130
void
131
6.01k
xsltFreeAVTList(void *avt) {
132
6.01k
    xsltAttrVTPtr cur = (xsltAttrVTPtr) avt, next;
133
134
14.2k
    while (cur != NULL) {
135
8.23k
        next = cur->next;
136
8.23k
  xsltFreeAttrVT(cur);
137
8.23k
  cur = next;
138
8.23k
    }
139
6.01k
}
140
/**
141
 * xsltSetAttrVTsegment:
142
 * @ avt: pointer to an xsltAttrVT structure
143
 * @ val: the value to be set to the next available segment
144
 *
145
 * Within xsltCompileAttr there are several places where a value
146
 * needs to be added to the 'segments' array within the xsltAttrVT
147
 * structure, and at each place the allocated size may have to be
148
 * re-allocated.  This routine takes care of that situation.
149
 *
150
 * Returns the avt pointer, which may have been changed by a re-alloc
151
 */
152
static xsltAttrVTPtr
153
27.4k
xsltSetAttrVTsegment(xsltAttrVTPtr avt, void *val) {
154
27.4k
    if (avt->nb_seg >= avt->max_seg) {
155
1.26k
        size_t size = sizeof(xsltAttrVT) +
156
1.26k
                      (avt->max_seg + MAX_AVT_SEG) * sizeof(void *);
157
1.26k
  avt = (xsltAttrVTPtr) xmlRealloc(avt, size);
158
1.26k
  if (avt == NULL)
159
4
      return NULL;
160
1.25k
  memset(&avt->segments[avt->nb_seg], 0, MAX_AVT_SEG*sizeof(void *));
161
1.25k
  avt->max_seg += MAX_AVT_SEG;
162
1.25k
    }
163
27.4k
    avt->segments[avt->nb_seg++] = val;
164
27.4k
    return avt;
165
27.4k
}
166
167
/**
168
 * xsltCompileAttr:
169
 * @style:  a XSLT process context
170
 * @attr: the attribute coming from the stylesheet.
171
 *
172
 * Precompile an attribute in a stylesheet, basically it checks if it is
173
 * an attribute value template, and if yes, establish some structures needed
174
 * to process it at transformation time.
175
 */
176
void
177
59.2k
xsltCompileAttr(xsltStylesheetPtr style, xmlAttrPtr attr) {
178
59.2k
    const xmlChar *str;
179
59.2k
    const xmlChar *cur;
180
59.2k
    xmlChar *ret = NULL;
181
59.2k
    xmlChar *expr = NULL;
182
59.2k
    xmlXPathCompExprPtr comp = NULL;
183
59.2k
    xsltAttrVTPtr avt, tmp;
184
59.2k
    int i = 0, lastavt = 0;
185
186
59.2k
    if ((style == NULL) || (attr == NULL) || (attr->children == NULL))
187
0
        return;
188
59.2k
    if ((attr->children->type != XML_TEXT_NODE) ||
189
59.2k
        (attr->children->next != NULL)) {
190
0
        xsltTransformError(NULL, style, attr->parent,
191
0
      "Attribute '%s': The content is expected to be a single text "
192
0
      "node when compiling an AVT.\n", attr->name);
193
0
  style->errors++;
194
0
  return;
195
0
    }
196
59.2k
    str = attr->children->content;
197
59.2k
    if ((xmlStrchr(str, '{') == NULL) &&
198
52.9k
        (xmlStrchr(str, '}') == NULL)) return;
199
200
#ifdef WITH_XSLT_DEBUG_AVT
201
    xsltGenericDebug(xsltGenericDebugContext,
202
        "Found AVT %s: %s\n", attr->name, str);
203
#endif
204
8.49k
    if (attr->psvi != NULL) {
205
#ifdef WITH_XSLT_DEBUG_AVT
206
  xsltGenericDebug(xsltGenericDebugContext,
207
      "AVT %s: already compiled\n", attr->name);
208
#endif
209
0
        return;
210
0
    }
211
    /*
212
    * Create a new AVT object.
213
    */
214
8.49k
    avt = xsltNewAttrVT(style);
215
8.49k
    if (avt == NULL)
216
266
  return;
217
8.23k
    attr->psvi = avt;
218
219
8.23k
    avt->nsList = xmlGetNsList(attr->doc, attr->parent);
220
8.23k
    if (avt->nsList != NULL) {
221
36.9k
  while (avt->nsList[i] != NULL)
222
28.7k
      i++;
223
8.22k
    }
224
8.23k
    avt->nsNr = i;
225
226
8.23k
    cur = str;
227
15.6M
    while (*cur != 0) {
228
15.6M
  if (*cur == '{') {
229
18.5k
      if (*(cur+1) == '{') { /* escaped '{' */
230
3.22k
          cur++;
231
3.22k
    ret = xmlStrncat(ret, str, cur - str);
232
3.22k
    cur++;
233
3.22k
    str = cur;
234
3.22k
    continue;
235
3.22k
      }
236
15.2k
      if (*(cur+1) == '}') { /* skip empty AVT */
237
666
    ret = xmlStrncat(ret, str, cur - str);
238
666
          cur += 2;
239
666
    str = cur;
240
666
    continue;
241
666
      }
242
14.6k
      if ((ret != NULL) || (cur - str > 0)) {
243
8.71k
    ret = xmlStrncat(ret, str, cur - str);
244
8.71k
    str = cur;
245
8.71k
    if (avt->nb_seg == 0)
246
3.66k
        avt->strstart = 1;
247
8.71k
    if ((tmp = xsltSetAttrVTsegment(avt, (void *) ret)) == NULL)
248
1
        goto error;
249
8.71k
                avt = tmp;
250
8.71k
    ret = NULL;
251
8.71k
    lastavt = 0;
252
8.71k
      }
253
254
14.6k
      cur++;
255
89.8k
      while ((*cur != 0) && (*cur != '}')) {
256
    /* Need to check for literal (bug539741) */
257
75.1k
    if ((*cur == '\'') || (*cur == '"')) {
258
1.85k
        char delim = *(cur++);
259
24.9k
        while ((*cur != 0) && (*cur != delim))
260
23.1k
      cur++;
261
1.85k
        if (*cur != 0)
262
1.40k
      cur++; /* skip the ending delimiter */
263
1.85k
    } else
264
73.3k
        cur++;
265
75.1k
      }
266
14.6k
      if (*cur == 0) {
267
1.67k
          xsltTransformError(NULL, style, attr->parent,
268
1.67k
         "Attribute '%s': The AVT has an unmatched '{'.\n",
269
1.67k
         attr->name);
270
1.67k
    style->errors++;
271
1.67k
    goto error;
272
1.67k
      }
273
12.9k
      str++;
274
12.9k
      expr = xmlStrndup(str, cur - str);
275
12.9k
      if (expr == NULL) {
276
    /*
277
    * TODO: What needs to be done here?
278
    */
279
13
          XSLT_TODO
280
13
    goto error;
281
12.9k
      } else {
282
12.9k
    comp = xsltXPathCompile(style, expr);
283
12.9k
    if (comp == NULL) {
284
741
        xsltTransformError(NULL, style, attr->parent,
285
741
       "Attribute '%s': Failed to compile the expression "
286
741
       "'%s' in the AVT.\n", attr->name, expr);
287
741
        style->errors++;
288
741
        goto error;
289
741
    }
290
12.1k
    if (avt->nb_seg == 0)
291
1.14k
        avt->strstart = 0;
292
12.1k
    if (lastavt == 1) {
293
3.88k
        if ((tmp = xsltSetAttrVTsegment(avt, NULL)) == NULL) {
294
1
                        xsltTransformError(NULL, style, attr->parent,
295
1
                                           "out of memory\n");
296
1
            goto error;
297
1
                    }
298
3.88k
                    avt = tmp;
299
3.88k
    }
300
12.1k
    if ((tmp = xsltSetAttrVTsegment(avt, (void *) comp)) == NULL) {
301
1
                    xsltTransformError(NULL, style, attr->parent,
302
1
                                       "out of memory\n");
303
1
        goto error;
304
1
                }
305
12.1k
                avt = tmp;
306
12.1k
    lastavt = 1;
307
12.1k
    xmlFree(expr);
308
12.1k
    expr = NULL;
309
12.1k
                comp = NULL;
310
12.1k
      }
311
12.1k
      cur++;
312
12.1k
      str = cur;
313
15.6M
  } else if (*cur == '}') {
314
9.23k
      cur++;
315
9.23k
      if (*cur == '}') { /* escaped '}' */
316
6.70k
    ret = xmlStrncat(ret, str, cur - str);
317
6.70k
    cur++;
318
6.70k
    str = cur;
319
6.70k
    continue;
320
6.70k
      } else {
321
2.53k
          xsltTransformError(NULL, style, attr->parent,
322
2.53k
         "Attribute '%s': The AVT has an unmatched '}'.\n",
323
2.53k
         attr->name);
324
2.53k
    goto error;
325
2.53k
      }
326
9.23k
  } else
327
15.6M
      cur++;
328
15.6M
    }
329
3.26k
    if ((ret != NULL) || (cur - str > 0)) {
330
2.65k
  ret = xmlStrncat(ret, str, cur - str);
331
2.65k
  str = cur;
332
2.65k
  if (avt->nb_seg == 0)
333
995
      avt->strstart = 1;
334
2.65k
  if ((tmp = xsltSetAttrVTsegment(avt, (void *) ret)) == NULL)
335
1
      goto error;
336
2.65k
        avt = tmp;
337
2.65k
  ret = NULL;
338
2.65k
    }
339
340
8.23k
error:
341
8.23k
    if (avt == NULL) {
342
0
        xsltTransformError(NULL, style, attr->parent,
343
0
    "xsltCompileAttr: malloc problem\n");
344
8.23k
    } else {
345
8.23k
        if (attr->psvi != avt) {  /* may have changed from realloc */
346
323
            attr->psvi = avt;
347
      /*
348
       * This is a "hack", but I can't see any clean method of
349
       * doing it.  If a re-alloc has taken place, then the pointer
350
       * for this AVT may have changed.  style->attVTs was set by
351
       * xsltNewAttrVT, so it needs to be re-set to the new value!
352
       */
353
323
      style->attVTs = avt;
354
323
  }
355
8.23k
    }
356
8.23k
    if (ret != NULL)
357
321
  xmlFree(ret);
358
8.23k
    if (expr != NULL)
359
743
  xmlFree(expr);
360
8.23k
    if (comp != NULL)
361
2
        xmlXPathFreeCompExpr(comp);
362
8.23k
}
363
364
365
/**
366
 * xsltEvalAVT:
367
 * @ctxt: the XSLT transformation context
368
 * @avt: the prevompiled attribute value template info
369
 * @node: the node hosting the attribute
370
 *
371
 * Process the given AVT, and return the new string value.
372
 *
373
 * Returns the computed string value or NULL, must be deallocated by the
374
 *         caller.
375
 */
376
xmlChar *
377
11.0k
xsltEvalAVT(xsltTransformContextPtr ctxt, void *avt, xmlNodePtr node) {
378
11.0k
    xmlChar *ret = NULL, *tmp;
379
11.0k
    xmlXPathCompExprPtr comp;
380
11.0k
    xsltAttrVTPtr cur = (xsltAttrVTPtr) avt;
381
11.0k
    int i;
382
11.0k
    int str;
383
384
11.0k
    if ((ctxt == NULL) || (avt == NULL) || (node == NULL))
385
0
        return(NULL);
386
11.0k
    str = cur->strstart;
387
45.5k
    for (i = 0;i < cur->nb_seg;i++) {
388
34.4k
        if (str) {
389
18.7k
      ret = xmlStrcat(ret, (const xmlChar *) cur->segments[i]);
390
18.7k
  } else {
391
15.7k
      comp = (xmlXPathCompExprPtr) cur->segments[i];
392
15.7k
      tmp = xsltEvalXPathStringNs(ctxt, comp, cur->nsNr, cur->nsList);
393
15.7k
      if (tmp != NULL) {
394
14.9k
          if (ret != NULL) {
395
14.0k
        ret = xmlStrcat(ret, tmp);
396
14.0k
        xmlFree(tmp);
397
14.0k
    } else {
398
923
        ret = tmp;
399
923
    }
400
14.9k
      }
401
15.7k
  }
402
34.4k
  str = !str;
403
34.4k
    }
404
11.0k
    return(ret);
405
11.0k
}