add a few missing null pointer checks, and fix check vs dereference order in some...
[project/uci.git] / list.c
1 /*
2 * libuci - Library for the Unified Configuration Interface
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 2.1
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 */
14
15 static void uci_list_set_pos(struct uci_list *head, struct uci_list *ptr, int pos)
16 {
17 struct uci_list *new_head = head;
18 struct uci_element *p = NULL;
19
20 uci_list_del(ptr);
21 uci_foreach_element(head, p) {
22 if (pos-- <= 0)
23 break;
24 new_head = &p->list;
25 }
26
27 uci_list_add(new_head->next, ptr);
28 }
29
30 static inline void uci_list_fixup(struct uci_list *ptr)
31 {
32 ptr->prev->next = ptr;
33 ptr->next->prev = ptr;
34 }
35
36 /*
37 * uci_alloc_generic allocates a new uci_element with payload
38 * payload is appended to the struct to save memory and reduce fragmentation
39 */
40 __private struct uci_element *
41 uci_alloc_generic(struct uci_context *ctx, int type, const char *name, int size)
42 {
43 struct uci_element *e;
44 int datalen = size;
45 void *ptr;
46
47 ptr = uci_malloc(ctx, datalen);
48 e = (struct uci_element *) ptr;
49 e->type = type;
50 if (name) {
51 UCI_TRAP_SAVE(ctx, error);
52 e->name = uci_strdup(ctx, name);
53 UCI_TRAP_RESTORE(ctx);
54 }
55 uci_list_init(&e->list);
56 goto done;
57
58 error:
59 free(ptr);
60 UCI_THROW(ctx, ctx->err);
61
62 done:
63 return e;
64 }
65
66 __private void
67 uci_free_element(struct uci_element *e)
68 {
69 if (e->name)
70 free(e->name);
71 if (!uci_list_empty(&e->list))
72 uci_list_del(&e->list);
73 free(e);
74 }
75
76 static struct uci_option *
77 uci_alloc_option(struct uci_section *s, const char *name, const char *value)
78 {
79 struct uci_package *p = s->package;
80 struct uci_context *ctx = p->ctx;
81 struct uci_option *o;
82
83 o = uci_alloc_element(ctx, option, name, strlen(value) + 1);
84 o->type = UCI_TYPE_STRING;
85 o->v.string = uci_dataptr(o);
86 o->section = s;
87 strcpy(o->v.string, value);
88 uci_list_add(&s->options, &o->e.list);
89
90 return o;
91 }
92
93 static inline void
94 uci_free_option(struct uci_option *o)
95 {
96 struct uci_element *e, *tmp;
97
98 switch(o->type) {
99 case UCI_TYPE_STRING:
100 if ((o->v.string != uci_dataptr(o)) &&
101 (o->v.string != NULL))
102 free(o->v.string);
103 break;
104 case UCI_TYPE_LIST:
105 uci_foreach_element_safe(&o->v.list, tmp, e) {
106 uci_free_element(e);
107 }
108 break;
109 default:
110 break;
111 }
112 uci_free_element(&o->e);
113 }
114
115 static struct uci_option *
116 uci_alloc_list(struct uci_section *s, const char *name)
117 {
118 struct uci_package *p = s->package;
119 struct uci_context *ctx = p->ctx;
120 struct uci_option *o;
121
122 o = uci_alloc_element(ctx, option, name, 0);
123 o->type = UCI_TYPE_LIST;
124 o->section = s;
125 uci_list_init(&o->v.list);
126 uci_list_add(&s->options, &o->e.list);
127
128 return o;
129 }
130
131 /* Based on an efficient hash function published by D. J. Bernstein */
132 static unsigned int djbhash(unsigned int hash, char *str)
133 {
134 int len = strlen(str);
135 int i;
136
137 /* initial value */
138 if (hash == ~0)
139 hash = 5381;
140
141 for(i = 0; i < len; i++) {
142 hash = ((hash << 5) + hash) + str[i];
143 }
144 return (hash & 0x7FFFFFFF);
145 }
146
147 /* fix up an unnamed section, e.g. after adding options to it */
148 __private void uci_fixup_section(struct uci_context *ctx, struct uci_section *s)
149 {
150 unsigned int hash = ~0;
151 struct uci_element *e;
152 char buf[16];
153
154 if (!s || s->e.name)
155 return;
156
157 /*
158 * Generate a name for unnamed sections. This is used as reference
159 * when locating or updating the section from apps/scripts.
160 * To make multiple concurrent versions somewhat safe for updating,
161 * the name is generated from a hash of its type and name/value
162 * pairs of its option, and it is prefixed by a counter value.
163 * If the order of the unnamed sections changes for some reason,
164 * updates to them will be rejected.
165 */
166 hash = djbhash(hash, s->type);
167 uci_foreach_element(&s->options, e) {
168 struct uci_option *o;
169 hash = djbhash(hash, e->name);
170 o = uci_to_option(e);
171 switch(o->type) {
172 case UCI_TYPE_STRING:
173 hash = djbhash(hash, o->v.string);
174 break;
175 default:
176 break;
177 }
178 }
179 sprintf(buf, "cfg%02x%04x", ++s->package->n_section, hash % (1 << 16));
180 s->e.name = uci_strdup(ctx, buf);
181 }
182
183 static struct uci_section *
184 uci_alloc_section(struct uci_package *p, const char *type, const char *name)
185 {
186 struct uci_context *ctx = p->ctx;
187 struct uci_section *s;
188
189 if (name && !name[0])
190 name = NULL;
191
192 s = uci_alloc_element(ctx, section, name, strlen(type) + 1);
193 uci_list_init(&s->options);
194 s->type = uci_dataptr(s);
195 s->package = p;
196 strcpy(s->type, type);
197 if (name == NULL)
198 s->anonymous = true;
199 p->n_section++;
200
201 uci_list_add(&p->sections, &s->e.list);
202
203 return s;
204 }
205
206 static void
207 uci_free_section(struct uci_section *s)
208 {
209 struct uci_element *o, *tmp;
210
211 uci_foreach_element_safe(&s->options, tmp, o) {
212 uci_free_option(uci_to_option(o));
213 }
214 if ((s->type != uci_dataptr(s)) &&
215 (s->type != NULL))
216 free(s->type);
217 uci_free_element(&s->e);
218 }
219
220 __plugin struct uci_package *
221 uci_alloc_package(struct uci_context *ctx, const char *name)
222 {
223 struct uci_package *p;
224
225 p = uci_alloc_element(ctx, package, name, 0);
226 p->ctx = ctx;
227 uci_list_init(&p->sections);
228 uci_list_init(&p->delta);
229 uci_list_init(&p->saved_delta);
230 return p;
231 }
232
233 __private void
234 uci_free_package(struct uci_package **package)
235 {
236 struct uci_element *e, *tmp;
237 struct uci_package *p = *package;
238
239 if(!p)
240 return;
241
242 if (p->path)
243 free(p->path);
244 uci_foreach_element_safe(&p->sections, tmp, e) {
245 uci_free_section(uci_to_section(e));
246 }
247 uci_foreach_element_safe(&p->delta, tmp, e) {
248 uci_free_delta(uci_to_delta(e));
249 }
250 uci_foreach_element_safe(&p->saved_delta, tmp, e) {
251 uci_free_delta(uci_to_delta(e));
252 }
253 uci_free_element(&p->e);
254 *package = NULL;
255 }
256
257 static void
258 uci_free_any(struct uci_element **e)
259 {
260 switch((*e)->type) {
261 case UCI_TYPE_SECTION:
262 uci_free_section(uci_to_section(*e));
263 break;
264 case UCI_TYPE_OPTION:
265 uci_free_option(uci_to_option(*e));
266 break;
267 default:
268 break;
269 }
270 *e = NULL;
271 }
272
273 __private struct uci_element *
274 uci_lookup_list(struct uci_list *list, const char *name)
275 {
276 struct uci_element *e;
277
278 uci_foreach_element(list, e) {
279 if (!strcmp(e->name, name))
280 return e;
281 }
282 return NULL;
283 }
284
285 static struct uci_element *
286 uci_lookup_ext_section(struct uci_context *ctx, struct uci_ptr *ptr)
287 {
288 char *idxstr, *t, *section, *name;
289 struct uci_element *e = NULL;
290 struct uci_section *s;
291 int idx, c;
292
293 section = uci_strdup(ctx, ptr->section);
294 name = idxstr = section + 1;
295
296 if (section[0] != '@')
297 goto error;
298
299 /* parse the section index part */
300 idxstr = strchr(idxstr, '[');
301 if (!idxstr)
302 goto error;
303 *idxstr = 0;
304 idxstr++;
305
306 t = strchr(idxstr, ']');
307 if (!t)
308 goto error;
309 if (t[1] != 0)
310 goto error;
311 *t = 0;
312
313 t = NULL;
314 idx = strtol(idxstr, &t, 10);
315 if (t && *t)
316 goto error;
317
318 if (!*name)
319 name = NULL;
320 else if (!uci_validate_type(name))
321 goto error;
322
323 /* if the given index is negative, it specifies the section number from
324 * the end of the list */
325 if (idx < 0) {
326 c = 0;
327 uci_foreach_element(&ptr->p->sections, e) {
328 s = uci_to_section(e);
329 if (name && (strcmp(s->type, name) != 0))
330 continue;
331
332 c++;
333 }
334 idx += c;
335 }
336
337 c = 0;
338 uci_foreach_element(&ptr->p->sections, e) {
339 s = uci_to_section(e);
340 if (name && (strcmp(s->type, name) != 0))
341 continue;
342
343 if (idx == c)
344 goto done;
345 c++;
346 }
347 e = NULL;
348 goto done;
349
350 error:
351 e = NULL;
352 memset(ptr, 0, sizeof(struct uci_ptr));
353 UCI_THROW(ctx, UCI_ERR_INVAL);
354 done:
355 free(section);
356 if (e)
357 ptr->section = e->name;
358 return e;
359 }
360
361 int
362 uci_lookup_next(struct uci_context *ctx, struct uci_element **e, struct uci_list *list, const char *name)
363 {
364 UCI_HANDLE_ERR(ctx);
365
366 *e = uci_lookup_list(list, name);
367 if (!*e)
368 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
369
370 return 0;
371 }
372
373 int
374 uci_lookup_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str, bool extended)
375 {
376 struct uci_element *e;
377
378 UCI_HANDLE_ERR(ctx);
379 UCI_ASSERT(ctx, ptr != NULL);
380
381 if (str)
382 UCI_INTERNAL(uci_parse_ptr, ctx, ptr, str);
383
384 ptr->flags |= UCI_LOOKUP_DONE;
385
386 /* look up the package first */
387 if (ptr->p)
388 e = &ptr->p->e;
389 else
390 e = uci_lookup_list(&ctx->root, ptr->package);
391
392 if (!e) {
393 UCI_INTERNAL(uci_load, ctx, ptr->package, &ptr->p);
394 if (!ptr->p)
395 goto notfound;
396 ptr->last = &ptr->p->e;
397 } else {
398 ptr->p = uci_to_package(e);
399 ptr->last = e;
400 }
401
402 if (!ptr->section && !ptr->s)
403 goto complete;
404
405 /* if the section name validates as a regular name, pass through
406 * to the regular uci_lookup function call */
407 if (ptr->s) {
408 e = &ptr->s->e;
409 } else if (ptr->flags & UCI_LOOKUP_EXTENDED) {
410 if (extended)
411 e = uci_lookup_ext_section(ctx, ptr);
412 else
413 UCI_THROW(ctx, UCI_ERR_INVAL);
414 } else {
415 e = uci_lookup_list(&ptr->p->sections, ptr->section);
416 }
417
418 if (!e)
419 goto abort;
420
421 ptr->last = e;
422 ptr->s = uci_to_section(e);
423
424 if (ptr->option) {
425 e = uci_lookup_list(&ptr->s->options, ptr->option);
426 if (!e)
427 goto abort;
428
429 ptr->o = uci_to_option(e);
430 ptr->last = e;
431 }
432
433 complete:
434 ptr->flags |= UCI_LOOKUP_COMPLETE;
435 abort:
436 return 0;
437
438 notfound:
439 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
440 return 0;
441 }
442
443 __private struct uci_element *
444 uci_expand_ptr(struct uci_context *ctx, struct uci_ptr *ptr, bool complete)
445 {
446 UCI_ASSERT(ctx, ptr != NULL);
447
448 if (!(ptr->flags & UCI_LOOKUP_DONE))
449 UCI_INTERNAL(uci_lookup_ptr, ctx, ptr, NULL, 1);
450 if (complete && !(ptr->flags & UCI_LOOKUP_COMPLETE))
451 UCI_THROW(ctx, UCI_ERR_NOTFOUND);
452 UCI_ASSERT(ctx, ptr->p != NULL);
453
454 /* fill in missing string info */
455 if (ptr->p && !ptr->package)
456 ptr->package = ptr->p->e.name;
457 if (ptr->s && !ptr->section)
458 ptr->section = ptr->s->e.name;
459 if (ptr->o && !ptr->option)
460 ptr->option = ptr->o->e.name;
461
462 if (ptr->o)
463 return &ptr->o->e;
464 if (ptr->s)
465 return &ptr->s->e;
466 if (ptr->p)
467 return &ptr->p->e;
468 else
469 return NULL;
470 }
471
472 static void uci_add_element_list(struct uci_context *ctx, struct uci_ptr *ptr, bool internal)
473 {
474 struct uci_element *e;
475 struct uci_package *p;
476
477 p = ptr->p;
478 if (!internal && p->has_delta)
479 uci_add_delta(ctx, &p->delta, UCI_CMD_LIST_ADD, ptr->section, ptr->option, ptr->value);
480
481 e = uci_alloc_generic(ctx, UCI_TYPE_ITEM, ptr->value, sizeof(struct uci_option));
482 uci_list_add(&ptr->o->v.list, &e->list);
483 }
484
485 int uci_rename(struct uci_context *ctx, struct uci_ptr *ptr)
486 {
487 /* NB: UCI_INTERNAL use means without delta tracking */
488 bool internal = ctx && ctx->internal;
489 struct uci_element *e;
490 struct uci_package *p;
491 char *n;
492
493 UCI_HANDLE_ERR(ctx);
494
495 e = uci_expand_ptr(ctx, ptr, true);
496 p = ptr->p;
497
498 UCI_ASSERT(ctx, ptr->s);
499 UCI_ASSERT(ctx, ptr->value);
500
501 if (!internal && p->has_delta)
502 uci_add_delta(ctx, &p->delta, UCI_CMD_RENAME, ptr->section, ptr->option, ptr->value);
503
504 n = uci_strdup(ctx, ptr->value);
505 if (e->name)
506 free(e->name);
507 e->name = n;
508
509 if (e->type == UCI_TYPE_SECTION)
510 uci_to_section(e)->anonymous = false;
511
512 return 0;
513 }
514
515 int uci_reorder_section(struct uci_context *ctx, struct uci_section *s, int pos)
516 {
517 struct uci_package *p = s->package;
518 char order[32];
519
520 UCI_HANDLE_ERR(ctx);
521
522 uci_list_set_pos(&s->package->sections, &s->e.list, pos);
523 if (!ctx->internal && p->has_delta) {
524 sprintf(order, "%d", pos);
525 uci_add_delta(ctx, &p->delta, UCI_CMD_REORDER, s->e.name, NULL, order);
526 }
527
528 return 0;
529 }
530
531 int uci_add_section(struct uci_context *ctx, struct uci_package *p, const char *type, struct uci_section **res)
532 {
533 bool internal = ctx && ctx->internal;
534 struct uci_section *s;
535
536 UCI_HANDLE_ERR(ctx);
537 UCI_ASSERT(ctx, p != NULL);
538 s = uci_alloc_section(p, type, NULL);
539 uci_fixup_section(ctx, s);
540 *res = s;
541 if (!internal && p->has_delta)
542 uci_add_delta(ctx, &p->delta, UCI_CMD_ADD, s->e.name, NULL, type);
543
544 return 0;
545 }
546
547 int uci_delete(struct uci_context *ctx, struct uci_ptr *ptr)
548 {
549 /* NB: pass on internal flag to uci_del_element */
550 bool internal = ctx && ctx->internal;
551 struct uci_package *p;
552 struct uci_element *e;
553
554 UCI_HANDLE_ERR(ctx);
555
556 e = uci_expand_ptr(ctx, ptr, true);
557 p = ptr->p;
558
559 UCI_ASSERT(ctx, ptr->s);
560
561 if (!internal && p->has_delta)
562 uci_add_delta(ctx, &p->delta, UCI_CMD_REMOVE, ptr->section, ptr->option, NULL);
563
564 uci_free_any(&e);
565
566 if (ptr->option)
567 ptr->o = NULL;
568 else if (ptr->section)
569 ptr->s = NULL;
570
571 return 0;
572 }
573
574 int uci_add_list(struct uci_context *ctx, struct uci_ptr *ptr)
575 {
576 /* NB: UCI_INTERNAL use means without delta tracking */
577 bool internal = ctx && ctx->internal;
578 struct uci_option *prev = NULL;
579 const char *value2 = NULL;
580
581 UCI_HANDLE_ERR(ctx);
582
583 uci_expand_ptr(ctx, ptr, false);
584 UCI_ASSERT(ctx, ptr->s);
585 UCI_ASSERT(ctx, ptr->value);
586
587 if (ptr->o) {
588 switch (ptr->o->type) {
589 case UCI_TYPE_STRING:
590 /* we already have a string value, convert that to a list */
591 prev = ptr->o;
592 value2 = ptr->value;
593 ptr->value = ptr->o->v.string;
594 break;
595 case UCI_TYPE_LIST:
596 uci_add_element_list(ctx, ptr, internal);
597 return 0;
598 default:
599 UCI_THROW(ctx, UCI_ERR_INVAL);
600 break;
601 }
602 }
603
604 ptr->o = uci_alloc_list(ptr->s, ptr->option);
605 if (prev) {
606 uci_add_element_list(ctx, ptr, true);
607 uci_free_option(prev);
608 ptr->value = value2;
609 }
610 uci_add_element_list(ctx, ptr, internal);
611
612 return 0;
613 }
614
615 int uci_set(struct uci_context *ctx, struct uci_ptr *ptr)
616 {
617 /* NB: UCI_INTERNAL use means without delta tracking */
618 bool internal = ctx && ctx->internal;
619
620 UCI_HANDLE_ERR(ctx);
621 uci_expand_ptr(ctx, ptr, false);
622 UCI_ASSERT(ctx, ptr->value);
623 UCI_ASSERT(ctx, ptr->s || (!ptr->option && ptr->section));
624 if (!ptr->option && ptr->value[0]) {
625 UCI_ASSERT(ctx, uci_validate_type(ptr->value));
626 }
627
628 if (!ptr->o && ptr->s && ptr->option) {
629 struct uci_element *e;
630 e = uci_lookup_list(&ptr->s->options, ptr->option);
631 if (e)
632 ptr->o = uci_to_option(e);
633 }
634 if (!ptr->value[0]) {
635 /* if setting a nonexistant option/section to a nonexistant value,
636 * exit without errors */
637 if (!(ptr->flags & UCI_LOOKUP_COMPLETE))
638 return 0;
639
640 return uci_delete(ctx, ptr);
641 } else if (!ptr->o && ptr->option) { /* new option */
642 ptr->o = uci_alloc_option(ptr->s, ptr->option, ptr->value);
643 ptr->last = &ptr->o->e;
644 } else if (!ptr->s && ptr->section) { /* new section */
645 ptr->s = uci_alloc_section(ptr->p, ptr->value, ptr->section);
646 ptr->last = &ptr->s->e;
647 } else if (ptr->o && ptr->option) { /* update option */
648 if ((ptr->o->type == UCI_TYPE_STRING) &&
649 !strcmp(ptr->o->v.string, ptr->value))
650 return 0;
651 uci_free_option(ptr->o);
652 ptr->o = uci_alloc_option(ptr->s, ptr->option, ptr->value);
653 ptr->last = &ptr->o->e;
654 } else if (ptr->s && ptr->section) { /* update section */
655 char *s = uci_strdup(ctx, ptr->value);
656
657 if (ptr->s->type == uci_dataptr(ptr->s)) {
658 ptr->last = NULL;
659 ptr->last = uci_realloc(ctx, ptr->s, sizeof(struct uci_section));
660 ptr->s = uci_to_section(ptr->last);
661 uci_list_fixup(&ptr->s->e.list);
662 } else {
663 free(ptr->s->type);
664 }
665 ptr->s->type = s;
666 } else {
667 UCI_THROW(ctx, UCI_ERR_INVAL);
668 }
669
670 if (!internal && ptr->p->has_delta)
671 uci_add_delta(ctx, &ptr->p->delta, UCI_CMD_CHANGE, ptr->section, ptr->option, ptr->value);
672
673 return 0;
674 }
675
676 int uci_unload(struct uci_context *ctx, struct uci_package *p)
677 {
678 UCI_HANDLE_ERR(ctx);
679 UCI_ASSERT(ctx, p != NULL);
680
681 uci_free_package(&p);
682 return 0;
683 }
684