diff mbox series

[1/5] drm/modes: parse_cmdline: Handle empty mode name part

Message ID 64e2e9b14c26df28908789374253fd12072c26c2.1657301107.git.geert@linux-m68k.org (mailing list archive)
State Handled Elsewhere
Headers show
Series drm/modes: Command line mode selection fixes and improvements | expand

Commit Message

Geert Uytterhoeven July 8, 2022, 6:21 p.m. UTC
If no mode name part was specified, mode_end is zero, and the "ret ==
mode_end" check does the wrong thing.

Fix this by checking for a non-zero return value instead.
While at it, skip all named mode handling when mode_end is zero, as it
is futile.

Fixes: 7b1cce760afe38b4 ("drm/modes: parse_cmdline: Allow specifying stand-alone options")
Signed-off-by: Geert Uytterhoeven <geert@linux-m68k.org>
---
 drivers/gpu/drm/drm_modes.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Hans de Goede July 8, 2022, 7:28 p.m. UTC | #1
Hi Geert,

On 7/8/22 20:21, Geert Uytterhoeven wrote:
> If no mode name part was specified, mode_end is zero, and the "ret ==
> mode_end" check does the wrong thing.
> 
> Fix this by checking for a non-zero return value instead.

Which is wrong to do, since now if you have e.g. a mode list
with:

"dblntsc",
"dblntsc-ff"

in there and the cmdline contains "dblntsc-ff" then you
will already stop with a (wrong!) match at "dblntsc".

> While at it, skip all named mode handling when mode_end is zero, as it
> is futile.

AFAICT, this is actually what needs to be done to fix this, while keeping
the ret == mode_end check.

Regards,

Hans


