Skip to content
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

lib/gis: fix potential buffer overflow issues #5216

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
2 changes: 1 addition & 1 deletion lib/gis/color_str.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ int G_str_to_color(const char *str, int *red, int *grn, int *blu)
int num_names = G_num_standard_color_names();
int i;

strcpy(buf, str);
G_strlcpy(buf, str, sizeof(buf));
G_chop(buf);

G_debug(3, "G_str_to_color(): str = '%s'", buf);
Expand Down
2 changes: 1 addition & 1 deletion lib/gis/commas.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ int G_insert_commas(char *buf)

while (*buf == ' ')
buf++;
strcpy(number, buf);
G_strlcpy(number, buf, sizeof(number));
for (len = 0; number[len]; len++)
if (number[len] == '.')
break;
Expand Down
2 changes: 1 addition & 1 deletion lib/gis/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ static FILE *open_env(const char *mode, int loc)
if (!st->gisrc) {
return NULL;
}
strcpy(buf, st->gisrc);
G_strlcpy(buf, st->gisrc, sizeof(buf));
}
else if (loc == G_VAR_MAPSET) {
/* Warning: G_VAR_GISRC must be previously read -> */
Expand Down
6 changes: 3 additions & 3 deletions lib/gis/file_name.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ char *G_file_name_misc(char *path, const char *dir, const char *element,
char *G_file_name_tmp(char *path, const char *element, const char *name,
const char *mapset)
{
const char *env, *tmp_path;
const char *env;
char tmp_path[GPATH_MAX] = {0};

tmp_path = NULL;
env = getenv("GRASS_VECTOR_TMPDIR_MAPSET");
if (env && strcmp(env, "0") == 0) {
tmp_path = getenv("TMPDIR");
snprintf(tmp_path, GPATH_MAX, "%s", getenv("TMPDIR"));
}

return file_name(path, NULL, element, name, mapset, tmp_path);
Expand Down
8 changes: 4 additions & 4 deletions lib/gis/get_ellipse.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ int G_read_ellipsoid_table(int fatal)
err++;
sprintf(buf, " %d", line);
if (*badlines)
strcat(badlines, ",");
strcat(badlines, buf);
G_strlcat(badlines, ",", sizeof(badlines));
G_strlcat(badlines, buf, sizeof(badlines));
continue;
}

Expand All @@ -303,8 +303,8 @@ int G_read_ellipsoid_table(int fatal)
err++;
sprintf(buf, " %d", line);
if (*badlines)
strcat(badlines, ",");
strcat(badlines, buf);
G_strlcat(badlines, ",", sizeof(badlines));
G_strlcat(badlines, buf, sizeof(badlines));
continue;
}
}
Expand Down
8 changes: 6 additions & 2 deletions lib/gis/get_window.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,12 @@ void G_get_window(struct Cell_head *window)
else {
char *wind = getenv("WIND_OVERRIDE");

if (wind)
G_get_element_window(&st->dbwindow, "windows", wind, G_mapset());
if (wind) {
char wind_env[GNAME_MAX] = {0};
snprintf(wind_env, GNAME_MAX, "%s", wind);
G_get_element_window(&st->dbwindow, "windows", wind_env,
G_mapset());
}
else
G_get_element_window(&st->dbwindow, "", "WIND", G_mapset());
}
Expand Down
3 changes: 2 additions & 1 deletion lib/gis/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,8 @@ int module_gui_wx(void)
if (!st->pgm_path)
G_fatal_error(_("Unable to determine program name"));

sprintf(script, "%s/gui/wxpython/gui_core/forms.py", getenv("GISBASE"));
snprintf(script, GPATH_MAX, "%s/gui/wxpython/gui_core/forms.py",
getenv("GISBASE"));
if (access(script, F_OK) != -1)
G_spawn(getenv("GRASS_PYTHON"), getenv("GRASS_PYTHON"), script,
G_recreate_command_original_path(), NULL);
Expand Down
28 changes: 14 additions & 14 deletions lib/gis/parser_help.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,40 +145,40 @@ static void usage(FILE *fp, int markers)
if (n > maxlen)
maxlen = n;

strcpy(item, " ");
G_strlcpy(item, " ", sizeof(item));
if (!opt->required)
strcat(item, "[");
strcat(item, opt->key);
strcat(item, "=");
strcat(item, key_desc);
G_strlcat(item, "[", sizeof(item));
G_strlcat(item, opt->key, sizeof(item));
G_strlcat(item, "=", sizeof(item));
G_strlcat(item, key_desc, sizeof(item));
if (opt->multiple) {
strcat(item, "[,");
strcat(item, key_desc);
strcat(item, ",...]");
G_strlcat(item, "[,", sizeof(item));
G_strlcat(item, key_desc, sizeof(item));
G_strlcat(item, ",...]", sizeof(item));
}
if (!opt->required)
strcat(item, "]");
G_strlcat(item, "]", sizeof(item));

len = show(fp, item, len);

opt = opt->next_opt;
}
}
if (new_prompt) {
strcpy(item, " [--overwrite]");
G_strlcpy(item, " [--overwrite]", sizeof(item));
len = show(fp, item, len);
}

strcpy(item, " [--help]");
G_strlcpy(item, " [--help]", sizeof(item));
len = show(fp, item, len);

strcpy(item, " [--verbose]");
G_strlcpy(item, " [--verbose]", sizeof(item));
len = show(fp, item, len);

strcpy(item, " [--quiet]");
G_strlcpy(item, " [--quiet]", sizeof(item));
len = show(fp, item, len);

strcpy(item, " [--ui]");
G_strlcpy(item, " [--ui]", sizeof(item));
len = show(fp, item, len);

fprintf(fp, "\n");
Expand Down
Loading