]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
tar: Always use correct length in safe_fprintf
authorTobias Stoeckmann <tobias@stoeckmann.org>
Fri, 16 May 2025 21:08:59 +0000 (23:08 +0200)
committerTobias Stoeckmann <tobias@stoeckmann.org>
Fri, 16 May 2025 22:22:15 +0000 (00:22 +0200)
If the format buffer shall not be further increased in size, the
length value mistakenly takes the terminating nul byte into account.

This is in contrast to a successful vsnprintf call.

Also use the correct string length if fallback to stack buffer is
required.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
tar/util.c

index dabb94058e801dbd512fd11214fa3c33361db98f..dc0ab419e3f2b89a9cd09d43f03cc5e0cc4382f9 100644 (file)
@@ -106,8 +106,8 @@ safe_fprintf(FILE * restrict f, const char * restrict fmt, ...)
                else if (fmtbuff_length < 1000000)
                        fmtbuff_length += fmtbuff_length / 4;
                else {
-                       length = fmtbuff_length;
-                       fmtbuff_heap[length-1] = '\0';
+                       fmtbuff[fmtbuff_length - 1] = '\0';
+                       length = (int)strlen(fmtbuff);
                        break;
                }
                free(fmtbuff_heap);
@@ -122,8 +122,9 @@ safe_fprintf(FILE * restrict f, const char * restrict fmt, ...)
                } else {
                        /* Leave fmtbuff pointing to the truncated
                         * string in fmtbuff_stack. */
+                       fmtbuff_stack[sizeof(fmtbuff_stack) - 1] = '\0';
                        fmtbuff = fmtbuff_stack;
-                       length = sizeof(fmtbuff_stack) - 1;
+                       length = (int)strlen(fmtbuff);
                        break;
                }
        }