All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/1] mvebu: fix end-of-array check
@ 2022-11-30 18:33 Derek LaHousse
  2022-12-04  7:17 ` [EXT] " Kostya Porotchkin
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Derek LaHousse @ 2022-11-30 18:33 UTC (permalink / raw)
  To: u-boot; +Cc: sr, kostap, pali

Properly seek the end of default_environment variables.

The current algorithm overwrites from the second variable.  This
replacement finds the end of the array of strings.

Stomped variables include "board", "soc", "loadaddr".  These can be
seen on a "env default -a" after patch, but they are not seen with a
version before the patch.

Signed-off-by: Derek LaHousse <derek@seaofdirac.org>
---
 board/Marvell/mvebu_armada-37xx/board.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/board/Marvell/mvebu_armada-37xx/board.c
b/board/Marvell/mvebu_armada-37xx/board.c
index c6ecc323bb..ac29ac5b95 100644
--- a/board/Marvell/mvebu_armada-37xx/board.c
+++ b/board/Marvell/mvebu_armada-37xx/board.c
@@ -100,8 +100,11 @@ int board_late_init(void)
 		return 0;
 
 	/* Find free buffer in default_environment[] for new variables
*/
-	while (*ptr != '\0' && *(ptr+1) != '\0') ptr++;
-	ptr += 2;
+	if (*ptr != '\0') { // Defending against empty default env
+		while ((i = strlen(ptr)) != 0) {
+			ptr += i + 1;
+		}
+	}
 
 	/*
 	 * Ensure that 'env default -a' does not erase permanent MAC
addresses
-- 
2.30.2




^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [EXT] [PATCH 1/1] mvebu: fix end-of-array check
  2022-11-30 18:33 [PATCH 1/1] mvebu: fix end-of-array check Derek LaHousse
@ 2022-12-04  7:17 ` Kostya Porotchkin
  2022-12-04 10:39 ` Pali Rohár
  2022-12-04 21:16 ` Simon Glass
  2 siblings, 0 replies; 11+ messages in thread
From: Kostya Porotchkin @ 2022-12-04  7:17 UTC (permalink / raw)
  To: Derek LaHousse, u-boot; +Cc: sr, pali


________________________________
From: Derek LaHousse <derek@seaofdirac.org>
Sent: Wednesday, November 30, 2022 20:33
To: u-boot@lists.denx.de <u-boot@lists.denx.de>
Cc: sr@denx.de <sr@denx.de>; Kostya Porotchkin <kostap@marvell.com>; pali@kernel.org <pali@kernel.org>
Subject: [EXT] [PATCH 1/1] mvebu: fix end-of-array check

External Email

----------------------------------------------------------------------
Properly seek the end of default_environment variables.

The current algorithm overwrites from the second variable.  This
replacement finds the end of the array of strings.

Stomped variables include "board", "soc", "loadaddr".  These can be
seen on a "env default -a" after patch, but they are not seen with a
version before the patch.

Signed-off-by: Derek LaHousse <derek@seaofdirac.org>
---
 board/Marvell/mvebu_armada-37xx/board.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/board/Marvell/mvebu_armada-37xx/board.c
