lkml.org 
[lkml]   [2015]   [Apr]   [28]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
SubjectRe: [PATCH] Adjust sysrq_register_handler() to avoid an array subscript warning
From
On 28 April 2015 at 03:49, David Howells <dhowells@redhat.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@redhat.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


\
 
 \ /
  Last update: 2015-04-28 17:21    [W:0.068 / U:0.860 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site