Skip to content

Commit

Permalink
lib: deprecate G_snprintf() and replace with C99 snprintf() (#4189)
Browse files Browse the repository at this point in the history
* lib: deprecate G_snprintf() and replace with C99 snprintf()

Beginning with UCRT in Visual Studio 2015 and Windows 10, the behaviour of snprintf (and vsnprintf) is now C99 standard conformant.

Thus, the output is now always null-terminated. The justification for a internal fix for that is no longer needed.

* r3.out.bin: fix -Werror=sizeof-pointer-memaccess GCC warning

* cast explicitly

* add missing #include
  • Loading branch information
nilason authored Sep 3, 2024
1 parent 2cbd98d commit 9222822
Show file tree
Hide file tree
Showing 22 changed files with 103 additions and 100 deletions.
4 changes: 2 additions & 2 deletions imagery/i.topo.corr/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ int main(int argc, char *argv[])
}
/* ----- */
dem.fd = Rast_open_old(base->answer, "");
G_snprintf(out.name, GNAME_MAX - 1, "%s.%s", output->answer,
input->answers[i]);
snprintf(out.name, GNAME_MAX - 1, "%s.%s", output->answer,
input->answers[i]);
out.fd = Rast_open_new(out.name, DCELL_TYPE);
out.rast = Rast_allocate_buf(out.type);
band.rast = Rast_allocate_buf(band.type);
Expand Down
11 changes: 6 additions & 5 deletions lib/gis/color_rules.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
(C) 2001-2011 by the GRASS Development Team
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Expand Down Expand Up @@ -267,7 +268,7 @@ struct colorinfo *get_colorinfo(int *nrules)
char **cnames;

/* load color rules */
G_snprintf(path, GPATH_MAX, "%s/etc/colors", G_gisbase());
snprintf(path, GPATH_MAX, "%s/etc/colors", G_gisbase());

*nrules = 0;
cnames = G_ls2(path, nrules);
Expand All @@ -283,8 +284,8 @@ struct colorinfo *get_colorinfo(int *nrules)
colorinfo[i].desc = NULL;

/* open color rule file */
G_snprintf(path, GPATH_MAX, "%s/etc/colors/%s", G_gisbase(),
colorinfo[i].name);
snprintf(path, GPATH_MAX, "%s/etc/colors/%s", G_gisbase(),
colorinfo[i].name);
fp = fopen(path, "r");
if (!fp)
G_fatal_error(_("Unable to open color rule"));
Expand Down Expand Up @@ -338,7 +339,7 @@ struct colorinfo *get_colorinfo(int *nrules)
if (cisperc)
colorinfo[i].type = G_store(_("range: map values"));
else {
G_snprintf(buf, sizeof(buf) - 1, _("range: %g to %g"), rmin, rmax);
snprintf(buf, sizeof(buf) - 1, _("range: %g to %g"), rmin, rmax);
colorinfo[i].type = G_store(buf);
}
}
Expand All @@ -360,7 +361,7 @@ struct colorinfo *get_colorinfo(int *nrules)
qsort(colorinfo, *nrules, sizeof(struct colorinfo), cmp_clrname);

/* load color descriptions */
G_snprintf(path, GPATH_MAX, "%s/etc/colors.desc", G_gisbase());
snprintf(path, GPATH_MAX, "%s/etc/colors.desc", G_gisbase());
fp = fopen(path, "r");
if (!fp)
G_fatal_error(_("Unable to open color descriptions"));
Expand Down
2 changes: 1 addition & 1 deletion lib/gis/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1812,7 +1812,7 @@ const char *get_renamed_option(const char *key)
/* read renamed options from file (renamed_options) */
char path[GPATH_MAX];

