# [PATCH] Add overflow check to get_delta_hdr_size

3 messages from 2017-08-12 to 2017-08-14. Participants: Martin Koegler, Junio C Hamano.
Thread: https://gitlist.dev/t/46574

## Martin Koegler, 2017-08-12 07:27

Subject: [PATCH] Add overflow check to get_delta_hdr_size
Message-ID: <1502522838-12581-1-git-send-email-martin@mail.zuhause>
URL: https://gitlist.dev/e/1502522838-12581-1-git-send-email-martin%40mail.zuhause

```
From: Martin Koegler <martin.koegler@chello.at>

Signed-off-by: Martin Koegler <martin.koegler@chello.at>
---
Applies on top of my size_t series

I'm not sure, if die or error is better.

 delta.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/delta.h b/delta.h
index 2df0f55..18a4983 100644
--- a/delta.h
+++ b/delta.h
@@ -96,6 +96,11 @@ static inline size_t get_delta_hdr_size(const unsigned char **datap,
 		cmd = *data++;
 		size |= (cmd & 0x7f) << i;
 		i += 7;
+		if (bitsizeof(size_t) <= i) {
+			error("too large object size");
+			size = 0;
+			break;
+		}
 	} while (cmd & 0x80 && data < top);
 	*datap = data;
 	return size;
-- 
2.1.4


```

## Junio C Hamano, 2017-08-14 16:55

Subject: Re: [PATCH] Add overflow check to get_delta_hdr_size
Message-ID: <xmqqlgmmw0lc.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqqlgmmw0lc.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <1502522838-12581-1-git-send-email-martin@mail.zuhause>

```
Martin Koegler <martin.koegler@chello.at> writes:

> From: Martin Koegler <martin.koegler@chello.at>
>
> Signed-off-by: Martin Koegler <martin.koegler@chello.at>
> ---
> Applies on top of my size_t series
>
> I'm not sure, if die or error is better.

As there is no fallback when we hit it, die would be sufficient; the
only thing the callers of this helper, or their callers, could do is
as a reaction to an error return from here would be to die themselves,
I would think.

>
>  delta.h | 5 +++++
>  1 file changed, 5 insertions(+)
>
> diff --git a/delta.h b/delta.h
> index 2df0f55..18a4983 100644
> --- a/delta.h
> +++ b/delta.h
> @@ -96,6 +96,11 @@ static inline size_t get_delta_hdr_size(const unsigned char **datap,
>  		cmd = *data++;
>  		size |= (cmd & 0x7f) << i;
>  		i += 7;
> +		if (bitsizeof(size_t) <= i) {
> +			error("too large object size");
> +			size = 0;
> +			break;
> +		}
>  	} while (cmd & 0x80 && data < top);
>  	*datap = data;
>  	return size;

```

## Junio C Hamano, 2017-08-14 16:56

Subject: Re: [PATCH] Add overflow check to get_delta_hdr_size
Message-ID: <xmqqk226w0kf.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqqk226w0kf.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <1502522838-12581-1-git-send-email-martin@mail.zuhause>

```
Martin Koegler <martin.koegler@chello.at> writes:

> From: Martin Koegler <martin.koegler@chello.at>
>
> Signed-off-by: Martin Koegler <martin.koegler@chello.at>
> ---
> Applies on top of my size_t series
>
> I'm not sure, if die or error is better.

As there is no fallback when we hit it, die would be sufficient; the
only thing the callers of this helper, or their callers, could do is
as a reaction to an error return from here would be to die themselves,
I would think.




>  delta.h | 5 +++++
>  1 file changed, 5 insertions(+)
>
> diff --git a/delta.h b/delta.h
> index 2df0f55..18a4983 100644
> --- a/delta.h
> +++ b/delta.h
> @@ -96,6 +96,11 @@ static inline size_t get_delta_hdr_size(const unsigned char **datap,
>  		cmd = *data++;
>  		size |= (cmd & 0x7f) << i;
>  		i += 7;
> +		if (bitsizeof(size_t) <= i) {
> +			error("too large object size");
> +			size = 0;
> +			break;
> +		}
>  	} while (cmd & 0x80 && data < top);
>  	*datap = data;
>  	return size;

```
