Coverage Report

Created: 2024-04-26 11:09

/src/libxslt/libxslt/attrvt.c
Line
Count
Source (jump to first uncovered line)
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
0
#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
0
xsltNewAttrVT(xsltStylesheetPtr style) {
69
0
    xsltAttrVTPtr cur;
70
0
    size_t size = sizeof(xsltAttrVT) + MAX_AVT_SEG * sizeof(void*);
71
72
0
    cur = (xsltAttrVTPtr) xmlMalloc(size);
73
0
    if (cur == NULL) {
74
0
  xsltTransformError(NULL, style, NULL,
75
0
    "xsltNewAttrVTPtr : malloc failed\n");
76
0
  if (style != NULL) style->errors++;
77
0
  return(NULL);
78
0
    }
79
0
    memset(cur, 0, size);
80
81
0
    cur->nb_seg = 0;
82
0
    cur->max_seg = MAX_AVT_SEG;
83
0
    cur->strstart = 0;
84
0
    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
0
    style->attVTs = (xsltAttrVTPtr) cur;
90
91
0
    return(cur);
92
0
}
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
0
xsltFreeAttrVT(xsltAttrVTPtr avt) {
102
0
    int i;
103
104
0
    if (avt == NULL) return;
105
106
0
    if (avt->strstart == 1) {
107
0
  for (i = 0;i < avt->nb_seg; i += 2)
108
0
      if (avt->segments[i] != NULL)
109
0
    xmlFree((xmlChar *) avt->segments[i]);
110
0
  for (i = 1;i < avt->nb_seg; i += 2)
111
0
      xmlXPathFreeCompExpr((xmlXPathCompExprPtr) avt->segments[i]);
112
0
    } else {
113
0
  for (i = 0;i < avt->nb_seg; i += 2)
114
0
      xmlXPathFreeCompExpr((xmlXPathCompExprPtr) avt->segments[i]);
115
0
  for (i = 1;i < avt->nb_seg; i += 2)
116
0
      if (avt->segments[i] != NULL)
117
0
    xmlFree((xmlChar *) avt->segments[i]);
118
0
    }
119
0
    if (avt->nsList != NULL)
120
0
        xmlFree(avt->nsList);
121
0
    xmlFree(avt);
122
0
}
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
0
xsltFreeAVTList(void *avt) {
132
0
    xsltAttrVTPtr cur = (xsltAttrVTPtr) avt, next;
133
134
0
    while (cur != NULL) {
135
0
        next = cur->next;
136
0
  xsltFreeAttrVT(cur);
137
0
  cur = next;
138
0
    }
139
0
}
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
0
xsltSetAttrVTsegment(xsltAttrVTPtr avt, void *val) {
154
0
    if (avt->nb_seg >= avt->max_seg) {
155
0
        size_t size = sizeof(xsltAttrVT) +
156
0
                      (avt->max_seg + MAX_AVT_SEG) * sizeof(void *);
157
0
  xsltAttrVTPtr tmp = (xsltAttrVTPtr) xmlRealloc(avt, size);
158
0
  if (tmp == NULL) {
159
0
            xsltFreeAttrVT(avt);
160
0
      return NULL;
161
0
  }
162
0
        avt = tmp;
163
0
  memset(&avt->segments[avt->nb_seg], 0, MAX_AVT_SEG*sizeof(void *));
164
0
  avt->max_seg += MAX_AVT_SEG;
165
0
    }
166
0
    avt->segments[avt->nb_seg++] = val;
167
0
    return avt;
168
0
}
169
170
/**
171
 * xsltCompileAttr:
172
 * @style:  a XSLT process context
173
 * @attr: the attribute coming from the stylesheet.
174
 *
175
 * Precompile an attribute in a stylesheet, basically it checks if it is
176
 * an attribute value template, and if yes, establish some structures needed
177
 * to process it at transformation time.
178
 */
