[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <CANLsYky-0mXg3gcgerg+NR_3S=yX64WJr2c2rxdm-7s6M0K=vQ@mail.gmail.com>
Date: Tue, 28 Apr 2015 08:41:56 -0600
From: Mathieu Poirier <mathieu.poirier@...aro.org>
To: David Howells <dhowells@...hat.com>
Cc: torvalds@...ux-foundation.org,
"linux-kernel@...r.kernel.org" <linux-kernel@...r.kernel.org>
Subject: Re: [PATCH] Adjust sysrq_register_handler() to avoid an array
subscript warning
On 28 April 2015 at 03:49, David Howells <dhowells@...hat.com> 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@...hat.com>
> ---
>
> 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@...r.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Powered by blists - more mailing lists