Skip to content

CQ: replace 'sprintf()` in general and imagery directories #5574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions general/g.access/exp_perms.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ char *explain_perms(int group, int other, int will)
if (will)
verb = _("have");

sprintf(buf, _("%s %s %s %saccess to mapset %s"), who,
will ? _("will") : _("now"), verb, read, G_mapset());
snprintf(buf, sizeof(buf), _("%s %s %s %saccess to mapset %s"), who,
will ? _("will") : _("now"), verb, read, G_mapset());
return buf;
}
2 changes: 1 addition & 1 deletion general/g.cairocomp/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static void write_xid(const char *filename, XID xid)
if (!fp)
G_fatal_error(_("Unable to open output file <%s>"), filename);

sprintf(buf, "0x%08lx\n", (unsigned long)xid);
snprintf(buf, sizeof(buf), "0x%08lx\n", (unsigned long)xid);

if (fputs(buf, fp) < 0)
G_fatal_error(_("Unable to write output file <%s>"), filename);
Expand Down
3 changes: 2 additions & 1 deletion general/g.gui/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ int main(int argc, char *argv[])
exit(EXIT_SUCCESS);
}

sprintf(progname, "%s/gui/wxpython/wxgui.py", G_gisbase());
snprintf(progname, sizeof(progname), "%s/gui/wxpython/wxgui.py",
G_gisbase());
if (access(progname, F_OK) == -1)
G_fatal_error(_("Your installation doesn't include GUI, exiting."));

Expand Down
9 changes: 6 additions & 3 deletions general/g.list/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ int main(int argc, char *argv[])
char *pattern;

pattern = (char *)G_malloc(strlen(opt.pattern->answer) + 3);
sprintf(pattern, "{%s}", opt.pattern->answer);
snprintf(pattern, (strlen(opt.pattern->answer) + 3), "{%s}",
opt.pattern->answer);

filter =
G_ls_glob_filter(pattern, 0, (int)flag.ignorecase->answer);
Expand All @@ -198,7 +199,8 @@ int main(int argc, char *argv[])
char *pattern;

pattern = (char *)G_malloc(strlen(opt.exclude->answer) + 3);
sprintf(pattern, "{%s}", opt.exclude->answer);
snprintf(pattern, (strlen(opt.exclude->answer) + 3), "{%s}",
opt.exclude->answer);

exclude =
G_ls_glob_filter(pattern, 1, (int)flag.ignorecase->answer);
Expand Down Expand Up @@ -294,7 +296,8 @@ int main(int argc, char *argv[])
if (flag.full->answer) {
char lister[GPATH_MAX];

sprintf(lister, "%s/etc/lister/%s", G_gisbase(), elem->element[0]);
snprintf(lister, sizeof(lister), "%s/etc/lister/%s", G_gisbase(),
elem->element[0]);

G_debug(3, "lister CMD: %s", lister);

Expand Down
6 changes: 3 additions & 3 deletions general/g.mapset/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ int main(int argc, char *argv[])

G_asprintf(&lock_prog, "%s/etc/lock", G_gisbase());

sprintf(path, "%s/.gislock", mapset_new_path);
snprintf(path, sizeof(path), "%s/.gislock", mapset_new_path);
G_debug(2, "%s", path);

ret = G_spawn(lock_prog, lock_prog, path, gis_lock, NULL);
Expand All @@ -191,7 +191,7 @@ int main(int argc, char *argv[])
}

/* Clean temporary directory */
sprintf(path, "%s/etc/clean_temp", G_gisbase());
snprintf(path, sizeof(path), "%s/etc/clean_temp", G_gisbase());
G_verbose_message(_("Cleaning up temporary files..."));
G_spawn(path, "clean_temp", NULL);

Expand All @@ -201,7 +201,7 @@ int main(int argc, char *argv[])
G_setenv("MAPSET", mapset_new);

/* Remove old lock */
sprintf(path, "%s/.gislock", mapset_old_path);
snprintf(path, sizeof(path), "%s/.gislock", mapset_old_path);
remove(path);