179
void
180
0
xsltCompileAttr(xsltStylesheetPtr style, xmlAttrPtr attr) {
181
0
    const xmlChar *str;
182
0
    const xmlChar *cur;
183
0
    xmlChar *ret = NULL;
184
0
    xmlChar *expr = NULL;
185
0
    xsltAttrVTPtr avt;
186
0
    int i = 0, lastavt = 0;
187
188
0
    if ((style == NULL) || (attr == NULL) || (attr->children == NULL))
189
0
        return;
190
0
    if ((attr->children->type != XML_TEXT_NODE) ||
191
0
        (attr->children->next != NULL)) {
192
0
        xsltTransformError(NULL, style, attr->parent,
193
0
      "Attribute '%s': The content is expected to be a single text "
194
0
      "node when compiling an AVT.\n", attr->name);
195
0
  style->errors++;
196
0
  return;
197
0
    }
198
0
    str = attr->children->content;
199
0
    if ((xmlStrchr(str, '{') == NULL) &&
200
0
        (xmlStrchr(str, '}') == NULL)) return;
201
202
#ifdef WITH_XSLT_DEBUG_AVT
203
    xsltGenericDebug(xsltGenericDebugContext,
204
        "Found AVT %s: %s\n", attr->name, str);
205
#endif
206
0
    if (attr->psvi != NULL) {
207
#ifdef WITH_XSLT_DEBUG_AVT
208
  xsltGenericDebug(xsltGenericDebugContext,
209
      "AVT %s: already compiled\n", attr->name);
210
#endif
211
0
        return;
212
0
    }
213
    /*
214
    * Create a new AVT object.
215
    */
216
0
    avt = xsltNewAttrVT(style);
217
0
    if (avt == NULL)
218
0
  return;
219
0
    attr->psvi = avt;
220
221
0
    avt->nsList = xmlGetNsList(attr->doc, attr->parent);
222
0
    if (avt->nsList != NULL) {
223
0
  while (avt->nsList[i] != NULL)
224
0
      i++;
225
0
    }
226
0
    avt->nsNr = i;
227
228
0
    cur = str;
229
0
    while (*cur != 0) {
230
0
  if (*cur == '{') {
231
0
      if (*(cur+1) == '{') { /* escaped '{' */
232
0
          cur++;
233
0
    ret = xmlStrncat(ret, str, cur - str);
234
0
    cur++;
235
0
    str = cur;
236
0
    continue;
237
0
      }
238
0
      if (*(cur+1) == '}') { /* skip empty AVT */
239
0
    ret = xmlStrncat(ret, str, cur - str);
240
0
          cur += 2;
241
0
    str = cur;
242
0
    continue;
243
0
      }
244
0
      if ((ret != NULL) || (cur - str > 0)) {
245
0
    ret = xmlStrncat(ret, str, cur - str);
246
0
    str = cur;
247
0
    if (avt->nb_seg == 0)
248
0
        avt->strstart = 1;
249
0
    if ((avt = xsltSetAttrVTsegment(avt, (void *) ret)) == NULL)
250
0
        goto error;
251
0
    ret = NULL;
252
0
    lastavt = 0;
253
0
      }
254
255
0
      cur++;
256
0
      while ((*cur != 0) && (*cur != '}')) {
257
    /* Need to check for literal (bug539741) */
258
0
    if ((*cur == '\'') || (*cur == '"')) {
259
0
        char delim = *(cur++);
260
0
        while ((*cur != 0) && (*cur != delim))
261
0
      cur++;
262
0
        if (*cur != 0)
263
0
      cur++; /* skip the ending delimiter */
264
0
    } else
265
0
        cur++;
266
0
      }
267
0
      if (*cur == 0) {
268
0
          xsltTransformError(NULL, style, attr->parent,
269
0
         "Attribute '%s': The AVT has an unmatched '{'.\n",
270
0
         attr->name);
271
0
    style->errors++;
272
0
    goto error;
273
0
      }
274
0
      str++;
275
0
      expr = xmlStrndup(str, cur - str);
276
0
      if (expr == NULL) {
277
    /*
278
    * TODO: What needs to be done here?
279
    */
280
0
          XSLT_TODO
281
0
    goto error;
282
0
      } else {
283
0
    xmlXPathCompExprPtr comp;
284
285
0
    comp = xsltXPathCompile(style, expr);
286
0
    if (comp == NULL) {
287
0
        xsltTransformError(NULL, style, attr->parent,
288
0
       "Attribute '%s': Failed to compile the expression "
289
0
       "'%s' in the AVT.\n", attr->name, expr);
290
0
        style->errors++;
291
0
        goto error;
292
0
    }
293
0
    if (avt->nb_seg == 0)
294
0
        avt->strstart = 0;
295
0
    if (lastavt == 1) {
296
0
        if ((avt = xsltSetAttrVTsegment(avt, NULL)) == NULL)
297
0
            goto error;
298
0
    }
299
0
    if ((avt = xsltSetAttrVTsegment(avt, (void *) comp)) == NULL)
300
0
        goto error;
301
0
    lastavt = 1;
302
0
    xmlFree(expr);
303
0
    expr = NULL;
304
0
      }
305
0
      cur++;
306
0
      str = cur;
307
0
  } else if (*cur == '}') {
308
0
      cur++;
309
0
      if (*cur == '}') { /* escaped '}' */
310
0
    ret = xmlStrncat(ret, str, cur - str);
311
0
    cur++;
312
0
    str = cur;
313
0
    continue;
314
0
      } else {
315
0
          xsltTransformError(NULL, style, attr->parent,
316
0
         "Attribute '%s': The AVT has an unmatched '}'.\n",
317
0
         attr->name);
318
0
    goto error;
319
0
      }
320
0
  } else
321
0
      cur++;
322
0
    }
323
0
    if ((ret != NULL) || (cur - str > 0)) {
324
0
  ret = xmlStrncat(ret, str, cur - str);
325
0
  str = cur;
326
0
  if (avt->nb_seg == 0)
327
0
      avt->strstart = 1;
328
0
  if ((avt = xsltSetAttrVTsegment(avt, (void *) ret)) == NULL)
329
0
      goto error;
330
0
  ret = NULL;
331
0
    }
332
333
0
error:
334
0
    if (avt == NULL) {
335
0
        xsltTransformError(NULL, style, attr->parent,
336
0
    "xsltCompileAttr: malloc problem\n");
337
0
    } else {
338
0
        if (attr->psvi != avt) {  /* may have changed from realloc */
339
0
            attr->psvi = avt;
340
      /*
341
       * This is a "hack", but I can't see any clean method of
342
       * doing it.  If a re-alloc has taken place, then the pointer
343
       * for this AVT may have changed.  style->attVTs was set by
344
       * xsltNewAttrVT, so it needs to be re-set to the new value!
345
       */
346
0
      style->attVTs = avt;
347
0
  }
348
0
    }
349
0
    if (ret != NULL)
350
0
  xmlFree(ret);
351
0
    if (expr != NULL)
352
0
  xmlFree(expr);
353
0
}
354
355
356
/**
357
 * xsltEvalAVT:
358
 * @ctxt: the XSLT transformation context
359
 * @avt: the prevompiled attribute value template info
360
 * @node: the node hosting the attribute
361
 *
362
 * Process the given AVT, and return the new string value.
363
 *
364
 * Returns the computed string value or NULL, must be deallocated by the
365
 *         caller.
366
 */
