Messages in this thread | | | Date | Tue, 28 Apr 2015 08:41:56 -0600 | Subject | Re: [PATCH] Adjust sysrq_register_handler() to avoid an array subscript warning | From | Mathieu Poirier <> |
| |
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
| |