b/board/Marvell/mvebu_armada-37xx/board.c
index c6ecc323bb..ac29ac5b95 100644
--- a/board/Marvell/mvebu_armada-37xx/board.c
+++ b/board/Marvell/mvebu_armada-37xx/board.c
@@ -100,8 +100,11 @@ int board_late_init(void)
                 return 0;

         /* Find free buffer in default_environment[] for new variables
*/
-       while (*ptr != '\0' && *(ptr+1) != '\0') ptr++;
-       ptr += 2;
+       if (*ptr != '\0') { // Defending against empty default env
+               while ((i = strlen(ptr)) != 0) {
+                       ptr += i + 1;
+               }
+       }

         /*
          * Ensure that 'env default -a' does not erase permanent MAC
addresses

Acked-by: Konstantin Porotchkin <kostap@marvell.com>

--
2.30.2




^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/1] mvebu: fix end-of-array check
  2022-11-30 18:33 [PATCH 1/1] mvebu: fix end-of-array check Derek LaHousse
  2022-12-04  7:17 ` [EXT] " Kostya Porotchkin
@ 2022-12-04 10:39 ` Pali Rohár
  2022-12-05 11:42   ` Stefan Roese
  2022-12-04 21:16 ` Simon Glass
  2 siblings, 1 reply; 11+ messages in thread
From: Pali Rohár @ 2022-12-04 10:39 UTC (permalink / raw)
  To: Derek LaHousse, sr; +Cc: u-boot, kostap

Hello!

I would suggest to change description of the patch from

  "mvebu: fix end-of-array check"

to something like:

  "arm: mvebu: Espressobin: fix end-of-array check in env"

as it affects only Espressobin boards (not other mvebu).

Stefan, please look below as this issue/fix is important.

On Wednesday 30 November 2022 13:33:40 Derek LaHousse wrote:
> Properly seek the end of default_environment variables.
> 
> The current algorithm overwrites from the second variable.  This
> replacement finds the end of the array of strings.
> 
> Stomped variables include "board", "soc", "loadaddr".  These can be
> seen on a "env default -a" after patch, but they are not seen with a
> version before the patch.

This is a real issue which I introduced in the past. So it some fix for
this issue should be pulled into the v2023.01 release.

> Signed-off-by: Derek LaHousse <derek@seaofdirac.org>
> ---
>  board/Marvell/mvebu_armada-37xx/board.c | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
> 
> diff --git a/board/Marvell/mvebu_armada-37xx/board.c
> b/board/Marvell/mvebu_armada-37xx/board.c
> index c6ecc323bb..ac29ac5b95 100644
> --- a/board/Marvell/mvebu_armada-37xx/board.c
> +++ b/board/Marvell/mvebu_armada-37xx/board.c
> @@ -100,8 +100,11 @@ int board_late_init(void)
>  		return 0;
>  
>  	/* Find free buffer in default_environment[] for new variables
> */
> -	while (*ptr != '\0' && *(ptr+1) != '\0') ptr++;
> -	ptr += 2;
> +	if (*ptr != '\0') { // Defending against empty default env
> +		while ((i = strlen(ptr)) != 0) {
> +			ptr += i + 1;
> +		}
> +	}

If I'm looking at the outer-if condition correctly then it is not
needed. strlen("") returns zero and so inner-while loop stops
immediately.

My proposed fix for this issue was just changing correct while loop
check to ensure that ptr is set after the _last_ variable.

-	while (*ptr != '\0' && *(ptr+1) != '\0') ptr++;
-	ptr += 2;
+	while (*ptr != '\0' || *(ptr+1) != '\0') ptr++;
+	ptr++;

Both changes should be equivalent, but I'm not sure which one is more
readable. The original issue was introduced really by non-readable
code...

Stefan, do you have a preference which one fix is better / more
readable?

It would be really a good idea if you try to check if any of those
proposed fixes/patches are now really correct.

