Message ID | 20200712124927.25240-1-toertel@gmail.com |
---|---|
State | Accepted |
Headers | show |
Series | [v2] swupdate-progress: Terminate bar array string | expand |
On 12.07.20 14:49, Mark Jonas wrote: > The bar array contains the progress bar. Its length is 60 characters > but space for a terminating \0 was forgotten. This causes problems when > feeding bar to fprintf(). > > By introducing the constant bar_len there is now a single place to > change in case a different progress bar length is required. > > The original code only used 59 characters for printing the progress bar > but the fprintf() call reserved a width of 60 characters. The code now > uses 60 visible characters for the progress bar. > > Suggested-by: Tomas Frydrych <tomas@tomtain.com> > Signed-off-by: Mark Jonas <toertel@gmail.com> > --- > Changes in v2: > - Width of bar now a precision parameter for %s > - More explanation of motivation in commit message > > tools/swupdate-progress.c | 17 ++++++++++------- > 1 file changed, 10 insertions(+), 7 deletions(-) > > diff --git a/tools/swupdate-progress.c b/tools/swupdate-progress.c > index 5a2c888..3c1958a 100644 > --- a/tools/swupdate-progress.c > +++ b/tools/swupdate-progress.c > @@ -179,7 +179,8 @@ int main(int argc, char **argv) > int psplash_ok = 0; > unsigned int curstep = 0; > unsigned int percent = 0; > - char bar[60]; > + const int bar_len = 60; > + char bar[bar_len+1]; > unsigned int filled_len; > int opt_c = 0; > int opt_w = 0; > @@ -222,7 +223,7 @@ int main(int argc, char **argv) > break; > } > } > - > + > rundir = getenv("PSPLASH_FIFO_DIR"); > if (!rundir) > rundir = "/run"; > @@ -292,14 +293,16 @@ int main(int argc, char **argv) > if ((msg.cur_step != curstep) && (curstep != 0)) > fprintf(stdout, "\n"); > > - filled_len = sizeof(bar) * msg.cur_percent / 100; > - if (filled_len > sizeof(bar) - 1) > - filled_len = sizeof(bar) - 1; > + filled_len = bar_len * msg.cur_percent / 100; > + if (filled_len > bar_len) > + filled_len = bar_len; > > memset(bar,'=', filled_len); > - memset(&bar[filled_len], '-', sizeof(bar) - filled_len - 1); > + memset(&bar[filled_len], '-', bar_len - filled_len); > + bar[bar_len] = '\0'; > > - fprintf(stdout, "[ %.60s ] %d of %d %d%% (%s)\r", > + fprintf(stdout, "[ %.*s ] %d of %d %d%% (%s)\r", > + bar_len, > bar, > msg.cur_step, msg.nsteps, msg.cur_percent, > msg.cur_image); > -- > 2.17.1 > Applied to -master, thanks ! Best regards, Stefano Babic
diff --git a/tools/swupdate-progress.c b/tools/swupdate-progress.c index 5a2c888..3c1958a 100644 --- a/tools/swupdate-progress.c +++ b/tools/swupdate-progress.c @@ -179,7 +179,8 @@ int main(int argc, char **argv) int psplash_ok = 0; unsigned int curstep = 0; unsigned int percent = 0; - char bar[60]; + const int bar_len = 60; + char bar[bar_len+1]; unsigned int filled_len; int opt_c = 0; int opt_w = 0; @@ -222,7 +223,7 @@ int main(int argc, char **argv) break; } } - + rundir = getenv("PSPLASH_FIFO_DIR"); if (!rundir) rundir = "/run"; @@ -292,14 +293,16 @@ int main(int argc, char **argv) if ((msg.cur_step != curstep) && (curstep != 0)) fprintf(stdout, "\n"); - filled_len = sizeof(bar) * msg.cur_percent / 100; - if (filled_len > sizeof(bar) - 1) - filled_len = sizeof(bar) - 1; + filled_len = bar_len * msg.cur_percent / 100; + if (filled_len > bar_len) + filled_len = bar_len; memset(bar,'=', filled_len); - memset(&bar[filled_len], '-', sizeof(bar) - filled_len - 1); + memset(&bar[filled_len], '-', bar_len - filled_len); + bar[bar_len] = '\0'; - fprintf(stdout, "[ %.60s ] %d of %d %d%% (%s)\r", + fprintf(stdout, "[ %.*s ] %d of %d %d%% (%s)\r", + bar_len, bar, msg.cur_step, msg.nsteps, msg.cur_percent, msg.cur_image);
The bar array contains the progress bar. Its length is 60 characters but space for a terminating \0 was forgotten. This causes problems when feeding bar to fprintf(). By introducing the constant bar_len there is now a single place to change in case a different progress bar length is required. The original code only used 59 characters for printing the progress bar but the fprintf() call reserved a width of 60 characters. The code now uses 60 visible characters for the progress bar. Suggested-by: Tomas Frydrych <tomas@tomtain.com> Signed-off-by: Mark Jonas <toertel@gmail.com> --- Changes in v2: - Width of bar now a precision parameter for %s - More explanation of motivation in commit message tools/swupdate-progress.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) -- 2.17.1