G_free(mapset_old_path);
Expand Down
4 changes: 2 additions & 2 deletions general/g.mapsets/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ int main(int argc, char *argv[])
if (opt.dialog->answer) {
if (opt.mapset->answer)
G_warning(_("Option <%s> ignored"), opt.mapset->key);
sprintf(path_buf, "%s/gui/wxpython/modules/mapsets_picker.py",
G_gisbase());
snprintf(path_buf, sizeof(path_buf),
"%s/gui/wxpython/modules/mapsets_picker.py", G_gisbase());
G_spawn(getenv("GRASS_PYTHON"), "mapsets_picker.py", path_buf, NULL);
exit(EXIT_SUCCESS);
}
Expand Down
3 changes: 2 additions & 1 deletion general/g.mkfontcap/freetype_fonts.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ static void find_fonts(const char *dirpath)
if (cur_entry->d_name[0] == '.')
continue; /* Skip hidden files */

sprintf(filepath, "%s%c%s", dirpath, HOST_DIRSEP, cur_entry->d_name);
snprintf(filepath, sizeof(filepath), "%s%c%s", dirpath, HOST_DIRSEP,
cur_entry->d_name);

if (stat(filepath, &info))
continue; /* File is unreadable */
Expand Down
7 changes: 4 additions & 3 deletions general/g.parser/revoke.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ int reinvoke_script(const struct context *ctx, const char *filename)
for (flag = ctx->first_flag; flag; flag = flag->next_flag) {
char buff[16];

sprintf(buff, "GIS_FLAG_%c=%d", flag->key, flag->answer ? 1 : 0);
snprintf(buff, sizeof(buff), "GIS_FLAG_%c=%d", flag->key,
flag->answer ? 1 : 0);
putenv(G_store(buff));

sprintf(buff, "GIS_FLAG_%c=%d", toupper(flag->key),
flag->answer ? 1 : 0);
snprintf(buff, sizeof(buff), "GIS_FLAG_%c=%d", toupper(flag->key),
flag->answer ? 1 : 0);

G_debug(2, "set %s", buff);
putenv(G_store(buff));
Expand Down
2 changes: 1 addition & 1 deletion general/g.proj/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ int input_epsg(int epsg_num)
ret = GPJ_osr_to_grass(&cellhd, &projinfo, &projunits, hSRS, 0);

/* EPSG code */
sprintf(epsgstr, "%d", epsg_num);
snprintf(epsgstr, sizeof(epsgstr), "%d", epsg_num);
projepsg = G_create_key_value();
G_set_key_value("epsg", epsgstr, projepsg);
/* srid as AUTHORITY:CODE */
Expand Down
10 changes: 5 additions & 5 deletions general/g.proj/list_codes.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void list_codes(char *authname)
authname = "epsg";

init_info = proj_init_info(authname);
sprintf(pathname, "%s", init_info.filename);
snprintf(pathname, sizeof(pathname), "%s", init_info.filename);

if (access(pathname, F_OK) != 0)
G_fatal_error(_("Unable to find init file %s"), authname);
Expand All @@ -118,7 +118,7 @@ void list_codes(char *authname)
grass_proj_share = getenv("GRASS_PROJSHARE");
if (!grass_proj_share)
G_fatal_error(_("Environment variable GRASS_PROJSHARE is not set"));
sprintf(pathname, "%s/%s", grass_proj_share, authname);
snprintf(pathname, sizeof(pathname), "%s/%s", grass_proj_share, authname);
G_convert_dirseps_to_host(pathname);
#endif

Expand Down Expand Up @@ -158,7 +158,7 @@ void list_codes(char *authname)
* definition follows code until next '<' */

if (*buf == '#' && buflen > 2) {
sprintf(name, "%s", buf + 2);
snprintf(name, sizeof(name), "%s", buf + 2);
continue;
}

Expand Down Expand Up @@ -187,7 +187,7 @@ void list_codes(char *authname)

if (j < buflen) {
buf[j] = '\0';
sprintf(code, "%s", buf + bufstart);
snprintf(code, sizeof(code), "%s", buf + bufstart);
}
i = j + 1;
bufstart = i;
Expand All @@ -198,7 +198,7 @@ void list_codes(char *authname)
/* the remaining content of the line could be the name */
bufstart = i + 1;
if (bufstart < buflen) {
sprintf(name, "%s", buf + bufstart);
snprintf(name, sizeof(name), "%s", buf + bufstart);
G_strip(name);
}
i = buflen;
Expand Down
10 changes: 5 additions & 5 deletions general/g.region/printwindow.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,19 @@ void print_window(struct Cell_head *window, int print_flag, int flat_flag,
/* flag.dist_res */
if (print_flag & PRINT_METERS) {
d_ewres = ((ew_dist1 + ew_dist2) / 2) / window->cols;
sprintf(ewres, "%.8f", d_ewres);
snprintf(ewres, sizeof(ewres), "%.8f", d_ewres);
G_trim_decimal(ewres);
d_ewres3 = ((ew_dist1 + ew_dist2) / 2) / window->cols3;
sprintf(ewres3, "%.8f", d_ewres3);
snprintf(ewres3, sizeof(ewres3), "%.8f", d_ewres3);
G_trim_decimal(ewres3);
d_nsres = ((ns_dist1 + ns_dist2) / 2) / window->rows;
sprintf(nsres, "%.8f", d_nsres);
snprintf(nsres, sizeof(nsres), "%.8f", d_nsres);
G_trim_decimal(nsres);
d_nsres3 = ((ns_dist1 + ns_dist2) / 2) / window->rows3;
sprintf(nsres3, "%.8f", d_nsres3);
snprintf(nsres3, sizeof(nsres3), "%.8f", d_nsres3);
G_trim_decimal(nsres3);
d_tbres = (window->top - window->bottom) / window->depths;
sprintf(tbres, "%.8f", d_tbres);
snprintf(tbres, sizeof(tbres), "%.8f", d_tbres);
G_trim_decimal(tbres);
}

Expand Down
4 changes: 2 additions & 2 deletions general/g.remove/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ int main(int argc, char *argv[])
char *buf;

buf = (char *)G_malloc(strlen(pattern) + 3);
sprintf(buf, "{%s}", pattern);
snprintf(buf, (strlen(pattern) + 3), "{%s}", pattern);

filter = G_ls_glob_filter(buf, 0, (int)flag.ignorecase->answer);
}
Expand All @@ -188,7 +188,7 @@ int main(int argc, char *argv[])
char *buf;

buf = (char *)G_malloc(strlen(exclude) + 3);
sprintf(buf, "{%s}", exclude);
snprintf(buf, (strlen(exclude) + 3), "{%s}", exclude);

exclude_filter =
G_ls_glob_filter(buf, 1, (int)flag.ignorecase->answer);
Expand Down
6 changes: 3 additions & 3 deletions general/g.rename/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,13 @@ void update_reclass_maps(const char *name, const char *mapset)
off_t ptr, l;

G_message(" %s", *rmaps);
sprintf(buf3, "%s", *rmaps);
snprintf(buf3, sizeof(buf3), "%s", *rmaps);
if ((str = strchr(buf3, '@'))) {
*str = 0;
sprintf(buf2, "%s", str + 1);
snprintf(buf2, sizeof(buf2), "%s", str + 1);
}
else {
sprintf(buf2, "%s", mapset);
snprintf(buf2, sizeof(buf2), "%s", mapset);
}
G_file_name(buf1, "cellhd", buf3, buf2);

Expand Down
2 changes: 1 addition & 1 deletion general/g.setproj/get_deg.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ int get_deg(char *strng, int ll_swt)
break;
}
} /* end of switch */
sprintf(strng, "%.10f", degrees);
snprintf(strng, 200, "%.10f", degrees);
return (1);
}
6 changes: 3 additions & 3 deletions general/g.setproj/get_num.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ int get_double(const struct proj_parm *parm, const struct proj_desc *desc,
{
char answer[200];

sprintf(answer, "Enter %s ", desc->desc);
snprintf(answer, sizeof(answer), "Enter %s ", desc->desc);
*val = prompt_num_double(answer, parm->deflt, 1);
return 1;
}
Expand All @@ -18,7 +18,7 @@ int get_int(const struct proj_parm *parm, const struct proj_desc *desc,
{
char answer[200];

sprintf(answer, "Enter %s ", desc->desc);
snprintf(answer, sizeof(answer), "Enter %s ", desc->desc);
*val = prompt_num_int(answer, (int)parm->deflt, 1);
return 1;
}
Expand All @@ -34,7 +34,7 @@ int get_zone(void)
first_time = 0;
else
fprintf(stdout, "Invalid zone! Try Again:\n");
sprintf(answer, "Enter Zone");
snprintf(answer, sizeof(answer), "Enter Zone");
zone = prompt_num_int(answer, 0, 0);
}
return (1);
Expand Down
28 changes: 15 additions & 13 deletions general/g.setproj/get_stp.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ void get_stp_proj(char string[])
leave(SP_NOCHANGE);
}
else if (strcmp(answer, "27") == 0) {
sprintf(buff, STP1927PARAMS);
snprintf(buff, sizeof(buff), STP1927PARAMS);
break;
}
else if (strcmp(answer, "83") == 0) {
sprintf(buff, STP1983PARAMS);
snprintf(buff, sizeof(buff), STP1983PARAMS);
break;
}
else
Expand All @@ -51,10 +51,10 @@ int get_stp_code(int code, char *string, char *paramfile)
int gotit = 0, stp;
FILE *fp;