>  
>  	/*
>  	 * Ensure that 'env default -a' does not erase permanent MAC
> addresses
> -- 
> 2.30.2
> 
> 
> 

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/1] mvebu: fix end-of-array check
  2022-11-30 18:33 [PATCH 1/1] mvebu: fix end-of-array check Derek LaHousse
  2022-12-04  7:17 ` [EXT] " Kostya Porotchkin
  2022-12-04 10:39 ` Pali Rohár
@ 2022-12-04 21:16 ` Simon Glass
  2022-12-04 21:51   ` Pali Rohár
  2 siblings, 1 reply; 11+ messages in thread
From: Simon Glass @ 2022-12-04 21:16 UTC (permalink / raw)
  To: Derek LaHousse; +Cc: u-boot, sr, kostap, pali

On Thu, 1 Dec 2022 at 07:55, Derek LaHousse <derek@seaofdirac.org> wrote:
>
> Properly seek the end of default_environment variables.
>
> The current algorithm overwrites from the second variable.  This
> replacement finds the end of the array of strings.
>
> Stomped variables include "board", "soc", "loadaddr".  These can be
> seen on a "env default -a" after patch, but they are not seen with a
> version before the patch.
>
> Signed-off-by: Derek LaHousse <derek@seaofdirac.org>
> ---
>  board/Marvell/mvebu_armada-37xx/board.c | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)

Reviewed-by: Simon Glass <sjg@chromium.org>

It seems odd to update the default environment, but if we want to do
this, it should really be done in a function in env/ along with some
tests in test/env

Regards,
Simon

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/1] mvebu: fix end-of-array check
  2022-12-04 21:16 ` Simon Glass
@ 2022-12-04 21:51   ` Pali Rohár
  2022-12-05 16:11     ` Simon Glass
  0 siblings, 1 reply; 11+ messages in thread
From: Pali Rohár @ 2022-12-04 21:51 UTC (permalink / raw)
  To: Simon Glass; +Cc: Derek LaHousse, u-boot, sr, kostap

On Monday 05 December 2022 10:16:51 Simon Glass wrote:
> On Thu, 1 Dec 2022 at 07:55, Derek LaHousse <derek@seaofdirac.org> wrote:
> >
> > Properly seek the end of default_environment variables.
> >
> > The current algorithm overwrites from the second variable.  This
> > replacement finds the end of the array of strings.
> >
> > Stomped variables include "board", "soc", "loadaddr".  These can be
> > seen on a "env default -a" after patch, but they are not seen with a
> > version before the patch.
> >
> > Signed-off-by: Derek LaHousse <derek@seaofdirac.org>
> > ---
> >  board/Marvell/mvebu_armada-37xx/board.c | 7 +++++--
> >  1 file changed, 5 insertions(+), 2 deletions(-)
> 
> Reviewed-by: Simon Glass <sjg@chromium.org>
> 
> It seems odd to update the default environment, but if we want to do
> this, it should really be done in a function in env/ along with some
> tests in test/env
> 
> Regards,
> Simon

Well, this is just a temporary solution until Marek's env patch series is merged:
https://lore.kernel.org/u-boot/20211103232332.2737-1-kabel@kernel.org/

But I agree that tests would prevent these bugs.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/1] mvebu: fix end-of-array check
  2022-12-04 10:39 ` Pali Rohár
@ 2022-12-05 11:42   ` Stefan Roese
  2022-12-05 18:18     ` Pali Rohár
  0 siblings, 1 reply; 11+ messages in thread
From: Stefan Roese @ 2022-12-05 11:42 UTC (permalink / raw)
  To: Pali Rohár, Derek LaHousse; +Cc: u-boot, kostap

Hi!

On 12/4/22 11:39, Pali Rohár wrote:
> Hello!
> 
> I would suggest to change description of the patch from
> 
>    "mvebu: fix end-of-array check"
> 
> to something like:
> 
>    "arm: mvebu: Espressobin: fix end-of-array check in env"
> 
> as it affects only Espressobin boards (not other mvebu).

Yes, please update the commit subject here.

> Stefan, please look below as this issue/fix is important.

Yes.

> On Wednesday 30 November 2022 13:33:40 Derek LaHousse wrote:
>> Properly seek the end of default_environment variables.
>>
>> The current algorithm overwrites from the second variable.  This
>> replacement finds the end of the array of strings.
>>
>> Stomped variables include "board", "soc", "loadaddr".  These can be
>> seen on a "env default -a" after patch, but they are not seen with a
>> version before the patch.
> 
> This is a real issue which I introduced in the past. So it some fix for
> this issue should be pulled into the v2023.01 release.

Understood.

>> Signed-off-by: Derek LaHousse <derek@seaofdirac.org>
>> ---
>>   board/Marvell/mvebu_armada-37xx/board.c | 7 +++++--
>>   1 file changed, 5 insertions(+), 2 deletions(-)
>>
>> diff --git a/board/Marvell/mvebu_armada-37xx/board.c
>> b/board/Marvell/mvebu_armada-37xx/board.c
>> index c6ecc323bb..ac29ac5b95 100644
>> --- a/board/Marvell/mvebu_armada-37xx/board.c
>> +++ b/board/Marvell/mvebu_armada-37xx/board.c
>> @@ -100,8 +100,11 @@ int board_late_init(void)
>>   		return 0;
>>   
>>   	/* Find free buffer in default_environment[] for new variables
>> */
>> -	while (*ptr != '\0' && *(ptr+1) != '\0') ptr++;
>> -	ptr += 2;
>> +	if (*ptr != '\0') { // Defending against empty default env
>> +		while ((i = strlen(ptr)) != 0) {
>> +			ptr += i + 1;
>> +		}
>> +	}
> 
> If I'm looking at the outer-if condition correctly then it is not
> needed. strlen("") returns zero and so inner-while loop stops
> immediately.
> 
> My proposed fix for this issue was just changing correct while loop
> check to ensure that ptr is set after the _last_ variable.
> 
> -	while (*ptr != '\0' && *(ptr+1) != '\0') ptr++;
> -	ptr += 2;
> +	while (*ptr != '\0' || *(ptr+1) != '\0') ptr++;
> +	ptr++;
> 
> Both changes should be equivalent, but I'm not sure which one is more
> readable. The original issue was introduced really by non-readable
> code...
> 
> Stefan, do you have a preference which one fix is better / more
> readable?

I would prefer to get Pali's corrected version included. Could you
please prepare a v2 patch with this update and also with the added
or changed patch subject.

> It would be really a good idea if you try to check if any of those
> proposed fixes/patches are now really correct.

Yes, please do.

Thanks,
Stefan

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/1] mvebu: fix end-of-array check
  2022-12-04 21:51   ` Pali Rohár
@ 2022-12-05 16:11     ` Simon Glass
  0 siblings, 0 replies; 11+ messages in thread
