net: add rte prefix to ether structures
[dpdk.git] / lib / librte_cfgfile / rte_cfgfile.c
index 124aef5..f8e7627 100644 (file)
@@ -1,34 +1,5 @@
-/*-
- *   BSD LICENSE
- *
- *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
- *   All rights reserved.
- *
- *   Redistribution and use in source and binary forms, with or without
- *   modification, are permitted provided that the following conditions
- *   are met:
- *
- *     * Redistributions of source code must retain the above copyright
- *       notice, this list of conditions and the following disclaimer.
- *     * Redistributions in binary form must reproduce the above copyright
- *       notice, this list of conditions and the following disclaimer in
- *       the documentation and/or other materials provided with the
- *       distribution.
- *     * Neither the name of Intel Corporation nor the names of its
- *       contributors may be used to endorse or promote products derived
- *       from this software without specific prior written permission.
- *
- *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2010-2014 Intel Corporation
  */
 
 #include <stdio.h>
@@ -36,6 +7,7 @@
 #include <string.h>
 #include <ctype.h>
 #include <errno.h>
+#include <rte_string_fns.h>
 #include <rte_common.h>
 
 #include "rte_cfgfile.h"
@@ -142,9 +114,8 @@ _add_entry(struct rte_cfgfile_section *section, const char *entryname,
        struct rte_cfgfile_entry *curr_entry =
                                        &section->entries[section->num_entries];
 
-       snprintf(curr_entry->name, sizeof(curr_entry->name), "%s", entryname);
-       snprintf(curr_entry->value,
-                               sizeof(curr_entry->value), "%s", entryvalue);
+       strlcpy(curr_entry->name, entryname, sizeof(curr_entry->name));
+       strlcpy(curr_entry->value, entryvalue, sizeof(curr_entry->value));
        section->num_entries++;
 
        return 0;
@@ -241,6 +212,11 @@ rte_cfgfile_load_with_params(const char *filename, int flags,
 
                        split[0] = buffer;
                        split[1] = memchr(buffer, '=', len);
+                       if (split[1] == NULL) {
+                               printf("Error line %d - no '='"
+                                       "character found\n", lineno);
+                               goto error1;
+                       }
                        *split[1] = '\0';
                        split[1]++;
 
@@ -248,10 +224,11 @@ rte_cfgfile_load_with_params(const char *filename, int flags,
                        _strip(split[1], strlen(split[1]));
                        char *end = memchr(split[1], '\\', strlen(split[1]));
 
+                       size_t split_len = strlen(split[1]) + 1;
                        while (end != NULL) {
                                if (*(end+1) == params->comment_character) {
                                        *end = '\0';
-                                       strcat(split[1], end+1);
+                                       strlcat(split[1], end+1, split_len);
                                } else
                                        end++;
                                end = memchr(end, '\\', strlen(end));
@@ -268,7 +245,7 @@ rte_cfgfile_load_with_params(const char *filename, int flags,
                                goto error1;
 
                        _add_entry(&cfg->sections[cfg->num_sections - 1],
-                                       split[0], (split[1] ? split[1] : ""));
+                                       split[0], split[1]);
                }
        }
        fclose(f);
@@ -298,7 +275,7 @@ rte_cfgfile_create(int flags)
                        CFG_ALLOC_SECTION_BATCH);
 
        if (cfg->sections == NULL)
-               return NULL;
+               goto error1;
 
        cfg->allocated_sections = CFG_ALLOC_SECTION_BATCH;
 
@@ -307,7 +284,7 @@ rte_cfgfile_create(int flags)
                        struct rte_cfgfile_entry) * CFG_ALLOC_ENTRY_BATCH);
 
                if (cfg->sections[i].entries == NULL)
-                       return NULL;
+                       goto error1;
 
                cfg->sections[i].num_entries = 0;
                cfg->sections[i].allocated_entries = CFG_ALLOC_ENTRY_BATCH;
@@ -315,7 +292,21 @@ rte_cfgfile_create(int flags)
 
        if (flags & CFG_FLAG_GLOBAL_SECTION)
                rte_cfgfile_add_section(cfg, "GLOBAL");
+
        return cfg;
+error1:
+       if (cfg->sections != NULL) {
+               for (i = 0; i < cfg->allocated_sections; i++) {
+                       if (cfg->sections[i].entries != NULL) {
+                               free(cfg->sections[i].entries);
+                               cfg->sections[i].entries = NULL;
+                       }
+               }
+               free(cfg->sections);
+               cfg->sections = NULL;
+       }
+       free(cfg);
+       return NULL;
 }
 
 int
@@ -351,8 +342,8 @@ rte_cfgfile_add_section(struct rte_cfgfile *cfg, const char *sectionname)
                cfg->allocated_sections += CFG_ALLOC_SECTION_BATCH;
        }
 
-       snprintf(cfg->sections[cfg->num_sections].name,
-                       sizeof(cfg->sections[0].name), "%s", sectionname);
+       strlcpy(cfg->sections[cfg->num_sections].name, sectionname,
+               sizeof(cfg->sections[0].name));
        cfg->sections[cfg->num_sections].num_entries = 0;
        cfg->num_sections++;
 
@@ -402,9 +393,8 @@ int rte_cfgfile_set_entry(struct rte_cfgfile *cfg, const char *sectionname,
 
        for (i = 0; i < curr_section->num_entries; i++)
                if (!strcmp(curr_section->entries[i].name, entryname)) {
-                       snprintf(curr_section->entries[i].value,
-                                       sizeof(curr_section->entries[i].value),
-                                                       "%s", entryvalue);
+                       strlcpy(curr_section->entries[i].value, entryvalue,
+                               sizeof(curr_section->entries[i].value));
                        return 0;
                }
        printf("Error - entry name doesn't exist\n");
@@ -478,8 +468,7 @@ rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[],
        int i;
 
        for (i = 0; i < cfg->num_sections && i < max_sections; i++)
-               snprintf(sections[i], CFG_NAME_LEN, "%s",
-               cfg->sections[i].name);
+               strlcpy(sections[i], cfg->sections[i].name, CFG_NAME_LEN);
 
        return i;
 }
@@ -509,7 +498,7 @@ rte_cfgfile_section_num_entries_by_index(struct rte_cfgfile *cfg,
 
        const struct rte_cfgfile_section *sect = &(cfg->sections[index]);
 
-       snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name);
+       strlcpy(sectionname, sect->name, CFG_NAME_LEN);
        return sect->num_entries;
 }
 int
@@ -536,7 +525,7 @@ rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg, int index,
        if (index < 0 || index >= cfg->num_sections)
                return -1;
        sect = &cfg->sections[index];
-       snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name);
+       strlcpy(sectionname, sect->name, CFG_NAME_LEN);
        for (i = 0; i < max_entries && i < sect->num_entries; i++)
                entries[i] = sect->entries[i];
        return i;