cfgfile: fix null pointer dereference in parsing
[dpdk.git] / lib / librte_cfgfile / rte_cfgfile.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <errno.h>
39 #include <rte_common.h>
40
41 #include "rte_cfgfile.h"
42
43 struct rte_cfgfile_section {
44         char name[CFG_NAME_LEN];
45         int num_entries;
46         int allocated_entries;
47         struct rte_cfgfile_entry *entries;
48 };
49
50 struct rte_cfgfile {
51         int flags;
52         int num_sections;
53         int allocated_sections;
54         struct rte_cfgfile_section *sections;
55 };
56
57 /** when we resize a file structure, how many extra entries
58  * for new sections do we add in */
59 #define CFG_ALLOC_SECTION_BATCH 8
60 /** when we resize a section structure, how many extra entries
61  * for new entries do we add in */
62 #define CFG_ALLOC_ENTRY_BATCH 16
63
64 /**
65  * Default cfgfile load parameters.
66  */
67 static const struct rte_cfgfile_parameters default_cfgfile_params = {
68         .comment_character = CFG_DEFAULT_COMMENT_CHARACTER,
69 };
70
71 /**
72  * Defines the list of acceptable comment characters supported by this
73  * library.
74  */
75 static const char valid_comment_chars[] = {
76         '!',
77         '#',
78         '%',
79         ';',
80         '@'
81 };
82
83 static unsigned
84 _strip(char *str, unsigned len)
85 {
86         int newlen = len;
87         if (len == 0)
88                 return 0;
89
90         if (isspace(str[len-1])) {
91                 /* strip trailing whitespace */
92                 while (newlen > 0 && isspace(str[newlen - 1]))
93                         str[--newlen] = '\0';
94         }
95
96         if (isspace(str[0])) {
97                 /* strip leading whitespace */
98                 int i, start = 1;
99                 while (isspace(str[start]) && start < newlen)
100                         start++
101                         ; /* do nothing */
102                 newlen -= start;
103                 for (i = 0; i < newlen; i++)
104                         str[i] = str[i+start];
105                 str[i] = '\0';
106         }
107         return newlen;
108 }
109
110 static struct rte_cfgfile_section *
111 _get_section(struct rte_cfgfile *cfg, const char *sectionname)
112 {
113         int i;
114
115         for (i = 0; i < cfg->num_sections; i++) {
116                 if (strncmp(cfg->sections[i].name, sectionname,
117                                 sizeof(cfg->sections[0].name)) == 0)
118                         return &cfg->sections[i];
119         }
120         return NULL;
121 }
122
123 static int
124 _add_entry(struct rte_cfgfile_section *section, const char *entryname,
125                 const char *entryvalue)
126 {
127         /* resize entry structure if we don't have room for more entries */
128         if (section->num_entries == section->allocated_entries) {
129                 struct rte_cfgfile_entry *n_entries = realloc(
130                                 section->entries,
131                                 sizeof(struct rte_cfgfile_entry) *
132                                 ((section->allocated_entries) +
133                                                 CFG_ALLOC_ENTRY_BATCH));
134
135                 if (n_entries == NULL)
136                         return -ENOMEM;
137
138                 section->entries = n_entries;
139                 section->allocated_entries += CFG_ALLOC_ENTRY_BATCH;
140         }
141         /* fill up entry fields with key name and value */
142         struct rte_cfgfile_entry *curr_entry =
143                                         &section->entries[section->num_entries];
144
145         snprintf(curr_entry->name, sizeof(curr_entry->name), "%s", entryname);
146         snprintf(curr_entry->value,
147                                 sizeof(curr_entry->value), "%s", entryvalue);
148         section->num_entries++;
149
150         return 0;
151 }
152
153 static int
154 rte_cfgfile_check_params(const struct rte_cfgfile_parameters *params)
155 {
156         unsigned int valid_comment;
157         unsigned int i;
158
159         if (!params) {
160                 printf("Error - missing cfgfile parameters\n");
161                 return -EINVAL;
162         }
163
164         valid_comment = 0;
165         for (i = 0; i < RTE_DIM(valid_comment_chars); i++) {
166                 if (params->comment_character == valid_comment_chars[i]) {
167                         valid_comment = 1;
168                         break;
169                 }
170         }
171
172         if (valid_comment == 0) {
173                 printf("Error - invalid comment characters %c\n",
174                        params->comment_character);
175                 return -ENOTSUP;
176         }
177
178         return 0;
179 }
180
181 struct rte_cfgfile *
182 rte_cfgfile_load(const char *filename, int flags)
183 {
184         return rte_cfgfile_load_with_params(filename, flags,
185                                             &default_cfgfile_params);
186 }
187
188 struct rte_cfgfile *
189 rte_cfgfile_load_with_params(const char *filename, int flags,
190                              const struct rte_cfgfile_parameters *params)
191 {
192         char buffer[CFG_NAME_LEN + CFG_VALUE_LEN + 4] = {0};
193         int lineno = 0;
194         struct rte_cfgfile *cfg = NULL;
195
196         if (rte_cfgfile_check_params(params))
197                 return NULL;
198
199         FILE *f = fopen(filename, "r");
200         if (f == NULL)
201                 return NULL;
202
203         cfg = rte_cfgfile_create(flags);
204
205         while (fgets(buffer, sizeof(buffer), f) != NULL) {
206                 char *pos = NULL;
207                 size_t len = strnlen(buffer, sizeof(buffer));
208                 lineno++;
209                 if ((len >= sizeof(buffer) - 1) && (buffer[len-1] != '\n')) {
210                         printf("Error line %d - no \\n found on string. "
211                                         "Check if line too long\n", lineno);
212                         goto error1;
213                 }
214                 /* skip parsing if comment character found */
215                 pos = memchr(buffer, params->comment_character, len);
216                 if (pos != NULL && (*(pos-1) != '\\')) {
217                         *pos = '\0';
218                         len = pos -  buffer;
219                 }
220
221                 len = _strip(buffer, len);
222                 /* skip lines without useful content */
223                 if (buffer[0] != '[' && memchr(buffer, '=', len) == NULL)
224                         continue;
225
226                 if (buffer[0] == '[') {
227                         /* section heading line */
228                         char *end = memchr(buffer, ']', len);
229                         if (end == NULL) {
230                                 printf("Error line %d - no terminating ']'"
231                                         "character found\n", lineno);
232                                 goto error1;
233                         }
234                         *end = '\0';
235                         _strip(&buffer[1], end - &buffer[1]);
236
237                         rte_cfgfile_add_section(cfg, &buffer[1]);
238                 } else {
239                         /* key and value line */
240                         char *split[2] = {NULL};
241
242                         split[0] = buffer;
243                         split[1] = memchr(buffer, '=', len);
244                         if (split[1] == NULL) {
245                                 printf("Error line %d - no '='"
246                                         "character found\n", lineno);
247                                 goto error1;
248                         }
249                         *split[1] = '\0';
250                         split[1]++;
251
252                         _strip(split[0], strlen(split[0]));
253                         _strip(split[1], strlen(split[1]));
254                         char *end = memchr(split[1], '\\', strlen(split[1]));
255
256                         while (end != NULL) {
257                                 if (*(end+1) == params->comment_character) {
258                                         *end = '\0';
259                                         strcat(split[1], end+1);
260                                 } else
261                                         end++;
262                                 end = memchr(end, '\\', strlen(end));
263                         }
264
265                         if (!(flags & CFG_FLAG_EMPTY_VALUES) &&
266                                         (*split[1] == '\0')) {
267                                 printf("Error at line %d - cannot use empty "
268                                                         "values\n", lineno);
269                                 goto error1;
270                         }
271
272                         if (cfg->num_sections == 0)
273                                 goto error1;
274
275                         _add_entry(&cfg->sections[cfg->num_sections - 1],
276                                         split[0], split[1]);
277                 }
278         }
279         fclose(f);
280         return cfg;
281 error1:
282         rte_cfgfile_close(cfg);
283         fclose(f);
284         return NULL;
285 }
286
287 struct rte_cfgfile *
288 rte_cfgfile_create(int flags)
289 {
290         int i;
291         struct rte_cfgfile *cfg = NULL;
292
293         cfg = malloc(sizeof(*cfg));
294
295         if (cfg == NULL)
296                 return NULL;
297
298         cfg->flags = flags;
299         cfg->num_sections = 0;
300
301         /* allocate first batch of sections and entries */
302         cfg->sections = malloc(sizeof(struct rte_cfgfile_section) *
303                         CFG_ALLOC_SECTION_BATCH);
304
305         if (cfg->sections == NULL)
306                 return NULL;
307
308         cfg->allocated_sections = CFG_ALLOC_SECTION_BATCH;
309
310         for (i = 0; i < CFG_ALLOC_SECTION_BATCH; i++) {
311                 cfg->sections[i].entries = malloc(sizeof(
312                         struct rte_cfgfile_entry) * CFG_ALLOC_ENTRY_BATCH);
313
314                 if (cfg->sections[i].entries == NULL)
315                         return NULL;
316
317                 cfg->sections[i].num_entries = 0;
318                 cfg->sections[i].allocated_entries = CFG_ALLOC_ENTRY_BATCH;
319         }
320
321         if (flags & CFG_FLAG_GLOBAL_SECTION)
322                 rte_cfgfile_add_section(cfg, "GLOBAL");
323         return cfg;
324 }
325
326 int
327 rte_cfgfile_add_section(struct rte_cfgfile *cfg, const char *sectionname)
328 {
329         int i;
330
331         if (cfg == NULL)
332                 return -EINVAL;
333
334         if (sectionname == NULL)
335                 return -EINVAL;
336
337         /* resize overall struct if we don't have room for more sections */
338         if (cfg->num_sections == cfg->allocated_sections) {
339
340                 struct rte_cfgfile_section *n_sections =
341                                 realloc(cfg->sections,
342                                 sizeof(struct rte_cfgfile_section) *
343                                 ((cfg->allocated_sections) +
344                                 CFG_ALLOC_SECTION_BATCH));
345
346                 if (n_sections == NULL)
347                         return -ENOMEM;
348
349                 for (i = 0; i < CFG_ALLOC_SECTION_BATCH; i++) {
350                         n_sections[i + cfg->allocated_sections].num_entries = 0;
351                         n_sections[i +
352                                  cfg->allocated_sections].allocated_entries = 0;
353                         n_sections[i + cfg->allocated_sections].entries = NULL;
354                 }
355                 cfg->sections = n_sections;
356                 cfg->allocated_sections += CFG_ALLOC_SECTION_BATCH;
357         }
358
359         snprintf(cfg->sections[cfg->num_sections].name,
360                         sizeof(cfg->sections[0].name), "%s", sectionname);
361         cfg->sections[cfg->num_sections].num_entries = 0;
362         cfg->num_sections++;
363
364         return 0;
365 }
366
367 int rte_cfgfile_add_entry(struct rte_cfgfile *cfg,
368                 const char *sectionname, const char *entryname,
369                 const char *entryvalue)
370 {
371         int ret;
372
373         if ((cfg == NULL) || (sectionname == NULL) || (entryname == NULL)
374                         || (entryvalue == NULL))
375                 return -EINVAL;
376
377         if (rte_cfgfile_has_entry(cfg, sectionname, entryname) != 0)
378                 return -EEXIST;
379
380         /* search for section pointer by sectionname */
381         struct rte_cfgfile_section *curr_section = _get_section(cfg,
382                                                                 sectionname);
383         if (curr_section == NULL)
384                 return -EINVAL;
385
386         ret = _add_entry(curr_section, entryname, entryvalue);
387
388         return ret;
389 }
390
391 int rte_cfgfile_set_entry(struct rte_cfgfile *cfg, const char *sectionname,
392                 const char *entryname, const char *entryvalue)
393 {
394         int i;
395
396         if ((cfg == NULL) || (sectionname == NULL) || (entryname == NULL))
397                 return -EINVAL;
398
399         /* search for section pointer by sectionname */
400         struct rte_cfgfile_section *curr_section = _get_section(cfg,
401                                                                 sectionname);
402         if (curr_section == NULL)
403                 return -EINVAL;
404
405         if (entryvalue == NULL)
406                 entryvalue = "";
407
408         for (i = 0; i < curr_section->num_entries; i++)
409                 if (!strcmp(curr_section->entries[i].name, entryname)) {
410                         snprintf(curr_section->entries[i].value,
411                                         sizeof(curr_section->entries[i].value),
412                                                         "%s", entryvalue);
413                         return 0;
414                 }
415         printf("Error - entry name doesn't exist\n");
416         return -EINVAL;
417 }
418
419 int rte_cfgfile_save(struct rte_cfgfile *cfg, const char *filename)
420 {
421         int i, j;
422
423         if ((cfg == NULL) || (filename == NULL))
424                 return -EINVAL;
425
426         FILE *f = fopen(filename, "w");
427
428         if (f == NULL)
429                 return -EINVAL;
430
431         for (i = 0; i < cfg->num_sections; i++) {
432                 fprintf(f, "[%s]\n", cfg->sections[i].name);
433
434                 for (j = 0; j < cfg->sections[i].num_entries; j++) {
435                         fprintf(f, "%s=%s\n",
436                                         cfg->sections[i].entries[j].name,
437                                         cfg->sections[i].entries[j].value);
438                 }
439         }
440         return fclose(f);
441 }
442
443 int rte_cfgfile_close(struct rte_cfgfile *cfg)
444 {
445         int i;
446
447         if (cfg == NULL)
448                 return -1;
449
450         if (cfg->sections != NULL) {
451                 for (i = 0; i < cfg->allocated_sections; i++) {
452                         if (cfg->sections[i].entries != NULL) {
453                                 free(cfg->sections[i].entries);
454                                 cfg->sections[i].entries = NULL;
455                         }
456                 }
457                 free(cfg->sections);
458                 cfg->sections = NULL;
459         }
460         free(cfg);
461         cfg = NULL;
462
463         return 0;
464 }
465
466 int
467 rte_cfgfile_num_sections(struct rte_cfgfile *cfg, const char *sectionname,
468 size_t length)
469 {
470         int i;
471         int num_sections = 0;
472         for (i = 0; i < cfg->num_sections; i++) {
473                 if (strncmp(cfg->sections[i].name, sectionname, length) == 0)
474                         num_sections++;
475         }
476         return num_sections;
477 }
478
479 int
480 rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[],
481         int max_sections)
482 {
483         int i;
484
485         for (i = 0; i < cfg->num_sections && i < max_sections; i++)
486                 snprintf(sections[i], CFG_NAME_LEN, "%s",
487                 cfg->sections[i].name);
488
489         return i;
490 }
491
492 int
493 rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname)
494 {
495         return _get_section(cfg, sectionname) != NULL;
496 }
497
498 int
499 rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg,
500         const char *sectionname)
501 {
502         const struct rte_cfgfile_section *s = _get_section(cfg, sectionname);
503         if (s == NULL)
504                 return -1;
505         return s->num_entries;
506 }
507
508 int
509 rte_cfgfile_section_num_entries_by_index(struct rte_cfgfile *cfg,
510         char *sectionname, int index)
511 {
512         if (index < 0 || index >= cfg->num_sections)
513                 return -1;
514
515         const struct rte_cfgfile_section *sect = &(cfg->sections[index]);
516
517         snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name);
518         return sect->num_entries;
519 }
520 int
521 rte_cfgfile_section_entries(struct rte_cfgfile *cfg, const char *sectionname,
522                 struct rte_cfgfile_entry *entries, int max_entries)
523 {
524         int i;
525         const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
526         if (sect == NULL)
527                 return -1;
528         for (i = 0; i < max_entries && i < sect->num_entries; i++)
529                 entries[i] = sect->entries[i];
530         return i;
531 }
532
533 int
534 rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg, int index,
535                 char *sectionname,
536                 struct rte_cfgfile_entry *entries, int max_entries)
537 {
538         int i;
539         const struct rte_cfgfile_section *sect;
540
541         if (index < 0 || index >= cfg->num_sections)
542                 return -1;
543         sect = &cfg->sections[index];
544         snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name);
545         for (i = 0; i < max_entries && i < sect->num_entries; i++)
546                 entries[i] = sect->entries[i];
547         return i;
548 }
549
550 const char *
551 rte_cfgfile_get_entry(struct rte_cfgfile *cfg, const char *sectionname,
552                 const char *entryname)
553 {
554         int i;
555         const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
556         if (sect == NULL)
557                 return NULL;
558         for (i = 0; i < sect->num_entries; i++)
559                 if (strncmp(sect->entries[i].name, entryname, CFG_NAME_LEN)
560                                                                         == 0)
561                         return sect->entries[i].value;
562         return NULL;
563 }
564
565 int
566 rte_cfgfile_has_entry(struct rte_cfgfile *cfg, const char *sectionname,
567                 const char *entryname)
568 {
569         return rte_cfgfile_get_entry(cfg, sectionname, entryname) != NULL;
570 }