git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/2] cat-file: Fix an gcc -Wuninitialized warning

From
Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Date
Mar 26, 2013, 19:20 UTC
Message-ID
<5151F4EB.80909@ramsay1.demon.co.uk>

After commit cbfd5e1c ("drop some obsolete "x = x" compiler warning hacks", 21-03-2013) removed a gcc specific hack, older versions of gcc now issue an "'contents' might be used uninitialized" warning. In order to suppress the warning, we simply initialize the variable to NULL in it's declaration.

Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
---
An alternative solution may look like this (note: *untested*):
    diff --git a/builtin/cat-file.c b/builtin/cat-file.c
    index ad29000..e50b20f 100644
    --- a/builtin/cat-file.c
    +++ b/builtin/cat-file.c
    @@ -193,7 +193,6 @@ static int batch_one_object(const char *obj_name, int print_contents)
     	unsigned char sha1[20];
     	enum object_type type = 0;
     	unsigned long size;
    -	void *contents;
     
     	if (!obj_name)
     	   return 1;
    @@ -204,16 +203,11 @@ static int batch_one_object(const char *obj_name, int print_contents)
     		return 0;
     	}
     
    -	if (print_contents == BATCH)
    -		contents = read_sha1_file(sha1, &type, &size);
    -	else
    -		type = sha1_object_info(sha1, &size);
    +	type = sha1_object_info(sha1, &size);
     
     	if (type <= 0) {
     		printf("%s missing\n", obj_name);
     		fflush(stdout);
    -		if (print_contents == BATCH)
    -			free(contents);
     		return 0;
     	}
     
    @@ -221,6 +215,7 @@ static int batch_one_object(const char *obj_name, int print_contents)
     	fflush(stdout);
     
     	if (print_contents == BATCH) {
    +		void *contents = read_sha1_file(sha1, &type, &size);
     		write_or_die(1, contents, size);
     		printf("\n");
     		fflush(stdout);
    -- 

However, this would add an additional call to sha1_object_info() to the "--batch" code path, with potential performance consequences (again untested). Also, if you are paranoid, I guess you should check that the (type,size) returned by sha1_object_info() was the same as that returned by read_sha1_file(). ;-)

ATB, Ramsay Jones

 builtin/cat-file.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index ad29000..40f87b4 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -193,7 +193,7 @@ static int batch_one_object(const char *obj_name, int print_contents)
 	unsigned char sha1[20];
 	enum object_type type = 0;
 	unsigned long size;
-	void *contents;
+	void *contents = NULL;
 
 	if (!obj_name)
 	   return 1;
-- 
1.8.2
Next: Jeff King
Message 1 of 6 in “cat-file: Fix an gcc -Wuninitialized warning”
  1. 2/2 cat-file: Fix an gcc -Wuninitialized warningRamsay Jones, Mar 26, 2013
  2. Jeff KingMar 26, 2013
  3. Jeff KingMar 26, 2013
  4. Ramsay JonesMar 28, 2013
  5. Jeff KingMar 28, 2013
  6. Jonathan NiederMar 28, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.