* [PATCH] monitor: fix cases in switch in memory_dump
@ 2024-10-30 14:06 Anastasia Belova
2024-10-30 19:03 ` Phil Dennis-Jordan
0 siblings, 1 reply; 6+ messages in thread
From: Anastasia Belova @ 2024-10-30 14:06 UTC (permalink / raw)
To: Dr. David Alan Gilbert; +Cc: Anastasia Belova, qemu-devel, sdl.qemu
default case has no condition. So if it is placed
higher that other cases, they are unreachable.
Move dafult case down.
Found by Linux Verification Center (linuxtesting.org)
Signed-off-by: Anastasia Belova <abelova@astralinux.ru>
---
monitor/hmp-cmds-target.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/monitor/hmp-cmds-target.c b/monitor/hmp-cmds-target.c
index ff01cf9d8d..eea8ca047b 100644
--- a/monitor/hmp-cmds-target.c
+++ b/monitor/hmp-cmds-target.c
@@ -189,7 +189,6 @@ static void memory_dump(Monitor *mon, int count, int format, int wsize,
i = 0;
while (i < l) {
switch(wsize) {
- default:
case 1:
v = ldub_p(buf + i);
break;
@@ -202,6 +201,9 @@ static void memory_dump(Monitor *mon, int count, int format, int wsize,
case 8:
v = ldq_p(buf + i);
break;
+ default:
+ v = ldub_p(buf + i);
+ break;
}
monitor_printf(mon, " ");
switch(format) {
--
2.47.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH] monitor: fix cases in switch in memory_dump
2024-10-30 14:06 [PATCH] monitor: fix cases in switch in memory_dump Anastasia Belova
@ 2024-10-30 19:03 ` Phil Dennis-Jordan
2024-10-31 0:31 ` Dr. David Alan Gilbert
2024-10-31 11:26 ` Anastasia Belova
0 siblings, 2 replies; 6+ messages in thread
From: Phil Dennis-Jordan @ 2024-10-30 19:03 UTC (permalink / raw)
To: Anastasia Belova; +Cc: Dr. David Alan Gilbert, qemu-devel, sdl.qemu
[-- Attachment #1: Type: text/plain, Size: 1497 bytes --]
On Wed 30. Oct 2024 at 15:09, Anastasia Belova <abelova@astralinux.ru>
wrote:
> default case has no condition. So if it is placed
> higher that other cases, they are unreachable.
>
> Move dafult case down.
>
The stylistic merits might be debatable, but: the order of cases in a
switch block in C does not matter, the default case can appear anywhere.
The other cases are still reachable. So at minimum, the commit message is
incorrect.
> Found by Linux Verification Center (linuxtesting.org)
>
> Signed-off-by: Anastasia Belova <abelova@astralinux.ru>
> ---
> monitor/hmp-cmds-target.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/monitor/hmp-cmds-target.c b/monitor/hmp-cmds-target.c
> index ff01cf9d8d..eea8ca047b 100644
> --- a/monitor/hmp-cmds-target.c
> +++ b/monitor/hmp-cmds-target.c
> @@ -189,7 +189,6 @@ static void memory_dump(Monitor *mon, int count, int
> format, int wsize,
> i = 0;
> while (i < l) {
> switch(wsize) {
> - default:
> case 1:
> v = ldub_p(buf + i);
> break;
> @@ -202,6 +201,9 @@ static void memory_dump(Monitor *mon, int count, int
> format, int wsize,
> case 8:
> v = ldq_p(buf + i);
> break;
> + default:
> + v = ldub_p(buf + i);
> + break;
> }
> monitor_printf(mon, " ");
> switch(format) {
> --
> 2.47.0
>
>
>
[-- Attachment #2: Type: text/html, Size: 2394 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] monitor: fix cases in switch in memory_dump
2024-10-30 19:03 ` Phil Dennis-Jordan
@ 2024-10-31 0:31 ` Dr. David Alan Gilbert
2024-10-31 10:52 ` Peter Maydell
2024-10-31 11:26 ` Anastasia Belova
1 sibling, 1 reply; 6+ messages in thread
From: Dr. David Alan Gilbert @ 2024-10-31 0:31 UTC (permalink / raw)
To: Phil Dennis-Jordan; +Cc: Anastasia Belova, qemu-devel, sdl.qemu
* Phil Dennis-Jordan (lists@philjordan.eu) wrote:
> On Wed 30. Oct 2024 at 15:09, Anastasia Belova <abelova@astralinux.ru>
> wrote:
>
> > default case has no condition. So if it is placed
> > higher that other cases, they are unreachable.
> >
> > Move dafult case down.
> >
>
> The stylistic merits might be debatable, but: the order of cases in a
> switch block in C does not matter, the default case can appear anywhere.
> The other cases are still reachable. So at minimum, the commit message is
> incorrect.
I'd agree; the analysis is wrong - it works as intended.
As for style, I'd normally agree that 'default' at end makes sense,
but:
a) I hate duplicating code
b) in a way this reads nicely:
default:
case 1:
'default is the same as case 1'.
Dave
>
>
> > Found by Linux Verification Center (linuxtesting.org)
> >
> > Signed-off-by: Anastasia Belova <abelova@astralinux.ru>
> > ---
> > monitor/hmp-cmds-target.c | 4 +++-
> > 1 file changed, 3 insertions(+), 1 deletion(-)
> >
> > diff --git a/monitor/hmp-cmds-target.c b/monitor/hmp-cmds-target.c
> > index ff01cf9d8d..eea8ca047b 100644
> > --- a/monitor/hmp-cmds-target.c
> > +++ b/monitor/hmp-cmds-target.c
> > @@ -189,7 +189,6 @@ static void memory_dump(Monitor *mon, int count, int
> > format, int wsize,
> > i = 0;
> > while (i < l) {
> > switch(wsize) {
> > - default:
> > case 1:
> > v = ldub_p(buf + i);
> > break;
> > @@ -202,6 +201,9 @@ static void memory_dump(Monitor *mon, int count, int
> > format, int wsize,
> > case 8:
> > v = ldq_p(buf + i);
> > break;
> > + default:
> > + v = ldub_p(buf + i);
> > + break;
> > }
> > monitor_printf(mon, " ");
> > switch(format) {
> > --
> > 2.47.0
> >
> >
> >
--
-----Open up your eyes, open up your mind, open up your code -------
/ Dr. David Alan Gilbert | Running GNU/Linux | Happy \
\ dave @ treblig.org | | In Hex /
\ _________________________|_____ http://www.treblig.org |_______/
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] monitor: fix cases in switch in memory_dump
2024-10-31 0:31 ` Dr. David Alan Gilbert
@ 2024-10-31 10:52 ` Peter Maydell
2024-10-31 11:26 ` Peter Maydell
0 siblings, 1 reply; 6+ messages in thread
From: Peter Maydell @ 2024-10-31 10:52 UTC (permalink / raw)
To: Dr. David Alan Gilbert
Cc: Phil Dennis-Jordan, Anastasia Belova, qemu-devel, sdl.qemu
On Thu, 31 Oct 2024 at 00:32, Dr. David Alan Gilbert <dave@treblig.org> wrote:
>
> * Phil Dennis-Jordan (lists@philjordan.eu) wrote:
> > On Wed 30. Oct 2024 at 15:09, Anastasia Belova <abelova@astralinux.ru>
> > wrote:
> >
> > > default case has no condition. So if it is placed
> > > higher that other cases, they are unreachable.
> > >
> > > Move dafult case down.
> > >
> >
> > The stylistic merits might be debatable, but: the order of cases in a
> > switch block in C does not matter, the default case can appear anywhere.
> > The other cases are still reachable. So at minimum, the commit message is
> > incorrect.
>
> I'd agree; the analysis is wrong - it works as intended.
> As for style, I'd normally agree that 'default' at end makes sense,
> but:
> a) I hate duplicating code
> b) in a way this reads nicely:
> default:
> case 1:
>
> 'default is the same as case 1'.
Is it actually possible to get here with a wsize that
isn't 1,2,4 or 8, though? This function is used only
by the hmp 'x' and 'xp' commands. Those document that
the valid size specifications are b, h, w or g (for
8, 16, 32 or 64 bits), and monitor_parse_arguments()
doesn't seem to have any undocumented handling that
would result in a different size value. And the
code in memory_dump() doesn't do anything sensible
with a wsize other than 1, 2, 4 or 8 -- if you hand
it a wsize of 3, for instance, I think it will print
every third byte.
So I think that probably the default case here should
be g_assert_not_reached().
Disclaimer: I haven't played with the x and xp commands
to confirm that we really don't ever get here with a
funny wsize value.
thanks
-- PMM
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] monitor: fix cases in switch in memory_dump
2024-10-31 10:52 ` Peter Maydell
@ 2024-10-31 11:26 ` Peter Maydell
0 siblings, 0 replies; 6+ messages in thread
From: Peter Maydell @ 2024-10-31 11:26 UTC (permalink / raw)
To: Dr. David Alan Gilbert
Cc: Phil Dennis-Jordan, Anastasia Belova, qemu-devel, sdl.qemu
On Thu, 31 Oct 2024 at 10:52, Peter Maydell <peter.maydell@linaro.org> wrote:
>
> On Thu, 31 Oct 2024 at 00:32, Dr. David Alan Gilbert <dave@treblig.org> wrote:
> >
> > * Phil Dennis-Jordan (lists@philjordan.eu) wrote:
> > > On Wed 30. Oct 2024 at 15:09, Anastasia Belova <abelova@astralinux.ru>
> > > wrote:
> > >
> > > > default case has no condition. So if it is placed
> > > > higher that other cases, they are unreachable.
> > > >
> > > > Move dafult case down.
> > > >
> > >
> > > The stylistic merits might be debatable, but: the order of cases in a
> > > switch block in C does not matter, the default case can appear anywhere.
> > > The other cases are still reachable. So at minimum, the commit message is
> > > incorrect.
> >
> > I'd agree; the analysis is wrong - it works as intended.
> > As for style, I'd normally agree that 'default' at end makes sense,
> > but:
> > a) I hate duplicating code
> > b) in a way this reads nicely:
> > default:
> > case 1:
> >
> > 'default is the same as case 1'.
>
> Is it actually possible to get here with a wsize that
> isn't 1,2,4 or 8, though? This function is used only
> by the hmp 'x' and 'xp' commands. Those document that
> the valid size specifications are b, h, w or g (for
> 8, 16, 32 or 64 bits), and monitor_parse_arguments()
> doesn't seem to have any undocumented handling that
> would result in a different size value. And the
> code in memory_dump() doesn't do anything sensible
> with a wsize other than 1, 2, 4 or 8 -- if you hand
> it a wsize of 3, for instance, I think it will print
> every third byte.
>
> So I think that probably the default case here should
> be g_assert_not_reached().
...better still, we could replace the whole switch(wsize)
with "v = ldn_p(buf + i, wsize);".
-- PMM
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] monitor: fix cases in switch in memory_dump
2024-10-30 19:03 ` Phil Dennis-Jordan
2024-10-31 0:31 ` Dr. David Alan Gilbert
@ 2024-10-31 11:26 ` Anastasia Belova
1 sibling, 0 replies; 6+ messages in thread
From: Anastasia Belova @ 2024-10-31 11:26 UTC (permalink / raw)
To: Phil Dennis-Jordan; +Cc: Dr. David Alan Gilbert, qemu-devel, sdl.qemu
[-- Attachment #1: Type: text/plain, Size: 1707 bytes --]
> 30 окт. 2024 г., в 22:03, Phil Dennis-Jordan <lists@philjordan.eu> написал(а):
>
>
> On Wed 30. Oct 2024 at 15:09, Anastasia Belova <abelova@astralinux.ru> wrote:
> default case has no condition. So if it is placed
> higher that other cases, they are unreachable.
>
> Move dafult case down.
>
> The stylistic merits might be debatable, but: the order of cases in a switch block in C does not matter, the default case can appear anywhere. The other cases are still reachable. So at minimum, the commit message is incorrect.
>
You’re right, I didn’t know about this. Thank you for reply and patience!
Anastasia Belova
> Found by Linux Verification Center (linuxtesting.org)
>
> Signed-off-by: Anastasia Belova <abelova@astralinux.ru>
> ---
> monitor/hmp-cmds-target.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/monitor/hmp-cmds-target.c b/monitor/hmp-cmds-target.c
> index ff01cf9d8d..eea8ca047b 100644
> --- a/monitor/hmp-cmds-target.c
> +++ b/monitor/hmp-cmds-target.c
> @@ -189,7 +189,6 @@ static void memory_dump(Monitor *mon, int count, int format, int wsize,
> i = 0;
> while (i < l) {
> switch(wsize) {
> - default:
> case 1:
> v = ldub_p(buf + i);
> break;
> @@ -202,6 +201,9 @@ static void memory_dump(Monitor *mon, int count, int format, int wsize,
> case 8:
> v = ldq_p(buf + i);
> break;
> + default:
> + v = ldub_p(buf + i);
> + break;
> }
> monitor_printf(mon, " ");
> switch(format) {
> --
> 2.47.0
[-- Attachment #2: Type: text/html, Size: 7051 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2024-10-31 12:17 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-30 14:06 [PATCH] monitor: fix cases in switch in memory_dump Anastasia Belova
2024-10-30 19:03 ` Phil Dennis-Jordan
2024-10-31 0:31 ` Dr. David Alan Gilbert
2024-10-31 10:52 ` Peter Maydell
2024-10-31 11:26 ` Peter Maydell
2024-10-31 11:26 ` Anastasia Belova
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).