From: Michael Tremer Date: Thu, 18 Aug 2022 19:43:11 +0000 (+0000) Subject: Remove use of snprintf() throughout the library X-Git-Tag: 0.9.28~442 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=45d1bfad1a26942b561694cd54f3a7f125e7a06f;p=pakfire.git Remove use of snprintf() throughout the library Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/config.c b/src/libpakfire/config.c index ad1095f73..ac98be034 100644 --- a/src/libpakfire/config.c +++ b/src/libpakfire/config.c @@ -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; } diff --git a/src/libpakfire/parser.c b/src/libpakfire/parser.c index ee1509cd1..e0baa96e4 100644 --- a/src/libpakfire/parser.c +++ b/src/libpakfire/parser.c @@ -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 diff --git a/tests/libpakfire/makefile.c b/tests/libpakfire/makefile.c index 20146ecd2..6ea97eb13 100644 --- a/tests/libpakfire/makefile.c +++ b/tests/libpakfire/makefile.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #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"); diff --git a/tests/libpakfire/parser.c b/tests/libpakfire/parser.c index de95b1f34..fa5f65993 100644 --- a/tests/libpakfire/parser.c +++ b/tests/libpakfire/parser.c @@ -23,6 +23,7 @@ #include #include +#include #include #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);