From: Simon Glass @ 2022-12-05 16:11 UTC (permalink / raw)
  To: Pali Rohár; +Cc: Derek LaHousse, u-boot, sr, kostap

Hi Pali,

On Mon, 5 Dec 2022 at 10:51, Pali Rohár <pali@kernel.org> wrote:
>
> On Monday 05 December 2022 10:16:51 Simon Glass wrote:
> > On Thu, 1 Dec 2022 at 07:55, Derek LaHousse <derek@seaofdirac.org> wrote:
> > >
> > > Properly seek the end of default_environment variables.
> > >
> > > The current algorithm overwrites from the second variable.  This
> > > replacement finds the end of the array of strings.
> > >
> > > Stomped variables include "board", "soc", "loadaddr".  These can be
> > > seen on a "env default -a" after patch, but they are not seen with a
> > > version before the patch.
> > >
> > > Signed-off-by: Derek LaHousse <derek@seaofdirac.org>
> > > ---
> > >  board/Marvell/mvebu_armada-37xx/board.c | 7 +++++--
> > >  1 file changed, 5 insertions(+), 2 deletions(-)
> >
> > Reviewed-by: Simon Glass <sjg@chromium.org>
> >
> > It seems odd to update the default environment, but if we want to do
> > this, it should really be done in a function in env/ along with some
> > tests in test/env
> >
> > Regards,
> > Simon
>
> Well, this is just a temporary solution until Marek's env patch series is merged:
> https://lore.kernel.org/u-boot/20211103232332.2737-1-kabel@kernel.org/
>
> But I agree that tests would prevent these bugs.

OK I see.

Regards,
Simon

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/1] mvebu: fix end-of-array check
  2022-12-05 11:42   ` Stefan Roese
@ 2022-12-05 18:18     ` Pali Rohár
  2022-12-06  6:10       ` Stefan Roese
  0 siblings, 1 reply; 11+ messages in thread
From: Pali Rohár @ 2022-12-05 18:18 UTC (permalink / raw)
  To: Stefan Roese; +Cc: Derek LaHousse, u-boot, kostap

On Monday 05 December 2022 12:42:44 Stefan Roese wrote:
> Hi!
> 
> On 12/4/22 11:39, Pali Rohár wrote:
> > Hello!
> > 
> > I would suggest to change description of the patch from
> > 
> >    "mvebu: fix end-of-array check"
> > 
> > to something like:
> > 
> >    "arm: mvebu: Espressobin: fix end-of-array check in env"
> > 
> > as it affects only Espressobin boards (not other mvebu).
> 
> Yes, please update the commit subject here.
> 
> > Stefan, please look below as this issue/fix is important.
> 
> Yes.
> 
> > On Wednesday 30 November 2022 13:33:40 Derek LaHousse wrote:
> > > Properly seek the end of default_environment variables.
> > > 
> > > The current algorithm overwrites from the second variable.  This
> > > replacement finds the end of the array of strings.
> > > 
> > > Stomped variables include "board", "soc", "loadaddr".  These can be
> > > seen on a "env default -a" after patch, but they are not seen with a
> > > version before the patch.
> > 
> > This is a real issue which I introduced in the past. So it some fix for
> > this issue should be pulled into the v2023.01 release.
> 
> Understood.
> 
> > > Signed-off-by: Derek LaHousse <derek@seaofdirac.org>
> > > ---
> > >   board/Marvell/mvebu_armada-37xx/board.c | 7 +++++--
> > >   1 file changed, 5 insertions(+), 2 deletions(-)
> > > 
> > > diff --git a/board/Marvell/mvebu_armada-37xx/board.c
> > > b/board/Marvell/mvebu_armada-37xx/board.c
> > > index c6ecc323bb..ac29ac5b95 100644
> > > --- a/board/Marvell/mvebu_armada-37xx/board.c
> > > +++ b/board/Marvell/mvebu_armada-37xx/board.c
> > > @@ -100,8 +100,11 @@ int board_late_init(void)
> > >   		return 0;
> > >   	/* Find free buffer in default_environment[] for new variables
> > > */
> > > -	while (*ptr != '\0' && *(ptr+1) != '\0') ptr++;
> > > -	ptr += 2;
> > > +	if (*ptr != '\0') { // Defending against empty default env
> > > +		while ((i = strlen(ptr)) != 0) {
> > > +			ptr += i + 1;
> > > +		}
> > > +	}
> > 
> > If I'm looking at the outer-if condition correctly then it is not
> > needed. strlen("") returns zero and so inner-while loop stops
> > immediately.
> > 
> > My proposed fix for this issue was just changing correct while loop
> > check to ensure that ptr is set after the _last_ variable.
> > 
> > -	while (*ptr != '\0' && *(ptr+1) != '\0') ptr++;
> > -	ptr += 2;
> > +	while (*ptr != '\0' || *(ptr+1) != '\0') ptr++;
> > +	ptr++;
> > 
> > Both changes should be equivalent, but I'm not sure which one is more
> > readable. The original issue was introduced really by non-readable
> > code...
> > 
> > Stefan, do you have a preference which one fix is better / more
> > readable?
> 
> I would prefer to get Pali's corrected version included. Could you
> please prepare a v2 patch with this update and also with the added
> or changed patch subject.

Originally this issue was reported half year ago on the armbian forum:
https://forum.armbian.com/topic/19564-making-espressobin-v7-work-in-2022/?do=findComment&comment=138136

U-Boot "changed" variable name scriptaddr to criptaddr (without leading
s) and broke booting.

Details are later in comments:
https://forum.armbian.com/topic/19564-making-espressobin-v7-work-in-2022/?do=findComment&comment=154062
https://forum.armbian.com/topic/19564-making-espressobin-v7-work-in-2022/?do=findComment&comment=154235

If you prefer my variant, I can prepare a v2 patch.

Please let me know.

> > It would be really a good idea if you try to check if any of those
> > proposed fixes/patches are now really correct.
> 
> Yes, please do.
> 
> Thanks,
> Stefan

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/1] mvebu: fix end-of-array check
  2022-12-05 18:18     ` Pali Rohár
