{"thread":{"id":"107","subject":"[PATCH 1/6] show-diff.c: clean up private buffer use.","startedAt":"2005-04-18T20:33:15Z","lastAt":"2005-04-18T20:33:15Z","messageCount":1,"participants":["Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":6},"messages":[{"id":"686","messageId":"7vvf6ju8ok.fsf@assigned-by-dhcp.cox.net","threadId":"107","inReplyTo":null,"subject":"[PATCH 1/6] show-diff.c: clean up private buffer use.","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2005-04-18T20:33:15Z","receivedAt":"2005-04-18T20:33:15Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"[PATCH 1/6] show-diff.c: clean up private buffer use.\n\nThis patch fixes sq_expand() and show_differences() not to use and\nhold onto its privately allocated buffer, which was a misguided\nattempt to reduce calls to malloc but made later changes harder.\n\nSigned-off-by: Junio C Hamano <junkio@cox.net>\n---\n\n show-diff.c |   22 ++++++----------------\n 1 files changed, 6 insertions(+), 16 deletions(-)\n\nshow-diff.c: 1342b3c9848a7675665594a6bc19d95e3b7ff358\n--- a/show-diff.c\n+++ b/show-diff.c\n@@ -23,7 +23,6 @@ static char *diff_cmd = \"diff -L '%s' -u\n static char *sq_expand(char *src)\n {\n \tstatic char *buf = NULL;\n-\tstatic int buf_size = -1;\n \tint cnt, c;\n \tchar *cp;\n \n@@ -32,12 +31,8 @@ static char *sq_expand(char *src)\n \t\tif (*cp == '\\'')\n \t\t\tcnt += 3;\n \n-\tif (buf_size < cnt) {\n-\t\tfree(buf);\n-\t\tbuf_size = cnt;\n-\t\tbuf = malloc(cnt);\n-\t}\n-\n+\tif (! (buf = malloc(cnt)))\n+\t    return buf;\n \tcp = buf;\n \twhile ((c = *src++)) {\n \t\tif (c != '\\'')\n@@ -55,22 +50,17 @@ static void show_differences(char *name,\n \t\t\t     unsigned long long old_size)\n {\n \tFILE *f;\n-\tstatic char *cmd = NULL;\n-\tstatic int cmd_size = -1;\n-\n \tchar *name_sq = sq_expand(name);\n-\tint cmd_required_length = strlen(name_sq) * 2 + strlen(diff_cmd);\n+\tint cmd_size = strlen(name_sq) * 2 + strlen(diff_cmd);\n+\tchar *cmd = malloc(cmd_size);\n \n-\tif (cmd_size < cmd_required_length) {\n-\t\tfree(cmd);\n-\t\tcmd_size = cmd_required_length;\n-\t\tcmd = malloc(cmd_required_length);\n-\t}\n \tsnprintf(cmd, cmd_size, diff_cmd, name_sq, name_sq);\n \tf = popen(cmd, \"w\");\n \tif (old_size)\n \t\tfwrite(old_contents, old_size, 1, f);\n \tpclose(f);\n+\tfree(name_sq);\n+\tfree(cmd);\n }\n \n static void show_diff_empty(struct cache_entry *ce)\n\n"}]}