G_snprintf(path, GPATH_MAX, "%s/etc/renamed_options", G_gisbase());
snprintf(path, GPATH_MAX, "%s/etc/renamed_options", G_gisbase());
st->renamed_options = G_read_key_value_file(path);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/gis/parser_wps.c
Original file line number Diff line number Diff line change
Expand Up @@ -869,10 +869,10 @@ static void wps_print_literal_input_output(
strcmp(datatype, "float") == 0) {
str = strtok((char *)choices[0], "-");
if (str != NULL) {
G_snprintf(range[0], 24, "%s", str);
snprintf(range[0], 24, "%s", str);
str = strtok(NULL, "-");
if (str != NULL) {
G_snprintf(range[1], 24, "%s", str);
snprintf(range[1], 24, "%s", str);
type = TYPE_RANGE;
}
}
Expand Down
12 changes: 4 additions & 8 deletions lib/gis/snprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@
* \date 2006-2008
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <assert.h>
#include <stdio.h>

#include <grass/gis.h>

/**
Expand All @@ -33,6 +31,8 @@
* discouraged in favour of calculating how long the string will be and
* allocating enough memory!
*
* \deprecated Use C99 standard function snprintf() instead.
*
* \param[in] str input string
* \param[in] size length of string
* \param[in] fmt
Expand All @@ -48,9 +48,5 @@ int G_snprintf(char *str, size_t size, const char *fmt, ...)
count = vsnprintf(str, size, fmt, ap);
va_end(ap);

/* Windows' vsnprintf() doesn't always NUL-terminate the buffer */
if (count >= 0 && (unsigned int)count == size)
str[--count] = '\0';

return count;
}
26 changes: 14 additions & 12 deletions lib/gis/timestamp.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@
* \author Soeren Gebbert, vector timestamp implementation update
*/

