diff mbox

btrfs-progs: don't overrun "answer" array in cmds-chunk.c

Message ID 52006509.1060207@redhat.com (mailing list archive)
State Under Review, archived
Headers show

Commit Message

Eric Sandeen Aug. 6, 2013, 2:52 a.m. UTC
If an array is 5 chars in size:

       char answer[5];

and we write the 6th char (counting from 0)...

       answer[5] = '\0';

we get problems:

cmds-chunk.c: In function 'ask_user.clone.0':
cmds-chunk.c:1343: warning: array subscript is above array bounds

Fix it...

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
---



--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Comments

Miao Xie Aug. 6, 2013, 3:17 a.m. UTC | #1
On 	mon, 05 Aug 2013 21:52:57 -0500, Eric Sandeen wrote:
> If an array is 5 chars in size:
> 
>        char answer[5];
> 
> and we write the 6th char (counting from 0)...
> 
>        answer[5] = '\0';
> 
> we get problems:
> 
> cmds-chunk.c: In function 'ask_user.clone.0':
> cmds-chunk.c:1343: warning: array subscript is above array bounds
> 
> Fix it...
> 
> Signed-off-by: Eric Sandeen <sandeen@redhat.com>
> ---

Thanks to fix this problem.

Reviewed-by: Miao Xie <miaox@cn.fujitsu.com>

> 
> 
> diff --git a/cmds-chunk.c b/cmds-chunk.c
> index 03314de..7c3257c 100644
> --- a/cmds-chunk.c
> +++ b/cmds-chunk.c
> @@ -1340,7 +1340,7 @@ again:
>  		}
>  		i++;
>  	}
> -	answer[5] = '\0';
> +	answer[4] = '\0';
>  	__fpurge(stdin);
>  
>  	if (strlen(answer) == 0) {
> 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Zach Brown Aug. 6, 2013, 3:57 a.m. UTC | #2
On Mon, Aug 05, 2013 at 09:52:57PM -0500, Eric Sandeen wrote:
> If an array is 5 chars in size:
> 
>        char answer[5];
> 
> and we write the 6th char (counting from 0)...
> 
>        answer[5] = '\0';

*high fives*

> -	answer[5] = '\0';
> +	answer[4] = '\0';

I went to see which way of avoiding another magical raw constant would
be best and did a bit of a double take.

If you're in here, want to reimplement this thing in a few lines of
scanf(%s) and strcasecmp()?  I can give it a go if you don't want to.

- z
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Eric Sandeen Aug. 6, 2013, 4:03 a.m. UTC | #3
On 8/5/13 10:57 PM, Zach Brown wrote:
> On Mon, Aug 05, 2013 at 09:52:57PM -0500, Eric Sandeen wrote:
>> If an array is 5 chars in size:
>>
>>        char answer[5];
>>
>> and we write the 6th char (counting from 0)...
>>
>>        answer[5] = '\0';
> 
> *high fives*
> 
>> -	answer[5] = '\0';
>> +	answer[4] = '\0';
> 
> I went to see which way of avoiding another magical raw constant would
> be best and did a bit of a double take.
> 
> If you're in here, want to reimplement this thing in a few lines of
> scanf(%s) and strcasecmp()?  I can give it a go if you don't want to.

Zach, I think you'd do that really, really well.  ;)

-Eric

(sorry for just going for the expedient/obvious/1-char fix :) )
 
> - z
> 

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Miao Xie Aug. 6, 2013, 5:57 a.m. UTC | #4
On Mon, 5 Aug 2013 20:57:57 -0700, Zach Brown wrote:
> On Mon, Aug 05, 2013 at 09:52:57PM -0500, Eric Sandeen wrote:
>> If an array is 5 chars in size:
>>
>>        char answer[5];
>>
>> and we write the 6th char (counting from 0)...
>>
>>        answer[5] = '\0';
> 
> *high fives*
> 
>> -	answer[5] = '\0';
>> +	answer[4] = '\0';
> 
> I went to see which way of avoiding another magical raw constant would
> be best and did a bit of a double take.
> 
> If you're in here, want to reimplement this thing in a few lines of
> scanf(%s) and strcasecmp()?  I can give it a go if you don't want to.
> 

I think it is better that moving it to utils.c because the other commands
may use it in the future.

Thanks
Miao

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/cmds-chunk.c b/cmds-chunk.c
index 03314de..7c3257c 100644
--- a/cmds-chunk.c
+++ b/cmds-chunk.c
@@ -1340,7 +1340,7 @@  again:
 		}
 		i++;
 	}
-	answer[5] = '\0';
+	answer[4] = '\0';
 	__fpurge(stdin);
 
 	if (strlen(answer) == 0) {