@ 2022-12-06  6:10       ` Stefan Roese
  2022-12-06 19:56         ` Pali Rohár
  0 siblings, 1 reply; 11+ messages in thread
From: Stefan Roese @ 2022-12-06  6:10 UTC (permalink / raw)
  To: Pali Rohár; +Cc: Derek LaHousse, u-boot, kostap

Hi Pali,

On 12/5/22 19:18, Pali Rohár wrote:
> On Monday 05 December 2022 12:42:44 Stefan Roese wrote:
>> Hi!
>>
>> On 12/4/22 11:39, Pali Rohár wrote:
>>> Hello!
>>>
>>> I would suggest to change description of the patch from
>>>
>>>     "mvebu: fix end-of-array check"
>>>
>>> to something like:
>>>
>>>     "arm: mvebu: Espressobin: fix end-of-array check in env"
>>>
>>> as it affects only Espressobin boards (not other mvebu).
>>
>> Yes, please update the commit subject here.
>>
>>> Stefan, please look below as this issue/fix is important.
>>
>> Yes.
>>
>>> On Wednesday 30 November 2022 13:33:40 Derek LaHousse wrote:
>>>> Properly seek the end of default_environment variables.
>>>>
>>>> The current algorithm overwrites from the second variable.  This
>>>> replacement finds the end of the array of strings.
>>>>
>>>> Stomped variables include "board", "soc", "loadaddr".  These can be
>>>> seen on a "env default -a" after patch, but they are not seen with a
>>>> version before the patch.
>>>
>>> This is a real issue which I introduced in the past. So it some fix for
>>> this issue should be pulled into the v2023.01 release.
>>
>> Understood.
>>
>>>> Signed-off-by: Derek LaHousse <derek@seaofdirac.org>
>>>> ---
>>>>    board/Marvell/mvebu_armada-37xx/board.c | 7 +++++--
>>>>    1 file changed, 5 insertions(+), 2 deletions(-)
>>>>
>>>> diff --git a/board/Marvell/mvebu_armada-37xx/board.c
>>>> b/board/Marvell/mvebu_armada-37xx/board.c
>>>> index c6ecc323bb..ac29ac5b95 100644
>>>> --- a/board/Marvell/mvebu_armada-37xx/board.c
>>>> +++ b/board/Marvell/mvebu_armada-37xx/board.c
>>>> @@ -100,8 +100,11 @@ int board_late_init(void)
>>>>    		return 0;
>>>>    	/* Find free buffer in default_environment[] for new variables
>>>> */
>>>> -	while (*ptr != '\0' && *(ptr+1) != '\0') ptr++;
>>>> -	ptr += 2;
>>>> +	if (*ptr != '\0') { // Defending against empty default env
>>>> +		while ((i = strlen(ptr)) != 0) {
>>>> +			ptr += i + 1;
>>>> +		}
>>>> +	}
>>>
>>> If I'm looking at the outer-if condition correctly then it is not
>>> needed. strlen("") returns zero and so inner-while loop stops
>>> immediately.
>>>
>>> My proposed fix for this issue was just changing correct while loop
>>> check to ensure that ptr is set after the _last_ variable.
>>>
>>> -	while (*ptr != '\0' && *(ptr+1) != '\0') ptr++;
>>> -	ptr += 2;
>>> +	while (*ptr != '\0' || *(ptr+1) != '\0') ptr++;
>>> +	ptr++;
>>>
>>> Both changes should be equivalent, but I'm not sure which one is more
>>> readable. The original issue was introduced really by non-readable
>>> code...
>>>
>>> Stefan, do you have a preference which one fix is better / more
>>> readable?
>>
>> I would prefer to get Pali's corrected version included. Could you
>> please prepare a v2 patch with this update and also with the added
>> or changed patch subject.
> 
> Originally this issue was reported half year ago on the armbian forum:
> https://forum.armbian.com/topic/19564-making-espressobin-v7-work-in-2022/?do=findComment&comment=138136
> 
> U-Boot "changed" variable name scriptaddr to criptaddr (without leading
> s) and broke booting.
> 
> Details are later in comments:
> https://forum.armbian.com/topic/19564-making-espressobin-v7-work-in-2022/?do=findComment&comment=154062
> https://forum.armbian.com/topic/19564-making-espressobin-v7-work-in-2022/?do=findComment&comment=154235
> 
> If you prefer my variant, I can prepare a v2 patch.