#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <grass/gis.h>
#include <grass/vect/dig_defines.h>
#include <grass/glocale.h>
Expand Down Expand Up @@ -425,11 +427,11 @@ int G_has_vector_timestamp(const char *name, const char *layer,
char ele[GNAME_MAX];

if (layer != NULL)
G_snprintf(ele, GNAME_MAX, "%s_%s", GV_TIMESTAMP_ELEMENT, layer);
snprintf(ele, GNAME_MAX, "%s_%s", GV_TIMESTAMP_ELEMENT, layer);
else
G_snprintf(ele, GNAME_MAX, "%s_1", GV_TIMESTAMP_ELEMENT);
snprintf(ele, GNAME_MAX, "%s_1", GV_TIMESTAMP_ELEMENT);

G_snprintf(dir, GPATH_MAX, "%s/%s", GV_DIRECTORY, name);
snprintf(dir, GPATH_MAX, "%s/%s", GV_DIRECTORY, name);
G_file_name(path, dir, ele, mapset);

G_debug(1, "Check for timestamp <%s>", path);
Expand Down Expand Up @@ -466,11 +468,11 @@ int G_read_vector_timestamp(const char *name, const char *layer,
return 0;

if (layer != NULL)
G_snprintf(ele, GNAME_MAX, "%s_%s", GV_TIMESTAMP_ELEMENT, layer);
snprintf(ele, GNAME_MAX, "%s_%s", GV_TIMESTAMP_ELEMENT, layer);
else
G_snprintf(ele, GNAME_MAX, "%s_1", GV_TIMESTAMP_ELEMENT);
snprintf(ele, GNAME_MAX, "%s_1", GV_TIMESTAMP_ELEMENT);

G_snprintf(dir, GPATH_MAX, "%s/%s", GV_DIRECTORY, name);
snprintf(dir, GPATH_MAX, "%s/%s", GV_DIRECTORY, name);

G_debug(1, "Read timestamp <%s/%s>", dir, ele);

Expand Down Expand Up @@ -511,11 +513,11 @@ int G_write_vector_timestamp(const char *name, const char *layer,
char ele[GNAME_MAX];

if (layer != NULL)
G_snprintf(ele, GNAME_MAX, "%s_%s", GV_TIMESTAMP_ELEMENT, layer);
snprintf(ele, GNAME_MAX, "%s_%s", GV_TIMESTAMP_ELEMENT, layer);
else
G_snprintf(ele, GNAME_MAX, "%s_1", GV_TIMESTAMP_ELEMENT);
snprintf(ele, GNAME_MAX, "%s_1", GV_TIMESTAMP_ELEMENT);

G_snprintf(dir, GPATH_MAX, "%s/%s", GV_DIRECTORY, name);
snprintf(dir, GPATH_MAX, "%s/%s", GV_DIRECTORY, name);

G_debug(1, "Write timestamp <%s/%s>", dir, ele);

Expand Down Expand Up @@ -554,11 +556,11 @@ int G_remove_vector_timestamp(const char *name, const char *layer)
char ele[GNAME_MAX];

if (layer)
G_snprintf(ele, GNAME_MAX, "%s_%s", GV_TIMESTAMP_ELEMENT, layer);
snprintf(ele, GNAME_MAX, "%s_%s", GV_TIMESTAMP_ELEMENT, layer);
else
G_snprintf(ele, GNAME_MAX, "%s_1", GV_TIMESTAMP_ELEMENT);
snprintf(ele, GNAME_MAX, "%s_1", GV_TIMESTAMP_ELEMENT);

G_snprintf(dir, GPATH_MAX, "%s/%s", GV_DIRECTORY, name);
snprintf(dir, GPATH_MAX, "%s/%s", GV_DIRECTORY, name);
return G_remove(dir, ele);
}

Expand Down
9 changes: 5 additions & 4 deletions lib/imagery/loc_info.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <grass/imagery.h>
#include <stdio.h>
#include <string.h>
#include <grass/gis.h>

Expand All @@ -10,13 +11,13 @@ char *I_location_info(const char *middle)
char *buf;
int len, buf_len;

G_snprintf(left, 80, "LOCATION: %s", G_location());
G_snprintf(right, 80, "MAPSET: %s", G_mapset());
snprintf(left, 80, "LOCATION: %s", G_location());
snprintf(right, 80, "MAPSET: %s", G_mapset());
len = 79 - strlen(left) - strlen(middle) - strlen(right);
buf_len = len + strlen(left) + strlen(middle) + strlen(right);
buf = (char *)G_calloc(buf_len, sizeof(char));
G_snprintf(buf, buf_len, "%s%*s%s%*s%s", left, len / 2, "", middle, len / 2,
"", right);
snprintf(buf, buf_len, "%s%*s%s%*s%s", left, len / 2, "", middle, len / 2,
"", right);

return buf;
}
5 changes: 3 additions & 2 deletions lib/init/clean_temp.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <grass/config.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <time.h>
Expand Down Expand Up @@ -57,8 +58,8 @@ void clean_dir(const char *pathname, uid_t uid, pid_t pid, time_t now,
(G_strcasecmp(cur_entry->d_name, "..") == 0))
continue; /* Skip dir and parent dir entries */

if ((pathlen = G_snprintf(buf, BUF_MAX, "%s/%s", pathname,
cur_entry->d_name)) >= BUF_MAX)
if ((pathlen = snprintf(buf, BUF_MAX, "%s/%s", pathname,
cur_entry->d_name)) >= BUF_MAX)
G_fatal_error("clean_temp: exceeded maximum pathname length %d, "
"got %d, shouldn't happen",
BUF_MAX, pathlen);
Expand Down
7 changes: 4 additions & 3 deletions lib/temporal/lib/default_name.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
Joel Jones (CERL/UIUC) and Radim Blazek
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <grass/gis.h>
Expand Down Expand Up @@ -40,8 +41,8 @@ char *tgis_get_default_database_name(void)
{
char default_connection[2048];

G_snprintf(default_connection, 2048, "$GISDBASE/$LOCATION_NAME/$MAPSET/%s",
TGISDB_DEFAULT_SQLITE_PATH);
snprintf(default_connection, 2048, "$GISDBASE/$LOCATION_NAME/$MAPSET/%s",
TGISDB_DEFAULT_SQLITE_PATH);

return G_store(default_connection);
}
Expand All @@ -57,7 +58,7 @@ int tgis_set_default_connection(void)
char db_name[2048];
char *tmp = tgis_get_default_database_name();

G_snprintf(db_name, 2048, "%s", tmp);
snprintf(db_name, 2048, "%s", tmp);
G_free(tmp);

if (strcmp(TGISDB_DEFAULT_DRIVER, "sqlite") == 0) {
Expand Down
5 changes: 3 additions & 2 deletions raster/r.colors/edit_colors.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*
***************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
Expand Down Expand Up @@ -470,7 +471,7 @@ int edit_colors(int argc, char **argv, int type, const char *maptype,
/* check if this style is a percentage style */
/* don't bother with native dirsep as not needed for backwards
* compatibility */
G_snprintf(path, GPATH_MAX, "%s/etc/colors/%s", G_gisbase(), style);
snprintf(path, GPATH_MAX, "%s/etc/colors/%s", G_gisbase(), style);
rule_is_percent = check_percent_rule(path);
do_scale = 1;
}
Expand All @@ -490,7 +491,7 @@ int edit_colors(int argc, char **argv, int type, const char *maptype,

/* don't bother with native dirsep as not needed for backwards
* compatibility */
G_snprintf(path, GPATH_MAX, "%s/etc/colors/%s", G_gisbase(), rules);
snprintf(path, GPATH_MAX, "%s/etc/colors/%s", G_gisbase(), rules);

if (!Rast_load_fp_colors(&colors, path, min, max))
G_fatal_error(_("Unable to load rules file <%s>"), rules);
Expand Down
3 changes: 2 additions & 1 deletion raster/r.external/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*
*****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
Expand Down Expand Up @@ -189,7 +190,7 @@ int main(int argc, char *argv[])
if (!cwd)
G_fatal_error(_("Unable to get current working directory"));

G_snprintf(path, GPATH_MAX, "%s%c%s", cwd, HOST_DIRSEP, input);
snprintf(path, GPATH_MAX, "%s%c%s", cwd, HOST_DIRSEP, input);
input = G_store(path);
CPLFree(cwd);
}
Expand Down
6 changes: 3 additions & 3 deletions raster/r.geomorphon/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -528,9 +528,9 @@ int main(int argc, char **argv)
prof_int("format_version_major", 0);
prof_int("format_version_minor", 9);
prof_utc("timestamp", time(NULL));
G_snprintf(buf, sizeof(buf),
"r.geomorphon GRASS GIS %s [%s]",
GRASS_VERSION_STRING, GRASS_HEADERS_VERSION);
snprintf(buf, sizeof(buf),
"r.geomorphon GRASS GIS %s [%s]",
GRASS_VERSION_STRING, GRASS_HEADERS_VERSION);
prof_str("generator", buf);

oneoff_done =
Expand Down
20 changes: 10 additions & 10 deletions raster/r.geomorphon/profile.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static void prof_int_internal(const toktype type, const char *key,
return;
}
token[size].type = type;
G_snprintf(token[size].key, MAX_STR_LEN, "%s", key);
snprintf(token[size].key, MAX_STR_LEN, "%s", key);
token[size].int_val = val;
size++;
}
Expand All @@ -79,7 +79,7 @@ static void prof_dbl_internal(const toktype type, const char *key,
return;
}
token[size].type = type;
G_snprintf(token[size].key, MAX_STR_LEN, "%s", key);
snprintf(token[size].key, MAX_STR_LEN, "%s", key);
token[size].dbl_val = val;
size++;
}
Expand All @@ -101,8 +101,8 @@ void prof_str(const char *key, const char *val)
return;
}
token[size].type = T_STR;
G_snprintf(token[size].key, MAX_STR_LEN, "%s", key);
G_snprintf(token[size].str_val, MAX_STR_LEN, "%s", val);
snprintf(token[size].key, MAX_STR_LEN, "%s", key);
snprintf(token[size].str_val, MAX_STR_LEN, "%s", val);
size++;
}