> 
> Fixes: 7b1cce760afe38b4 ("drm/modes: parse_cmdline: Allow specifying stand-alone options")
> Signed-off-by: Geert Uytterhoeven <geert@linux-m68k.org>
> ---
>  drivers/gpu/drm/drm_modes.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/gpu/drm/drm_modes.c b/drivers/gpu/drm/drm_modes.c
> index 14b746f7ba975954..30a7be97707bfb16 100644
> --- a/drivers/gpu/drm/drm_modes.c
> +++ b/drivers/gpu/drm/drm_modes.c
> @@ -1823,9 +1823,9 @@ bool drm_mode_parse_command_line_for_connector(const char *mode_option,
>  	}
>  
>  	/* First check for a named mode */
> -	for (i = 0; i < ARRAY_SIZE(drm_named_modes_whitelist); i++) {
> +	for (i = 0; mode_end && i < ARRAY_SIZE(drm_named_modes_whitelist); i++) {
>  		ret = str_has_prefix(name, drm_named_modes_whitelist[i]);
> -		if (ret == mode_end) {
> +		if (ret) {
>  			if (refresh_ptr)
>  				return false; /* named + refresh is invalid */
>
Geert Uytterhoeven July 8, 2022, 8:06 p.m. UTC | #2
Hi Hans.

On Fri, Jul 8, 2022 at 9:28 PM Hans de Goede <hdegoede@redhat.com> wrote:
> On 7/8/22 20:21, Geert Uytterhoeven wrote:
> > If no mode name part was specified, mode_end is zero, and the "ret ==
> > mode_end" check does the wrong thing.
> >
> > Fix this by checking for a non-zero return value instead.
>
> Which is wrong to do, since now if you have e.g. a mode list
> with:
>
> "dblntsc",
> "dblntsc-ff"
>
> in there and the cmdline contains "dblntsc-ff" then you
> will already stop with a (wrong!) match at "dblntsc".

It indeed behaves that way, and did so before, as str_has_prefix()
checks for a matching prefix, and thus may never get to the full
match.  However, can we change that to an exact match, without
introducing regressions?
This can be avoided by reverse-sorting the modelist (or iterating
backwards through a sorted modelist), though.

> > While at it, skip all named mode handling when mode_end is zero, as it
> > is futile.
>
> AFAICT, this is actually what needs to be done to fix this, while keeping
> the ret == mode_end check.

"ret == mode_end" or "ret" doesn't matter (except for the special
case of mode_end is zero), as str_has_prefix() returns either zero or
the length of the prefix.  Hence it never returns a non-zero value
smaller than the length of the prefix.

> > Fixes: 7b1cce760afe38b4 ("drm/modes: parse_cmdline: Allow specifying stand-alone options")
> > Signed-off-by: Geert Uytterhoeven <geert@linux-m68k.org>
> > ---
> >  drivers/gpu/drm/drm_modes.c | 4 ++--
> >  1 file changed, 2 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/gpu/drm/drm_modes.c b/drivers/gpu/drm/drm_modes.c
> > index 14b746f7ba975954..30a7be97707bfb16 100644
> > --- a/drivers/gpu/drm/drm_modes.c
> > +++ b/drivers/gpu/drm/drm_modes.c
> > @@ -1823,9 +1823,9 @@ bool drm_mode_parse_command_line_for_connector(const char *mode_option,
> >       }
> >
> >       /* First check for a named mode */
> > -     for (i = 0; i < ARRAY_SIZE(drm_named_modes_whitelist); i++) {
> > +     for (i = 0; mode_end && i < ARRAY_SIZE(drm_named_modes_whitelist); i++) {
> >               ret = str_has_prefix(name, drm_named_modes_whitelist[i]);
> > -             if (ret == mode_end) {
> > +             if (ret) {
> >                       if (refresh_ptr)
> >                               return false; /* named + refresh is invalid */

Gr{oetje,eeting}s,

                        Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
Geert Uytterhoeven July 8, 2022, 8:09 p.m. UTC | #3
Hi Hans,

On Fri, Jul 8, 2022 at 10:06 PM Geert Uytterhoeven <geert@linux-m68k.org> wrote:
> On Fri, Jul 8, 2022 at 9:28 PM Hans de Goede <hdegoede@redhat.com> wrote:
> > On 7/8/22 20:21, Geert Uytterhoeven wrote:
> > > If no mode name part was specified, mode_end is zero, and the "ret ==
> > > mode_end" check does the wrong thing.
> > >
> > > Fix this by checking for a non-zero return value instead.
> >
> > Which is wrong to do, since now if you have e.g. a mode list
> > with:
> >
> > "dblntsc",
> > "dblntsc-ff"
> >
> > in there and the cmdline contains "dblntsc-ff" then you
> > will already stop with a (wrong!) match at "dblntsc".
>
> It indeed behaves that way, and did so before, as str_has_prefix()
> checks for a matching prefix, and thus may never get to the full
> match.  However, can we change that to an exact match, without
> introducing regressions?
> This can be avoided by reverse-sorting the modelist (or iterating
> backwards through a sorted modelist), though.
>
> > > While at it, skip all named mode handling when mode_end is zero, as it
> > > is futile.
> >
> > AFAICT, this is actually what needs to be done to fix this, while keeping
> > the ret == mode_end check.
>
> "ret == mode_end" or "ret" doesn't matter (except for the special
> case of mode_end is zero), as str_has_prefix() returns either zero or
> the length of the prefix.  Hence it never returns a non-zero value
> smaller than the length of the prefix.

Ignore that.  I finally saw what's really happening.
And I do agree with your comment.

Gr{oetje,eeting}s,

                        Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
diff mbox series

Patch

diff --git a/drivers/gpu/drm/drm_modes.c b/drivers/gpu/drm/drm_modes.c
index 14b746f7ba975954..30a7be97707bfb16 100644
--- a/drivers/gpu/drm/drm_modes.c
+++ b/drivers/gpu/drm/drm_modes.c
@@ -1823,9 +1823,9 @@  bool drm_mode_parse_command_line_for_connector(const char *mode_option,
 	}
 
 	/* First check for a named mode */
-	for (i = 0; i < ARRAY_SIZE(drm_named_modes_whitelist); i++) {
+	for (i = 0; mode_end && i < ARRAY_SIZE(drm_named_modes_whitelist); i++) {
 		ret = str_has_prefix(name, drm_named_modes_whitelist[i]);
-		if (ret == mode_end) {
+		if (ret) {
 			if (refresh_ptr)
 				return false; /* named + refresh is invalid */