]> git.ipfire.org Git - pakfire.git/commitdiff
transaction: Copy input string to stack
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 25 Jun 2021 15:46:21 +0000 (15:46 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 25 Jun 2021 15:46:21 +0000 (15:46 +0000)
asprintf suddently does not seem to be happy when the input and output
strings overlap

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/transaction.c

index 83fe079c173493ee4e2e3ed0f235afcd6ecdbc42..fa54e603ec704837ad238b5183c8ffc89981cfbd 100644 (file)
@@ -261,17 +261,18 @@ PAKFIRE_EXPORT ssize_t pakfire_transaction_downloadsize(struct pakfire_transacti
 }
 
 static void pakfire_transaction_add_headline(char** str, size_t width, const char* headline) {
-       asprintf(str, "%s%s\n", *str, headline);
+       asprintf(str, "%s%s\n", strdup(*str), headline);
 }
 
 static void pakfire_transaction_add_newline(char** str, size_t width) {
-       asprintf(str, "%s\n", *str);
+       asprintf(str, "%s\n", strdupa(*str));
 }
 
 static void pakfire_transaction_add_line(char** str, size_t width, const char* name,
                const char* arch, const char* version, const char* repo, const char* size) {
        // XXX need to adapt to size
-       asprintf(str, "%s %-21s %-8s %-21s %-18s %6s \n", *str, name, arch, version, repo, size);
+       asprintf(str, "%s %-21s %-8s %-21s %-18s %6s \n", strdupa(*str),
+               name, arch, version, repo, size);
 }
 
 static void pakfire_transaction_add_package(char** str, size_t width, PakfirePackage pkg) {
@@ -298,27 +299,31 @@ static void pakfire_transaction_add_package_change(char** str, size_t width,
        // Print the new package first
        pakfire_transaction_add_package(str, width, new_pkg);
 
-       asprintf(str, "%s   --> %s\n", *str, pakfire_package_get_nevra(old_pkg));
+       asprintf(str, "%s   --> %s\n", strdupa(*str), pakfire_package_get_nevra(old_pkg));
 }
 
 static void pakfire_transaction_add_separator(char** str, size_t width) {
-       while (width-- > 0)
-               asprintf(str, "%s=", *str);
+       char* separator = alloca(width + 1);
+
+       // Make separator of appropriate length
+       for (unsigned int i = 0; i < width; i++)
+               separator[i] = '=';
+       separator[width] = '\0';
 
        // newline
-       asprintf(str, "%s\n", *str);
+       asprintf(str, "%s%s\n", strdupa(*str), separator);
 }
 
 static void pakfire_transaction_add_usage_line(char** str, size_t width, const char* headline, ssize_t size) {
        char buffer[128];
        pakfire_format_size(buffer, sizeof(buffer) - 1, size);
 
-       asprintf(str, "%s%-21s: %s\n", *str, headline, buffer);
+       asprintf(str, "%s%-21s: %s\n", strdupa(*str), headline, buffer);
 }
 
 PAKFIRE_EXPORT char* pakfire_transaction_dump(struct pakfire_transaction* transaction, size_t width) {
        char headline[1024];
-       char* string = "";
+       char* string = strdup("");
 
        Pool* pool = transaction->transaction->pool;
        const int mode =