# [PATCH] object-info: init request_info before reading arg

3 messages from 2023-04-02 to 2023-04-04. Participants: Jiang Xin, Junio C Hamano.
Thread: https://gitlist.dev/t/59523

## Jiang Xin, 2023-04-02 13:05

Subject: [PATCH] object-info: init request_info before reading arg
Message-ID: <20230402130557.17662-1-worldhello.net@gmail.com>
URL: https://gitlist.dev/e/20230402130557.17662-1-worldhello.net%40gmail.com

```
From: Jiang Xin <zhiyou.jx@alibaba-inc.com>

When retrieving object info via capability "object-info", we store the
command args into a requested_info variable, but forget to initialize
it. Initialize the variable before use to prevent unexpected output.

Signed-off-by: Jiang Xin <zhiyou.jx@alibaba-inc.com>
---
 protocol-caps.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/protocol-caps.c b/protocol-caps.c
index 874bc815b4..94c51862c5 100644
--- a/protocol-caps.c
+++ b/protocol-caps.c
@@ -79,7 +79,7 @@ static void send_info(struct repository *r, struct packet_writer *writer,
 
 int cap_object_info(struct repository *r, struct packet_reader *request)
 {
-	struct requested_info info;
+	struct requested_info info = { 0 };
 	struct packet_writer writer;
 	struct string_list oid_str_list = STRING_LIST_INIT_DUP;
 
-- 
2.39.1.418.g7876265d61


```

## Junio C Hamano, 2023-04-03 16:36

Subject: Re: [PATCH] object-info: init request_info before reading arg
Message-ID: <xmqqsfdg7wif.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqqsfdg7wif.fsf%40gitster.g
In-Reply-To: <20230402130557.17662-1-worldhello.net@gmail.com>

```
Jiang Xin <worldhello.net@gmail.com> writes:

> From: Jiang Xin <zhiyou.jx@alibaba-inc.com>
>
> When retrieving object info via capability "object-info", we store the
> command args into a requested_info variable, but forget to initialize
> it. Initialize the variable before use to prevent unexpected output.

Good eyes.  We read the request packets to decide if we want to flip
the .size member of the structure, but the result would not make
much sense if the structure starts with a random garbage in it.

I wonder if we can tell our compilers (or runtime checker) to help
catch a mistake like this.  Did you see our sanitizers complain, or
something?

Will queue.  Thanks.

> Signed-off-by: Jiang Xin <zhiyou.jx@alibaba-inc.com>
> ---
>  protocol-caps.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/protocol-caps.c b/protocol-caps.c
> index 874bc815b4..94c51862c5 100644
> --- a/protocol-caps.c
> +++ b/protocol-caps.c
> @@ -79,7 +79,7 @@ static void send_info(struct repository *r, struct packet_writer *writer,
>  
>  int cap_object_info(struct repository *r, struct packet_reader *request)
>  {
> -	struct requested_info info;
> +	struct requested_info info = { 0 };
>  	struct packet_writer writer;
>  	struct string_list oid_str_list = STRING_LIST_INIT_DUP;

```

## Jiang Xin, 2023-04-04 01:07

Subject: Re: [PATCH] object-info: init request_info before reading arg
Message-ID: <CANYiYbHB9AMpahiULEF=PoVG6y_ogDvVQvWY=ThKUdERmVbQyg@mail.gmail.com>
URL: https://gitlist.dev/e/CANYiYbHB9AMpahiULEF%3DPoVG6y_ogDvVQvWY%3DThKUdERmVbQyg%40mail.gmail.com
In-Reply-To: <xmqqsfdg7wif.fsf@gitster.g>

```
On Tue, Apr 4, 2023 at 12:36 AM Junio C Hamano <gitster@pobox.com> wrote:
>
> Jiang Xin <worldhello.net@gmail.com> writes:
>
> > From: Jiang Xin <zhiyou.jx@alibaba-inc.com>
> >
> > When retrieving object info via capability "object-info", we store the
> > command args into a requested_info variable, but forget to initialize
> > it. Initialize the variable before use to prevent unexpected output.
>
> Good eyes.  We read the request packets to decide if we want to flip
> the .size member of the structure, but the result would not make
> much sense if the structure starts with a random garbage in it.
>
> I wonder if we can tell our compilers (or runtime checker) to help
> catch a mistake like this.  Did you see our sanitizers complain, or
> something?

I accidentally came across this issue when I wanted to implement a new
similar capability. I'm curious why adding "-Wuninitialized" or
"-Wmaybe-uninitialized" to gcc fails to spot this case.

```