sprintf(nad27, "%s%s", G_gisbase(), paramfile);
snprintf(nad27, sizeof(nad27), "%s%s", G_gisbase(), paramfile);
fp = fopen(nad27, "r");
if (fp == NULL) {
sprintf(buff, "Can not open NAD27 file %s", nad27);
snprintf(buff, sizeof(buff), "Can not open NAD27 file %s", nad27);
G_fatal_error(buff);
}
while (!gotit) {
Expand All @@ -67,7 +67,7 @@ int get_stp_code(int code, char *string, char *paramfile)
p = strtok(NULL, "\n");
while (*p == ' ')
p++;
sprintf(string, "%s", p);
snprintf(string, sizeof(string), "%s", p);
gotit = 1;
}
}
Expand All @@ -84,7 +84,7 @@ int get_stp_num(void)
int record, icode, reccnt = 0, special_case;
char STabbr[50], COname[50];

sprintf(FIPSfile, "%s/etc/proj/FIPS.code", G_gisbase());
snprintf(FIPSfile, sizeof(FIPSfile), "%s/etc/proj/FIPS.code", G_gisbase());

for (;;) {

Expand Down Expand Up @@ -160,7 +160,7 @@ int ask_fips(FILE *fp, int *s, int *c, int *sc)
fprintf(Tmp_fd1, "%4d -- %s\n", sfips, STabbr);
fprintf(Tmp_fd2, "%d:%s\n", sfips, STabbr);
}
sprintf(STabbr_prev, "%s", STabbr);
snprintf(STabbr_prev, sizeof(STabbr_prev), "%s", STabbr);
}
fclose(Tmp_fd1);
fclose(Tmp_fd2);
Expand Down Expand Up @@ -192,13 +192,14 @@ int ask_fips(FILE *fp, int *s, int *c, int *sc)
pager = "cat";