Expand All @@ -121,7 +121,7 @@ void prof_sso(const char *key)
return;
}
token[size].type = T_SSO;
G_snprintf(token[size].key, MAX_STR_LEN, "%s", key);
snprintf(token[size].key, MAX_STR_LEN, "%s", key);
size++;
}

Expand Down Expand Up @@ -213,7 +213,7 @@ static const char *quote_val(const toktype t, const char *v)

if (t != T_STR)
return v;
G_snprintf(buf, sizeof(buf), "\"%s\"", v);
snprintf(buf, sizeof(buf), "\"%s\"", v);
return buf;
}

Expand All @@ -225,19 +225,19 @@ static const char *format_token_common(const struct token *t)
case T_BLN:
return t->int_val ? "true" : "false";
case T_INT:
G_snprintf(buf, sizeof(buf), "%d", t->int_val);
snprintf(buf, sizeof(buf), "%d", t->int_val);
return buf;
case T_DBL:
if (isnan(t->dbl_val))
return "null";
G_snprintf(buf, sizeof(buf), "%.8f", t->dbl_val);
snprintf(buf, sizeof(buf), "%.8f", t->dbl_val);
return buf;
case T_STR:
return t->str_val;
case T_MTR:
if (isnan(t->dbl_val))
return "null";
G_snprintf(buf, sizeof(buf), "%.2f", t->dbl_val);
snprintf(buf, sizeof(buf), "%.2f", t->dbl_val);
return buf;
default:
return NULL;
Expand Down Expand Up @@ -330,7 +330,7 @@ static unsigned stack_push(const char *s)
overflow = 1;
return 0;
}
G_snprintf(stack[stack_size], MAX_STR_LEN, "%s", s);
snprintf(stack[stack_size], MAX_STR_LEN, "%s", s);
stack_size++;
return 1;
}
Expand Down
Loading

0 comments on commit 9222822

Please sign in to comment.