]> git.ipfire.org Git - people/stevee/pakfire.git/commitdiff
Remove use of snprintf() throughout the library
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 18 Aug 2022 19:43:11 +0000 (19:43 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 18 Aug 2022 19:43:11 +0000 (19:43 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/config.c
src/libpakfire/parser.c
tests/libpakfire/makefile.c
tests/libpakfire/parser.c

index ad1095f73924f0b182231d4c08577eb6efb88018..ac98be03487f2aea46d70dbd870fce9d61a6eaa2 100644 (file)
@@ -326,9 +326,9 @@ int pakfire_config_read(struct pakfire_config* config, FILE* f) {
                if (line[0] == '[' && line[length - 1] == ']') {
                        line[length - 1] = '\0';
 
-                       int r = snprintf(section, sizeof(section) - 1, "%s", line + 1);
-                       if (r == sizeof(section))
-                               return ENOBUFS;
+                       int r = pakfire_string_set(section, line + 1);
+                       if (r)
+                               return r;
 
                        continue;
                }
index ee1509cd1cc18ac5a945656b0a0250806988eca3..e0baa96e4f7a8619bf6bac9d1d37cbc8b08a74dc 100644 (file)
@@ -338,8 +338,8 @@ int pakfire_parser_apply_declaration(struct pakfire_parser* parser,
                declaration->name, declaration->value, declaration->flags);
 }
 
-static const char* pakfire_parser_find_template(struct pakfire_parser* parser,
-               char* template, size_t size, const char* namespace) {
+static int pakfire_parser_find_template(struct pakfire_parser* parser,
+               char* template, const size_t length, const char* namespace) {
        DEBUG(parser->pakfire, "Looking up template in namespace '%s'\n", namespace);
 
        struct pakfire_parser_declaration* d = pakfire_parser_get_declaration(
@@ -347,13 +347,14 @@ static const char* pakfire_parser_find_template(struct pakfire_parser* parser,
 
        const char* value = (d && *d->value) ? d->value : "MAIN";
 
-       snprintf(template, size, "packages.template:%s", value);
-
-       return template;
+       // Format full variable name
+       return __pakfire_string_format(template, length, "packages.template:%s", value);
 }
 
 static const char* pakfire_parser_get_raw(struct pakfire_parser* parser, const char* namespace, const char* name) {
        struct pakfire_parser_declaration* d = NULL;
+       char template[NAME_MAX];
+       int r;
 
        // First, perform a simple lookup
        d = pakfire_parser_get_declaration_recursive(parser, namespace, name);
@@ -362,18 +363,16 @@ static const char* pakfire_parser_get_raw(struct pakfire_parser* parser, const c
        if (d && d->value)
                return d->value;
 
-       char template[NAME_MAX] = "";
-
        // If we couldn't find anything, we check if there is a template, and if that
        // has our value...
        if (namespace && pakfire_string_startswith(namespace, "packages.package:")) {
-               pakfire_parser_find_template(parser, template, sizeof(template) - 1, namespace);
+               r = pakfire_parser_find_template(parser, template, sizeof(template), namespace);
+               if (r)
+                       return NULL;
 
-               if (*template) {
-                       d = pakfire_parser_find_declaration(parser, template, name);
-                       if (d && d->value)
-                               return d->value;
-               }
+               d = pakfire_parser_find_declaration(parser, template, name);
+               if (d && d->value)
+                       return d->value;
        }
 
        // Otherwise we walk up the namespace to find a match
index 20146ecd2ac1a2613f72dd36ad5d810418526697..6ea97eb13ad769227481a79c0e7db48292438ea1 100644 (file)
@@ -27,6 +27,7 @@
 #include <pakfire/package.h>
 #include <pakfire/parser.h>
 #include <pakfire/repo.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
 #include "../testsuite.h"
@@ -63,8 +64,7 @@ static int test_parse(const struct test* t) {
        int r = EXIT_FAILURE;
 
        while (*makefile) {
-               r = snprintf(path, sizeof(path) - 1, "%s/%s", TEST_SRC_PATH, *makefile);
-               ASSERT(r >= 0);
+               ASSERT_SUCCESS(pakfire_string_format(path, "%s/%s", TEST_SRC_PATH, *makefile));
 
                // Open file
                FILE* f = fopen(path, "r");
index de95b1f34c5983a98eead3515579a251cd809c4d..fa5f6599372d352d0f200c56c9588c3eb8327214 100644 (file)
@@ -23,6 +23,7 @@
 #include <string.h>
 
 #include <pakfire/parser.h>
+#include <pakfire/string.h>
 #include <pakfire/util.h>
 
 #include "../testsuite.h"
@@ -120,8 +121,7 @@ static int test_parser_files(const struct test* t) {
        int r = EXIT_FAILURE;
 
        while (*file) {
-               r = snprintf(path, sizeof(path) - 1, "%s/%s", TEST_SRC_PATH, *file);
-               ASSERT(r >= 0);
+               ASSERT_SUCCESS(pakfire_string_format(path, "%s/%s", TEST_SRC_PATH, *file));
 
                // Create a new parser
                struct pakfire_parser* parser = pakfire_parser_create(t->pakfire, NULL, NULL, 0);