Yes, please do. That's easiest for me. I'll push this quickly then.

Thanks,
Stefan

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/1] mvebu: fix end-of-array check
  2022-12-06  6:10       ` Stefan Roese
@ 2022-12-06 19:56         ` Pali Rohár
  2022-12-07  7:14           ` Stefan Roese
  0 siblings, 1 reply; 11+ messages in thread
From: Pali Rohár @ 2022-12-06 19:56 UTC (permalink / raw)
  To: Stefan Roese; +Cc: Derek LaHousse, u-boot, kostap

On Tuesday 06 December 2022 07:10:15 Stefan Roese wrote:
> Hi Pali,
> 
> On 12/5/22 19:18, Pali Rohár wrote:
> > On Monday 05 December 2022 12:42:44 Stefan Roese wrote:
> > > Hi!
> > > 
> > > On 12/4/22 11:39, Pali Rohár wrote:
> > > > Hello!
> > > > 
> > > > I would suggest to change description of the patch from
> > > > 
> > > >     "mvebu: fix end-of-array check"
> > > > 
> > > > to something like:
> > > > 
> > > >     "arm: mvebu: Espressobin: fix end-of-array check in env"
> > > > 
> > > > as it affects only Espressobin boards (not other mvebu).
> > > 
> > > Yes, please update the commit subject here.
> > > 
> > > > Stefan, please look below as this issue/fix is important.
> > > 
> > > Yes.
> > > 
> > > > On Wednesday 30 November 2022 13:33:40 Derek LaHousse wrote:
> > > > > Properly seek the end of default_environment variables.
> > > > > 
> > > > > The current algorithm overwrites from the second variable.  This
> > > > > replacement finds the end of the array of strings.
> > > > > 
> > > > > Stomped variables include "board", "soc", "loadaddr".  These can be
> > > > > seen on a "env default -a" after patch, but they are not seen with a
> > > > > version before the patch.
> > > > 
> > > > This is a real issue which I introduced in the past. So it some fix for
> > > > this issue should be pulled into the v2023.01 release.
> > > 
> > > Understood.
> > > 
> > > > > Signed-off-by: Derek LaHousse <derek@seaofdirac.org>
> > > > > ---
> > > > >    board/Marvell/mvebu_armada-37xx/board.c | 7 +++++--
> > > > >    1 file changed, 5 insertions(+), 2 deletions(-)
> > > > > 
> > > > > diff --git a/board/Marvell/mvebu_armada-37xx/board.c
> > > > > b/board/Marvell/mvebu_armada-37xx/board.c
> > > > > index c6ecc323bb..ac29ac5b95 100644
> > > > > --- a/board/Marvell/mvebu_armada-37xx/board.c
> > > > > +++ b/board/Marvell/mvebu_armada-37xx/board.c
> > > > > @@ -100,8 +100,11 @@ int board_late_init(void)
> > > > >    		return 0;
> > > > >    	/* Find free buffer in default_environment[] for new variables
> > > > > */
> > > > > -	while (*ptr != '\0' && *(ptr+1) != '\0') ptr++;
> > > > > -	ptr += 2;
> > > > > +	if (*ptr != '\0') { // Defending against empty default env
> > > > > +		while ((i = strlen(ptr)) != 0) {
> > > > > +			ptr += i + 1;
> > > > > +		}
> > > > > +	}
> > > > 
> > > > If I'm looking at the outer-if condition correctly then it is not
> > > > needed. strlen("") returns zero and so inner-while loop stops
> > > > immediately.
> > > > 
> > > > My proposed fix for this issue was just changing correct while loop
> > > > check to ensure that ptr is set after the _last_ variable.
> > > > 
> > > > -	while (*ptr != '\0' && *(ptr+1) != '\0') ptr++;
> > > > -	ptr += 2;
> > > > +	while (*ptr != '\0' || *(ptr+1) != '\0') ptr++;
> > > > +	ptr++;
> > > > 
> > > > Both changes should be equivalent, but I'm not sure which one is more
> > > > readable. The original issue was introduced really by non-readable
> > > > code...
> > > > 
> > > > Stefan, do you have a preference which one fix is better / more
> > > > readable?
> > > 
> > > I would prefer to get Pali's corrected version included. Could you
> > > please prepare a v2 patch with this update and also with the added
> > > or changed patch subject.
> > 
> > Originally this issue was reported half year ago on the armbian forum:
> > https://forum.armbian.com/topic/19564-making-espressobin-v7-work-in-2022/?do=findComment&comment=138136
> > 
> > U-Boot "changed" variable name scriptaddr to criptaddr (without leading
> > s) and broke booting.
> > 
> > Details are later in comments:
> > https://forum.armbian.com/topic/19564-making-espressobin-v7-work-in-2022/?do=findComment&comment=154062
> > https://forum.armbian.com/topic/19564-making-espressobin-v7-work-in-2022/?do=findComment&comment=154235
> > 
> > If you prefer my variant, I can prepare a v2 patch.
> 
> Yes, please do. That's easiest for me. I'll push this quickly then.
> 
> Thanks,
> Stefan

Hello Stefan! During discussion on the forum, Derek pointed that my
"fix" does not work for another edge case when *ptr is '\0' before
entering into while-loop. That is a valid point and code needs to be
adjusted.

With Derek we do not have an agreement how to write this peace of the
code in readable way, which should move ptr pointer to the end of env
list - find two nul bytes which indicates end of the env list and then
set ptr to the second nul byte, so at this position can be put another
new variable. Plus handle special case nul byte is at the beginning.

My idea was to use single while loop to find this location to have code
minimal in its size. Derek thinks that code is better readable if
iteration is done via strlen() calls in while-loop, like it is in this
version.

Stefan, do you have an idea how to write this code in a way that is fast
and also readable and maintainable?

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/1] mvebu: fix end-of-array check
  2022-12-06 19:56         ` Pali Rohár
