* [U-Boot] [PATCH] tools: env: Use getline rather than fgets when reading config/script
@ 2018-06-07 12:20 Alex Kiernan
2018-06-07 12:26 ` Lukasz Majewski
2018-06-13 15:42 ` [U-Boot] " Tom Rini
0 siblings, 2 replies; 3+ messages in thread
From: Alex Kiernan @ 2018-06-07 12:20 UTC (permalink / raw)
To: u-boot
When reading the config file, or a script file, use getline rather than
fgets so line lengths aren't limited by the size of a compiled in buffer
(128 characters for config, 1024 for scripts).
Rename 'dump' to 'line' so it's clear we're working with a line of text.
Signed-off-by: Alex Kiernan <alex.kiernan@gmail.com>
---
tools/env/fw_env.c | 40 +++++++++++++++++++++-------------------
1 file changed, 21 insertions(+), 19 deletions(-)
diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c
index 4b2caf6960..0b22345a91 100644
--- a/tools/env/fw_env.c
+++ b/tools/env/fw_env.c
@@ -737,7 +737,8 @@ int fw_env_set(int argc, char *argv[], struct env_opts *opts)
int fw_parse_script(char *fname, struct env_opts *opts)
{
FILE *fp;
- char dump[1024]; /* Maximum line length in the file */
+ char *line = NULL;
+ size_t linesize = 0;
char *name;
char *val;
int lineno = 0;
@@ -763,36 +764,34 @@ int fw_parse_script(char *fname, struct env_opts *opts)
}
}
- while (fgets(dump, sizeof(dump), fp)) {
+ while ((len = getline(&line, &linesize, fp)) != -1) {
lineno++;
- len = strlen(dump);
/*
- * Read a whole line from the file. If the line is too long
- * or is not terminated, reports an error and exit.
+ * Read a whole line from the file. If the line is not
+ * terminated, reports an error and exit.
*/
- if (dump[len - 1] != '\n') {
+ if (line[len - 1] != '\n') {
fprintf(stderr,
- "Line %d not corrected terminated or too long\n",
+ "Line %d not correctly terminated\n",
lineno);
ret = -1;
break;
}
/* Drop ending line feed / carriage return */
- dump[--len] = '\0';
- if (len && dump[len - 1] == '\r')
- dump[--len] = '\0';
+ line[--len] = '\0';
+ if (len && line[len - 1] == '\r')
+ line[--len] = '\0';
/* Skip comment or empty lines */
- if (len == 0 || dump[0] == '#')
+ if (len == 0 || line[0] == '#')
continue;
/*
- * Search for variable's name,
- * remove leading whitespaces
+ * Search for variable's name remove leading whitespaces
*/
- name = skip_blanks(dump);
+ name = skip_blanks(line);
if (!name)
continue;
@@ -829,6 +828,7 @@ int fw_parse_script(char *fname, struct env_opts *opts)
}
}
+ free(line);
/* Close file if not stdin */
if (strcmp(fname, "-") != 0)
@@ -1760,19 +1760,20 @@ static int get_config(char *fname)
FILE *fp;
int i = 0;
int rc;
- char dump[128];
+ char *line = NULL;
+ size_t linesize = 0;
char *devname;
fp = fopen(fname, "r");
if (fp == NULL)
return -1;
- while (i < 2 && fgets(dump, sizeof(dump), fp)) {
- /* Skip incomplete conversions and comment strings */
- if (dump[0] == '#')
+ while (i < 2 && getline(&line, &linesize, fp) != -1) {
+ /* Skip comment strings */
+ if (line[0] == '#')
continue;
- rc = sscanf(dump, "%ms %lli %lx %lx %lx",
+ rc = sscanf(line, "%ms %lli %lx %lx %lx",
&devname,
&DEVOFFSET(i),
&ENVSIZE(i), &DEVESIZE(i), &ENVSECTORS(i));
@@ -1788,6 +1789,7 @@ static int get_config(char *fname)
i++;
}
+ free(line);
fclose(fp);
have_redund_env = i - 1;
--
2.17.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* [U-Boot] [PATCH] tools: env: Use getline rather than fgets when reading config/script
2018-06-07 12:20 [U-Boot] [PATCH] tools: env: Use getline rather than fgets when reading config/script Alex Kiernan
@ 2018-06-07 12:26 ` Lukasz Majewski
2018-06-13 15:42 ` [U-Boot] " Tom Rini
1 sibling, 0 replies; 3+ messages in thread
From: Lukasz Majewski @ 2018-06-07 12:26 UTC (permalink / raw)
To: u-boot
On Thu, 7 Jun 2018 12:20:05 +0000
Alex Kiernan <alex.kiernan@gmail.com> wrote:
> When reading the config file, or a script file, use getline rather
> than fgets so line lengths aren't limited by the size of a compiled
> in buffer (128 characters for config, 1024 for scripts).
>
> Rename 'dump' to 'line' so it's clear we're working with a line of
> text.
Reviwed-by: Lukasz Majewski <lukma@denx.de>
Best regards,
Lukasz Majewski
--
DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20180607/c3dfd109/attachment.sig>
^ permalink raw reply [flat|nested] 3+ messages in thread
* [U-Boot] tools: env: Use getline rather than fgets when reading config/script
2018-06-07 12:20 [U-Boot] [PATCH] tools: env: Use getline rather than fgets when reading config/script Alex Kiernan
2018-06-07 12:26 ` Lukasz Majewski
@ 2018-06-13 15:42 ` Tom Rini
1 sibling, 0 replies; 3+ messages in thread
From: Tom Rini @ 2018-06-13 15:42 UTC (permalink / raw)
To: u-boot
On Thu, Jun 07, 2018 at 12:20:05PM +0000, Alex Kiernan wrote:
> When reading the config file, or a script file, use getline rather than
> fgets so line lengths aren't limited by the size of a compiled in buffer
> (128 characters for config, 1024 for scripts).
>
> Rename 'dump' to 'line' so it's clear we're working with a line of text.
>
> Signed-off-by: Alex Kiernan <alex.kiernan@gmail.com>
Applied to u-boot/master, thanks!
--
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20180613/90ed65e5/attachment.sig>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2018-06-13 15:42 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-06-07 12:20 [U-Boot] [PATCH] tools: env: Use getline rather than fgets when reading config/script Alex Kiernan
2018-06-07 12:26 ` Lukasz Majewski
2018-06-13 15:42 ` [U-Boot] " Tom Rini
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox