Re: [PATCH] Adjust sysrq_register_handler() to avoid an array subscript warning
From: Mathieu Poirier
Date: Tue Apr 28 2015 - 10:42:04 EST
On 28 April 2015 at 03:49, David Howells <dhowells@xxxxxxxxxx> wrote:
> sysrq_register_handler() iterates over platform_sysrq_reset_seq[] using
> ARRAY_SIZE() on sysrq_reset_seq[] as a limit (indeed, the platform array is
> expected to be shorter). gcc-5 spots the potential dereference beyond the end
> of the array and issues the following warnings:
>
> CC drivers/tty/sysrq.o
> ../drivers/tty/sysrq.c: In function 'sysrq_init':
> ../drivers/tty/sysrq.c:958:33: warning: array subscript is above array bounds [-Warray-bounds]
> key = platform_sysrq_reset_seq[i];
> ^
> ../drivers/tty/sysrq.c: In function 'sysrq_toggle_support':
> ../drivers/tty/sysrq.c:958:33: warning: array subscript is above array bounds [-Warray-bounds]
> key = platform_sysrq_reset_seq[i];
> ^
>
> Since the platform_sysrq_reset_seq[] array is apparently meant to be
> terminated rather than being fixed length, use a pointer to iterate over it
> instead.
>
> One further note: Should platform_sysrq_reset_seq[] be const?
>
> Signed-off-by: David Howells <dhowells@xxxxxxxxxx>
> ---
>
> drivers/tty/sysrq.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/tty/sysrq.c b/drivers/tty/sysrq.c
> index 843f2cdc280b..431af8b6bdb7 100644
> --- a/drivers/tty/sysrq.c
> +++ b/drivers/tty/sysrq.c
> @@ -949,13 +949,14 @@ static bool sysrq_handler_registered;
>
> static inline void sysrq_register_handler(void)
> {
> + const unsigned short *p = platform_sysrq_reset_seq;
> unsigned short key;
> int error;
> int i;
>
> /* First check if a __weak interface was instantiated. */
> for (i = 0; i < ARRAY_SIZE(sysrq_reset_seq); i++) {
> - key = platform_sysrq_reset_seq[i];
> + key = *p++;
> if (key == KEY_RESERVED || key > KEY_MAX)
> break;
>
>
It's so obvious when reading the code again...
"platform_sysrq_reset_seq[]" should be of length SYSRQ_KEY_RESET_MAX
and initialised to KEY_RESERVED. Making it "const" is probably not a
bad idea too. If you don't have time (or the interest) to make a
patch I'll do it.
Thanks for spotting this,
Mathieu
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/