@ 2022-12-07  7:14           ` Stefan Roese
  0 siblings, 0 replies; 11+ messages in thread
From: Stefan Roese @ 2022-12-07  7:14 UTC (permalink / raw)
  To: Pali Rohár; +Cc: Derek LaHousse, u-boot, kostap

Hi Pali,
Hi Derek,

On 12/6/22 20:56, Pali Rohár wrote:
> On Tuesday 06 December 2022 07:10:15 Stefan Roese wrote:
>> Hi Pali,
>>
>> On 12/5/22 19:18, Pali Rohár wrote:
>>> On Monday 05 December 2022 12:42:44 Stefan Roese wrote:
>>>> Hi!
>>>>
>>>> On 12/4/22 11:39, Pali Rohár wrote:
>>>>> Hello!
>>>>>
>>>>> I would suggest to change description of the patch from
>>>>>
>>>>>      "mvebu: fix end-of-array check"
>>>>>
>>>>> to something like:
>>>>>
>>>>>      "arm: mvebu: Espressobin: fix end-of-array check in env"
>>>>>
>>>>> as it affects only Espressobin boards (not other mvebu).
>>>>
>>>> Yes, please update the commit subject here.
>>>>
>>>>> Stefan, please look below as this issue/fix is important.
>>>>
>>>> Yes.
>>>>
>>>>> On Wednesday 30 November 2022 13:33:40 Derek LaHousse wrote:
>>>>>> Properly seek the end of default_environment variables.
>>>>>>
>>>>>> The current algorithm overwrites from the second variable.  This
>>>>>> replacement finds the end of the array of strings.
>>>>>>
>>>>>> Stomped variables include "board", "soc", "loadaddr".  These can be
>>>>>> seen on a "env default -a" after patch, but they are not seen with a
>>>>>> version before the patch.
>>>>>
>>>>> This is a real issue which I introduced in the past. So it some fix for
>>>>> this issue should be pulled into the v2023.01 release.
>>>>
>>>> Understood.
>>>>
>>>>>> Signed-off-by: Derek LaHousse <derek@seaofdirac.org>
>>>>>> ---
>>>>>>     board/Marvell/mvebu_armada-37xx/board.c | 7 +++++--
>>>>>>     1 file changed, 5 insertions(+), 2 deletions(-)
>>>>>>
>>>>>> diff --git a/board/Marvell/mvebu_armada-37xx/board.c
>>>>>> b/board/Marvell/mvebu_armada-37xx/board.c
>>>>>> index c6ecc323bb..ac29ac5b95 100644
>>>>>> --- a/board/Marvell/mvebu_armada-37xx/board.c
>>>>>> +++ b/board/Marvell/mvebu_armada-37xx/board.c
>>>>>> @@ -100,8 +100,11 @@ int board_late_init(void)
>>>>>>     		return 0;
>>>>>>     	/* Find free buffer in default_environment[] for new variables
>>>>>> */
>>>>>> -	while (*ptr != '\0' && *(ptr+1) != '\0') ptr++;
>>>>>> -	ptr += 2;
>>>>>> +	if (*ptr != '\0') { // Defending against empty default env
>>>>>> +		while ((i = strlen(ptr)) != 0) {
>>>>>> +			ptr += i + 1;
>>>>>> +		}
>>>>>> +	}
>>>>>
>>>>> If I'm looking at the outer-if condition correctly then it is not
>>>>> needed. strlen("") returns zero and so inner-while loop stops
>>>>> immediately.
>>>>>
>>>>> My proposed fix for this issue was just changing correct while loop
>>>>> check to ensure that ptr is set after the _last_ variable.
>>>>>
>>>>> -	while (*ptr != '\0' && *(ptr+1) != '\0') ptr++;
>>>>> -	ptr += 2;
>>>>> +	while (*ptr != '\0' || *(ptr+1) != '\0') ptr++;
>>>>> +	ptr++;
>>>>>
>>>>> Both changes should be equivalent, but I'm not sure which one is more
>>>>> readable. The original issue was introduced really by non-readable
>>>>> code...
>>>>>
>>>>> Stefan, do you have a preference which one fix is better / more
>>>>> readable?
>>>>
>>>> I would prefer to get Pali's corrected version included. Could you
>>>> please prepare a v2 patch with this update and also with the added
>>>> or changed patch subject.
>>>
>>> Originally this issue was reported half year ago on the armbian forum:
>>> https://forum.armbian.com/topic/19564-making-espressobin-v7-work-in-2022/?do=findComment&comment=138136
>>>
>>> U-Boot "changed" variable name scriptaddr to criptaddr (without leading
>>> s) and broke booting.
>>>
>>> Details are later in comments:
>>> https://forum.armbian.com/topic/19564-making-espressobin-v7-work-in-2022/?do=findComment&comment=154062
>>> https://forum.armbian.com/topic/19564-making-espressobin-v7-work-in-2022/?do=findComment&comment=154235
>>>
>>> If you prefer my variant, I can prepare a v2 patch.
>>
>> Yes, please do. That's easiest for me. I'll push this quickly then.
>>
>> Thanks,
>> Stefan
> 
> Hello Stefan! During discussion on the forum, Derek pointed that my
> "fix" does not work for another edge case when *ptr is '\0' before
> entering into while-loop. That is a valid point and code needs to be
> adjusted.
> 
> With Derek we do not have an agreement how to write this peace of the
> code in readable way, which should move ptr pointer to the end of env
> list - find two nul bytes which indicates end of the env list and then
> set ptr to the second nul byte, so at this position can be put another
> new variable. Plus handle special case nul byte is at the beginning.
> 
> My idea was to use single while loop to find this location to have code
> minimal in its size. Derek thinks that code is better readable if
> iteration is done via strlen() calls in while-loop, like it is in this
> version.
> 
> Stefan, do you have an idea how to write this code in a way that is fast
> and also readable and maintainable?

Sorry, I don't have an "idea" quickly that might be "better" than your
suggested version(s) without diving into this deeper. You are both
experienced developers I assume. I have no real problem using Derek's
version if this solves the problem and is a bit slower. The code
patch we are talking about is not accessed frequently I assume.

Thanks,
Stefan

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2022-12-07  7:14 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-30 18:33 [PATCH 1/1] mvebu: fix end-of-array check Derek LaHousse
2022-12-04  7:17 ` [EXT] " Kostya Porotchkin
2022-12-04 10:39 ` Pali Rohár
2022-12-05 11:42   ` Stefan Roese
2022-12-05 18:18     ` Pali Rohár
2022-12-06  6:10       ` Stefan Roese
2022-12-06 19:56         ` Pali Rohár
2022-12-07  7:14           ` Stefan Roese
2022-12-04 21:16 ` Simon Glass
2022-12-04 21:51   ` Pali Rohár
2022-12-05 16:11     ` Simon Glass

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.