/* Always print interactive output to stderr */
sprintf(buff, "%s \"%s\" 1>&2", pager,
G_convert_dirseps_to_host(Tmp_file1));
snprintf(buff, sizeof(buff), "%s \"%s\" 1>&2", pager,
G_convert_dirseps_to_host(Tmp_file1));
G_system(buff);
}
else {
a = G_find_key_value(answer, sf_keys);
sprintf(buff, "You have chosen state %s, Correct", a);
snprintf(buff, sizeof(buff), "You have chosen state %s, Correct",
a);
if (a == NULL)
G_warning(_("Invalid State FIPS code"));
else if (G_yes(buff, 1))
Expand Down Expand Up @@ -317,13 +318,14 @@ int ask_fips(FILE *fp, int *s, int *c, int *sc)
pager = "cat";

/* Always print interactive output to stderr */
sprintf(buff, "%s \"%s\" 1>&2", pager,
G_convert_dirseps_to_host(Tmp_file1));
snprintf(buff, sizeof(buff), "%s \"%s\" 1>&2", pager,
G_convert_dirseps_to_host(Tmp_file1));
G_system(buff);
}
else {
b = G_find_key_value(answer, cf_keys);
sprintf(buff, "You have chosen %s county, correct", b);
snprintf(buff, sizeof(buff), "You have chosen %s county, correct",
b);
if (b == NULL)
G_warning(_("Invalid County FIPS code"));
else if (G_yes(buff, 1))
Expand Down
Loading
Loading