Thread (10 messages) 10 messages, 4 authors, 2025-07-17
STALE365d

[PATCH 1/4] cat_one_file(): make it easy to see that the `size` variable is initialized

From: Johannes Schindelin via GitGitGadget <hidden>
Date: 2025-03-27 12:43:52
Subsystem: the rest · Maintainer: Linus Torvalds

From: Johannes Schindelin <redacted>

The large `switch` statement makes it a bit impractical to reason about
the code.

One of the code paths can technically lead to using `size` without being
initialized: if the `t` case is taken and the type name is set to the
empty string, we would actually leave `size` unintialized right until we
use it.

Practically, this cannot happen because the
`do_oid_object_info_extended()` function is expected to always populate
the `type_name` if asked for. However, it is quite unnecessary to leave
the code as unwieldy to reason about: Just initialize the variable to 0
and be done with it.

Signed-off-by: Johannes Schindelin <redacted>
---
 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 b13561cf73b..128c901fa8e 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -104,7 +104,7 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
 	struct object_id oid;
 	enum object_type type;
 	char *buf;
-	unsigned long size;
+	unsigned long size = 0;
 	struct object_context obj_context = {0};
 	struct object_info oi = OBJECT_INFO_INIT;
 	struct strbuf sb = STRBUF_INIT;
-- 
gitgitgadget
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help