linux-renesas-soc.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH mmc-utils] fix GCC7 build by refactoring trimming routines
@ 2018-12-03 13:19 Wolfram Sang
  2018-12-03 14:02 ` Avri Altman
  0 siblings, 1 reply; 4+ messages in thread
From: Wolfram Sang @ 2018-12-03 13:19 UTC (permalink / raw)
  To: linux-mmc; +Cc: linux-renesas-soc, Chris Ball, Wolfram Sang

I got a compile error with GCC7. When trimming white spaces from strings
lsmmc uses strncpy with overlapping memory areas. This is not allowed.
In addition, the implementation was not efficient with calling strlen
and strncpy once per iteration. Refactor the code to be valid and more
effective.

Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
---
 lsmmc.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/lsmmc.c b/lsmmc.c
index c4faa00..9737b37 100644
--- a/lsmmc.c
+++ b/lsmmc.c
@@ -316,8 +316,9 @@ int parse_ids(struct config *config)
 /* MMC/SD file parsing functions */
 char *read_file(char *name)
 {
-	char *preparsed;
 	char line[4096];
+	char *preparsed, *start = line;
+	int len;
 	FILE *f;
 
 	f = fopen(name, "r");
@@ -348,12 +349,17 @@ char *read_file(char *name)
 	}
 
 	line[sizeof(line) - 1] = '\0';
+	len = strlen(line);
 
-	while (isspace(line[strlen(line) - 1]))
-		line[strlen(line) - 1] = '\0';
+	while (len > 0 && isspace(line[len - 1]))
+		len--;
 
-	while (isspace(line[0]))
-		strncpy(&line[0], &line[1], sizeof(line));
+	while (len > 0 && isspace(*start)) {
+		start++;
+		len--;
+	}
+	memmove(line, start, len);
+	line[len] = '\0';
 
 	return strdup(line);
 }
-- 
2.11.0

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

* RE: [PATCH mmc-utils] fix GCC7 build by refactoring trimming routines
  2018-12-03 13:19 [PATCH mmc-utils] fix GCC7 build by refactoring trimming routines Wolfram Sang
@ 2018-12-03 14:02 ` Avri Altman
  2018-12-03 14:24   ` Wolfram Sang
  0 siblings, 1 reply; 4+ messages in thread
From: Avri Altman @ 2018-12-03 14:02 UTC (permalink / raw)
  To: Wolfram Sang, linux-mmc; +Cc: linux-renesas-soc, Chris Ball

Hi,

> 
> I got a compile error with GCC7. When trimming white spaces from strings
> lsmmc uses strncpy with overlapping memory areas. This is not allowed.
> In addition, the implementation was not efficient with calling strlen
> and strncpy once per iteration. Refactor the code to be valid and more
> effective.
> 
> Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
> ---
>  lsmmc.c | 16 +++++++++++-----
>  1 file changed, 11 insertions(+), 5 deletions(-)
> 
> diff --git a/lsmmc.c b/lsmmc.c
> index c4faa00..9737b37 100644
> --- a/lsmmc.c
> +++ b/lsmmc.c
> @@ -316,8 +316,9 @@ int parse_ids(struct config *config)
>  /* MMC/SD file parsing functions */
>  char *read_file(char *name)
>  {

See Uwe's note - https://www.spinics.net/lists/linux-mmc/msg51882.html

Cheers,
Avri

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

* Re: [PATCH mmc-utils] fix GCC7 build by refactoring trimming routines
  2018-12-03 14:02 ` Avri Altman
@ 2018-12-03 14:24   ` Wolfram Sang
  2018-12-08  5:56     ` Chris Ball
  0 siblings, 1 reply; 4+ messages in thread
From: Wolfram Sang @ 2018-12-03 14:24 UTC (permalink / raw)
  To: Avri Altman, Chris Ball; +Cc: Wolfram Sang, linux-mmc, linux-renesas-soc

[-- Attachment #1: Type: text/plain, Size: 1031 bytes --]

Hi Avri, hi Chris,

> > I got a compile error with GCC7. When trimming white spaces from strings
> > lsmmc uses strncpy with overlapping memory areas. This is not allowed.
> > In addition, the implementation was not efficient with calling strlen
> > and strncpy once per iteration. Refactor the code to be valid and more
> > effective.
> > 
> > Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
> > ---
> >  lsmmc.c | 16 +++++++++++-----
> >  1 file changed, 11 insertions(+), 5 deletions(-)
> > 
> > diff --git a/lsmmc.c b/lsmmc.c
> > index c4faa00..9737b37 100644
> > --- a/lsmmc.c
> > +++ b/lsmmc.c
> > @@ -316,8 +316,9 @@ int parse_ids(struct config *config)
> >  /* MMC/SD file parsing functions */
> >  char *read_file(char *name)
> >  {
> 
> See Uwe's note - https://www.spinics.net/lists/linux-mmc/msg51882.html

Thanks for the pointer. Chris, how do you feel with maintaining
mmc-utils? Would you maybe be willing to share maintenance with someone?

Happy hacking,

   Wolfram


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [PATCH mmc-utils] fix GCC7 build by refactoring trimming routines
  2018-12-03 14:24   ` Wolfram Sang
@ 2018-12-08  5:56     ` Chris Ball
  0 siblings, 0 replies; 4+ messages in thread
From: Chris Ball @ 2018-12-08  5:56 UTC (permalink / raw)
  To: Wolfram Sang
  Cc: Avri Altman, Wolfram Sang, linux-mmc, linux-renesas-soc,
	Hans-Christian Noren Egtvedt, Clément Péron,
	Michael Heimpold, Uwe Kleine-König

Hi everyone, I'm very sorry for the delay in responding to these patches
and the duplicated effort it caused.

On Mon, Dec 03, 2018 at 03:24:42PM +0100, Wolfram Sang wrote:
> Hi Avri, hi Chris,
> 
> > > I got a compile error with GCC7. When trimming white spaces from strings
> > > lsmmc uses strncpy with overlapping memory areas. This is not allowed.
> > > In addition, the implementation was not efficient with calling strlen
> > > and strncpy once per iteration. Refactor the code to be valid and more
> > > effective.
> > > 
> > > Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>

Thanks, I've applied this fix of Wolfram's to mmc-utils master.

> Thanks for the pointer. Chris, how do you feel with maintaining
> mmc-utils? Would you maybe be willing to share maintenance with someone?

Yep, that'd make sense.

-- 
Chris Ball   <http://printf.net/>

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

end of thread, other threads:[~2018-12-08  6:01 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-12-03 13:19 [PATCH mmc-utils] fix GCC7 build by refactoring trimming routines Wolfram Sang
2018-12-03 14:02 ` Avri Altman
2018-12-03 14:24   ` Wolfram Sang
2018-12-08  5:56     ` Chris Ball

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).