367
xmlChar *
368
0
xsltEvalAVT(xsltTransformContextPtr ctxt, void *avt, xmlNodePtr node) {
369
0
    xmlChar *ret = NULL, *tmp;
370
0
    xmlXPathCompExprPtr comp;
371
0
    xsltAttrVTPtr cur = (xsltAttrVTPtr) avt;
372
0
    int i;
373
0
    int str;
374
375
0
    if ((ctxt == NULL) || (avt == NULL) || (node == NULL))
376
0
        return(NULL);
377
0
    str = cur->strstart;
378
0
    for (i = 0;i < cur->nb_seg;i++) {
379
0
        if (str) {
380
0
      ret = xmlStrcat(ret, (const xmlChar *) cur->segments[i]);
381
0
  } else {
382
0
      comp = (xmlXPathCompExprPtr) cur->segments[i];
383
0
      tmp = xsltEvalXPathStringNs(ctxt, comp, cur->nsNr, cur->nsList);
384
0
      if (tmp != NULL) {
385
0
          if (ret != NULL) {
386
0
        ret = xmlStrcat(ret, tmp);
387
0
        xmlFree(tmp);
388
0
    } else {
389
0
        ret = tmp;
390
0
    }
391
0
      }
392
0
  }
393
0
  str = !str;
394
0
    }
395
0
    return(ret);
396
0
}