Skip to content

Commit

Permalink
Replace sprintf() with snprintf() in C++ code (#2767)
Browse files Browse the repository at this point in the history
  • Loading branch information
nilason committed Jan 21, 2023
1 parent f76b267 commit 1860180
Show file tree
Hide file tree
Showing 22 changed files with 128 additions and 110 deletions.
3 changes: 2 additions & 1 deletion include/grass/iostream/ami_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,8 @@ template <class T>
char *AMI_STREAM<T>::sprint()
{
static char desc[BUFSIZ + 256];
sprintf(desc, "[AMI_STREAM %s %ld]", path, (long)stream_len());
snprintf(desc, sizeof(desc), "[AMI_STREAM %s %ld]", path,
(long)stream_len());
return desc;
}

Expand Down
23 changes: 12 additions & 11 deletions include/grass/iostream/embuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -414,28 +414,29 @@ em_buffer<T, Key>::em_buffer(const unsigned short i, const unsigned long bs,
assert((level >= 1) && (basesize >= 0));

char str[100];
sprintf(str, "em_buffer: allocate %d AMI_STREAM*, total %ld\n", arity,
(long)(arity * sizeof(AMI_STREAM<T> *)));
snprintf(str, sizeof(str),
"em_buffer: allocate %d AMI_STREAM*, total %ld\n", arity,
(long)(arity * sizeof(AMI_STREAM<T> *)));
MEMORY_LOG(str);
// allocate STREAM* array
data = new AMI_STREAM<T> *[arity];

// allocate deleted array
sprintf(str, "em_buffer: allocate deleted array: %ld\n",
(long)(arity * sizeof(long)));
snprintf(str, sizeof(str), "em_buffer: allocate deleted array: %ld\n",
(long)(arity * sizeof(long)));
MEMORY_LOG(str);
deleted = new long[arity];

// allocate streamsize array
sprintf(str, "em_buffer: allocate streamsize array: %ld\n",
(long)(arity * sizeof(long)));
snprintf(str, sizeof(str), "em_buffer: allocate streamsize array: %ld\n",
(long)(arity * sizeof(long)));
MEMORY_LOG(str);
streamsize = new unsigned long[arity];

#ifdef SAVE_MEMORY
// allocate name array
sprintf(str, "em_buffer: allocate name array: %ld\n",
(long)(arity * sizeof(char *)));
snprintf(str, sizeof(str), "em_buffer: allocate name array: %ld\n",
(long)(arity * sizeof(char *)));
MEMORY_LOG(str);
name = new char *[arity];
assert(name);
Expand Down Expand Up @@ -937,9 +938,9 @@ AMI_err em_buffer<T, Key>::substream_merge(AMI_STREAM<T> **instreams,
AMI_err ami_err;

char str[200];
sprintf(str,
"em_buffer::substream_merge: allocate keys array, total %ldB\n",
(long)((long)arity * sizeof(merge_key<Key>)));
snprintf(str, sizeof(str),
"em_buffer::substream_merge: allocate keys array, total %ldB\n",
(long)((long)arity * sizeof(merge_key<Key>)));
MEMORY_LOG(str);

// keys array is initialized with smallest key from each stream (only
Expand Down
24 changes: 14 additions & 10 deletions include/grass/iostream/empq_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,9 @@ em_pqueue<T, Key>::em_pqueue(long pq_sz, long buf_sz, unsigned short nb_buf,
assert(buff_0);

char str[200];
sprintf(str, "em_pqueue: allocating array of %ld buff pointers\n",
(long)max_nbuf);
snprintf(str, sizeof(str),
"em_pqueue: allocating array of %ld buff pointers\n",
(long)max_nbuf);
MEMORY_LOG(str);

// allocate ext memory buffers array
Expand Down Expand Up @@ -317,8 +318,9 @@ em_pqueue<T, Key>::em_pqueue()

// allocate ext memory buffers array
char str[200];
sprintf(str, "em_pqueue: allocating array of %ld buff pointers\n",
(long)max_nbuf);
snprintf(str, sizeof(str),
"em_pqueue: allocating array of %ld buff pointers\n",
(long)max_nbuf);
MEMORY_LOG(str);
// allocate ext memory buffers array
buff = new em_buffer<T, Key> *[max_nbuf];
Expand Down Expand Up @@ -445,8 +447,9 @@ em_pqueue<T, Key>::em_pqueue(MinMaxHeap<T> *im, AMI_STREAM<T> *amis)

// allocate ext memory buffer array
char str[200];
sprintf(str, "em_pqueue: allocating array of %ld buff pointers\n",
(long)max_nbuf);
snprintf(str, sizeof(str),
"em_pqueue: allocating array of %ld buff pointers\n",
(long)max_nbuf);
MEMORY_LOG(str);
buff = new em_buffer<T, Key> *[max_nbuf];
assert(buff);
Expand Down Expand Up @@ -594,8 +597,9 @@ bool em_pqueue<T, Key>::fillpq()
AMI_err ae;
{
char str[200];
sprintf(str, "em_pqueue::fillpq: allocate array of %hd AMI_STREAMs\n",
crt_buf);
snprintf(str, sizeof(str),
"em_pqueue::fillpq: allocate array of %hd AMI_STREAMs\n",
crt_buf);
MEMORY_LOG(str);
}
// merge pqsize smallest elements from each buffer into a new stream
Expand Down Expand Up @@ -1178,8 +1182,8 @@ void em_pqueue<T, Key>::empty_buff(unsigned short i)
if (!buff[i + 1]) {
// create buff[i+1] as a level-(i+2) buffer
char str[200];
sprintf(str, "em_pqueue::empty_buff( %hd ) allocate new em_buffer\n",
i);
snprintf(str, sizeof(str),
"em_pqueue::empty_buff( %hd ) allocate new em_buffer\n", i);
MEMORY_LOG(str);
buff[i + 1] = new em_buffer<T, Key>(i + 2, bufsize, buf_arity);
}
Expand Down
3 changes: 2 additions & 1 deletion include/grass/iostream/imbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ class im_buffer {
assert(n >= 0);

char str[100];
sprintf(str, "im_buffer: allocate %ld\n", (long)(maxsize * sizeof(T)));
snprintf(str, sizeof(str), "im_buffer: allocate %ld\n",
(long)(maxsize * sizeof(T)));
MEMORY_LOG(str);

data = new T[maxsize];
Expand Down
2 changes: 1 addition & 1 deletion include/grass/iostream/mem_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ template <class T>
char *MEM_STREAM<T>::sprint()
{
static char buf[BUFSIZ];
sprintf(buf, "[MEM_STREAM %d]", stream_len());
snprintf(buf, sizeof(buf), "[MEM_STREAM %d]", stream_len());
return buf;
}

Expand Down
4 changes: 2 additions & 2 deletions include/grass/iostream/minmaxheap.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ class BasicMinMaxHeap {
BasicMinMaxHeap(HeapIndex size) : maxsize(size)
{
char str[100];
sprintf(str, "BasicMinMaxHeap: allocate %ld\n",
(long)((size + 1) * sizeof(T)));
snprintf(str, sizeof(str), "BasicMinMaxHeap: allocate %ld\n",
(long)((size + 1) * sizeof(T)));
// MEMORY_LOG(str);

lastindex = 0;
Expand Down
2 changes: 1 addition & 1 deletion lib/iostream/ami_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ int ami_single_temp_name(const std::string &base, char *tmp_path)
assert(base_dir);
exit(1);
}
sprintf(tmp_path, "%s/%s_XXXXXX", base_dir, base.c_str());
snprintf(tmp_path, GPATH_MAX, "%s/%s_XXXXXX", base_dir, base.c_str());

fd = G_mkstemp(tmp_path, O_RDWR, 0600);

Expand Down
23 changes: 12 additions & 11 deletions lib/iostream/rtimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,25 @@
#include <string.h>
#include <strings.h>

// #include <rtimer.h>
#include <grass/iostream/rtimer.h>

#define BUFMAX 256

char *rt_sprint_safe(char *buf, Rtimer rt)
{
if (rt_w_useconds(rt) == 0) {
sprintf(buf, "[%4.2fu (%.0f%%) %4.2fs (%.0f%%) %4.2f %.1f%%]", 0.0, 0.0,
0.0, 0.0, 0.0, 0.0);
snprintf(buf, BUFMAX, "[%4.2fu (%.0f%%) %4.2fs (%.0f%%) %4.2f %.1f%%]",
0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
}
else {
sprintf(buf, "[%4.2fu (%.0f%%) %4.2fs (%.0f%%) %4.2f %.1f%%]",
rt_u_useconds(rt) / 1000000,
100.0 * rt_u_useconds(rt) / rt_w_useconds(rt),
rt_s_useconds(rt) / 1000000,
100.0 * rt_s_useconds(rt) / rt_w_useconds(rt),
rt_w_useconds(rt) / 1000000,
100.0 * (rt_u_useconds(rt) + rt_s_useconds(rt)) /
rt_w_useconds(rt));
snprintf(buf, BUFMAX, "[%4.2fu (%.0f%%) %4.2fs (%.0f%%) %4.2f %.1f%%]",
rt_u_useconds(rt) / 1000000,
100.0 * rt_u_useconds(rt) / rt_w_useconds(rt),
rt_s_useconds(rt) / 1000000,
100.0 * rt_s_useconds(rt) / rt_w_useconds(rt),
rt_w_useconds(rt) / 1000000,
100.0 * (rt_u_useconds(rt) + rt_s_useconds(rt)) /
rt_w_useconds(rt));
}
return buf;
}
24 changes: 13 additions & 11 deletions raster/r.in.pdal/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -870,8 +870,10 @@ int main(int argc, char *argv[])
/* close raster file & write history */
Rast_close(out_fd);

sprintf(title, "Raw X,Y,Z data binned into a raster grid by cell %s",
method_opt->answer);
snprintf(title, sizeof(title),
"Raw X,Y,Z data binned into a raster grid by cell %s",
method_opt->answer);

Rast_put_cell_title(outmap, title);

Rast_short_history(outmap, "raster", &history);
Expand All @@ -894,17 +896,17 @@ int main(int argc, char *argv[])
G_put_window(&region);

if (infiles.num_items > 1) {
sprintf(buff,
_("Raster map <%s> created."
" " GPOINT_COUNT_FORMAT
" points from %d files found in region."),
outmap, grass_filter.num_passed(), infiles.num_items);
snprintf(buff, BUFFSIZE,
_("Raster map <%s> created."
" " GPOINT_COUNT_FORMAT
" points from %d files found in region."),
outmap, grass_filter.num_passed(), infiles.num_items);
}
else {
sprintf(buff,
_("Raster map <%s> created."
" " GPOINT_COUNT_FORMAT " points found in region."),
outmap, grass_filter.num_passed());
snprintf(buff, BUFFSIZE,
_("Raster map <%s> created."
" " GPOINT_COUNT_FORMAT " points found in region."),
outmap, grass_filter.num_passed());
}

G_done_msg("%s", buff);
Expand Down
8 changes: 4 additions & 4 deletions raster/r.terraflow/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ void *LargeMemory::alloc(size_t leng)
next++;
if (stats) {
char buf[BUFSIZ], buf2[32];
sprintf(buf, "allocated large memory: %s 0x%lX",
formatNumber(buf2, leng), (unsigned long)p);
snprintf(buf, BUFSIZ, "allocated large memory: %s 0x%lX",
formatNumber(buf2, leng), (unsigned long)p);
stats->comment(buf);
}
return p;
Expand All @@ -121,8 +121,8 @@ void LargeMemory::free(void *p)

if (stats) {
char buf[BUFSIZ], buf2[32];
sprintf(buf, "freed large memory: %s 0x%lX", formatNumber(buf2, len[i]),
(unsigned long)p);
snprintf(buf, BUFSIZ, "freed large memory: %s 0x%lX",
formatNumber(buf2, len[i]), (unsigned long)p);
stats->comment(buf);
}

Expand Down
18 changes: 9 additions & 9 deletions raster/r.terraflow/fill.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class printElevation {
char *operator()(const elevation_type &p)
{
static char buf[20];
sprintf(buf, "%.1f", (float)p);
snprintf(buf, sizeof(buf), "%.1f", (float)p);
return buf;
}
};
Expand All @@ -83,13 +83,13 @@ class printDirection {
char *operator()(const direction_type &p)
{
static char buf[20];
sprintf(buf, "%3d", p);
snprintf(buf, sizeof(buf), "%3d", p);
return buf;
}
char *operator()(const waterWindowBaseType &p)
{
static char buf[20];
sprintf(buf, "%3d", p.dir);
snprintf(buf, sizeof(buf), "%3d", p.dir);
return buf;
}
#if (0)
Expand All @@ -108,19 +108,19 @@ class printLabel {
char *operator()(const labelElevType &p)
{
static char buf[8];
sprintf(buf, CCLABEL_FMT, p.getLabel());
snprintf(buf, sizeof(buf), CCLABEL_FMT, p.getLabel());
return buf;
}
char *operator()(const waterGridType &p)
{
static char buf[8];
sprintf(buf, CCLABEL_FMT, p.getLabel());
snprintf(buf, sizeof(buf), CCLABEL_FMT, p.getLabel());
return buf;
}
char *operator()(const waterType &p)
{
static char buf[8];
sprintf(buf, CCLABEL_FMT, p.getLabel());
snprintf(buf, sizeof(buf), CCLABEL_FMT, p.getLabel());
return buf;
}
};
Expand All @@ -130,15 +130,15 @@ class printDepth {
char *operator()(const waterGridType &p)
{
static char buf[3];
sprintf(buf, "%1u", p.depth);
snprintf(buf, sizeof(buf), "%1u", p.depth);
return buf;
}
};

char *verbosedir(const std::string &s)
{
static char buf[BUFSIZ];
sprintf(buf, "dump/%s", s.c_str());
snprintf(buf, BUFSIZ, "dump/%s", s.c_str());
return buf;
}

Expand Down Expand Up @@ -365,7 +365,7 @@ computeFlowDirections(AMI_STREAM<elevation_type> *&elstr,
char path[BUFSIZ];
char *base_dir = getenv(STREAM_TMPDIR);
assert(base_dir);
sprintf(path, "%s/flowStream", base_dir);
snprintf(path, BUFSIZ, "%s/flowStream", base_dir);
flowStream = new AMI_STREAM<waterWindowBaseType>(path);
/*flowStream->persist(PERSIST_PERSISTENT); */
if (stats)
Expand Down
8 changes: 4 additions & 4 deletions raster/r.terraflow/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,13 @@ void record_args(int argc, char **argv)
stats->comment("MFD flow direction");
}

sprintf(buf, "D8CUT=%f", opt->d8cut);
snprintf(buf, BUFSIZ, "D8CUT=%f", opt->d8cut);
stats->comment(buf);

size_t mm_size = (size_t)opt->mem << 20; /* (in bytes) */
char tmp[100];
formatNumber(tmp, mm_size);
sprintf(buf, "Memory size: %s bytes", tmp);
snprintf(buf, BUFSIZ, "Memory size: %s bytes", tmp);
stats->comment(buf);
}

Expand Down Expand Up @@ -488,7 +488,7 @@ int main(int argc, char *argv[])
G_verbose_message(_("Region size is %d x %d"), nrows, ncols);

/* check STREAM path (the place where intermediate STREAMs are placed) */
sprintf(buf, "%s=%s", STREAM_TMPDIR, opt->streamdir);
snprintf(buf, BUFSIZ, "%s=%s", STREAM_TMPDIR, opt->streamdir);
/* don't pass an automatic variable; putenv() isn't guaranteed to make a
* copy */
putenv(G_store(buf));
Expand Down Expand Up @@ -584,7 +584,7 @@ int main(int argc, char *argv[])
AMI_STREAM<waterWindowBaseType> *flowStream;
char path[GPATH_MAX];

sprintf(path, "%s/flowStream", streamdir->answer);
snprintf(path, GPATH_MAX, "%s/flowStream", streamdir->answer);
flowStream = new AMI_STREAM<waterWindowBaseType>(path);
G_verbose_message(_("flowStream opened: len=%lld\n", flowStream->stream_len());
G_verbose_message(_("jumping to flow accumulation computation\n");
Expand Down
2 changes: 1 addition & 1 deletion raster/r.terraflow/nodata.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class nodataType : public ijBaseType {
static char *printLabel(const nodataType &p)
{
static char buf[8];
sprintf(buf, CCLABEL_FMT, p.label);
snprintf(buf, sizeof(buf), CCLABEL_FMT, p.label);
return buf;
}

Expand Down
2 changes: 1 addition & 1 deletion raster/r.terraflow/plateau.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class plateauType : public ijBaseType {
static char *printLabel(const plateauType &p)
{
static char buf[8];
sprintf(buf, CCLABEL_FMT, p.cclabel);
snprintf(buf, sizeof(buf), CCLABEL_FMT, p.cclabel);
return buf;
}

Expand Down

0 comments on commit 1860180

Please sign in to comment.