Skip to content

Commit 5e998d3

Browse files
authored
CQ: replace 'sprintf()` in lib directories (part 1) (#5572)
1 parent b356c7e commit 5e998d3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+198
-169
lines changed

lib/cairodriver/text.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,10 @@ static void font_list_fc(char ***list, int *count, int verbose)
238238
FcPatternGetString(pat, FC_STYLE, 0, &style);
239239

240240
if (verbose)
241-
sprintf(buf, "%s:%s|%s:%s|%d|%s|%d|%s|", family, style, family,
242-
style, GFONT_DRIVER, "", 0, "utf-8");
241+
snprintf(buf, sizeof(buf), "%s:%s|%s:%s|%d|%s|%d|%s|", family,
242+
style, family, style, GFONT_DRIVER, "", 0, "utf-8");
243243
else
244-
sprintf(buf, "%s:%s", family, style);
244+
snprintf(buf, sizeof(buf), "%s:%s", family, style);
245245

246246
fonts[num_fonts++] = G_store(buf);
247247
}
@@ -304,8 +304,8 @@ static void font_list_toy(char ***list, int *count, int verbose)
304304
char buf[256];
305305

306306
if (verbose)
307-
sprintf(buf, "%s|%s|%d|%s|%d|%s|", toy_fonts[i], toy_fonts[i],
308-
GFONT_DRIVER, "", 0, "utf-8");
307+
snprintf(buf, sizeof(buf), "%s|%s|%d|%s|%d|%s|", toy_fonts[i],
308+
toy_fonts[i], GFONT_DRIVER, "", 0, "utf-8");
309309
else
310310
strcpy(buf, toy_fonts[i]);
311311
fonts[num_fonts++] = G_store(buf);

lib/cairodriver/write_xid.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ void cairo_write_xid(void)
1212
if (!fp)
1313
G_fatal_error(_("Unable to open output file <%s>"), ca.file_name);
1414

15-
sprintf(buf, "0x%08lx\n", (unsigned long)ca.win);
15+
snprintf(buf, sizeof(buf), "0x%08lx\n", (unsigned long)ca.win);
1616

1717
if (fputs(buf, fp) < 0)
1818
G_fatal_error(_("Unable to write output file <%s>"), ca.file_name);

lib/cluster/c_begin.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ int I_cluster_begin(struct Cluster *C, int nbands)
5454
/* prepare the signatures for nbands */
5555

5656
I_init_signatures(&C->S, nbands);
57-
sprintf(C->S.title, _("produced by i.cluster"));
57+
snprintf(C->S.title, sizeof(C->S.title), _("produced by i.cluster"));
5858

5959
/* allocate the data (points) arrays */
6060
C->points = (DCELL **)malloc(C->nbands * sizeof(DCELL *));

lib/datetime/format.c

+14-13
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ int datetime_format(const DateTime *dt, char *buf)
4141

4242
if (datetime_is_absolute(dt)) {
4343
if (datetime_get_day(dt, &n) == 0) {
44-
sprintf(temp, "%d", n);
44+
snprintf(temp, sizeof(temp), "%d", n);
4545
strcat(buf, temp);
4646
}
4747

@@ -54,7 +54,7 @@ int datetime_format(const DateTime *dt, char *buf)
5454
if (datetime_get_year(dt, &n) == 0) {
5555
if (*buf)
5656
strcat(buf, " ");
57-
sprintf(temp, "%d", n);
57+
snprintf(temp, sizeof(temp), "%d", n);
5858
strcat(buf, temp);
5959
if (datetime_is_negative(dt))
6060
strcat(buf, " bc");
@@ -63,14 +63,14 @@ int datetime_format(const DateTime *dt, char *buf)
6363
if (datetime_get_hour(dt, &n) == 0) {
6464
if (*buf)
6565
strcat(buf, " ");
66-
sprintf(temp, "%02d", n);
66+
snprintf(temp, sizeof(temp), "%02d", n);
6767
strcat(buf, temp);
6868
}
6969

7070
if (datetime_get_minute(dt, &n) == 0) {
7171
if (*buf)
7272
strcat(buf, ":");
73-
sprintf(temp, "%02d", n);
73+
snprintf(temp, sizeof(temp), "%02d", n);
7474
strcat(buf, temp);
7575
}
7676

@@ -79,7 +79,7 @@ int datetime_format(const DateTime *dt, char *buf)
7979
strcat(buf, ":");
8080
if (datetime_get_fracsec(dt, &n) != 0)
8181
n = 0;
82-
sprintf(temp, "%02.*f", n, sec);
82+
snprintf(temp, sizeof(temp), "%02.*f", n, sec);
8383
strcat(buf, temp);
8484
}
8585

@@ -89,7 +89,8 @@ int datetime_format(const DateTime *dt, char *buf)
8989
if (*buf)
9090
strcat(buf, " ");
9191
datetime_decompose_timezone(n, &hour, &minute);
92-
sprintf(temp, "%s%02d%02d", n < 0 ? "-" : "+", hour, minute);
92+
snprintf(temp, sizeof(temp), "%s%02d%02d", n < 0 ? "-" : "+", hour,
93+
minute);
9394
strcat(buf, temp);
9495
}
9596
}
@@ -101,35 +102,35 @@ int datetime_format(const DateTime *dt, char *buf)
101102
if (datetime_get_year(dt, &n) == 0) {
102103
if (*buf)
103104
strcat(buf, " ");
104-
sprintf(temp, "%d year%s", n, n == 1 ? "" : "s");
105+
snprintf(temp, sizeof(temp), "%d year%s", n, n == 1 ? "" : "s");
105106
strcat(buf, temp);
106107
}
107108

108109
if (datetime_get_month(dt, &n) == 0) {
109110
if (*buf)
110111
strcat(buf, " ");
111-
sprintf(temp, "%d month%s", n, n == 1 ? "" : "s");
112+
snprintf(temp, sizeof(temp), "%d month%s", n, n == 1 ? "" : "s");
112113
strcat(buf, temp);
113114
}
114115

115116
if (datetime_get_day(dt, &n) == 0) {
116117
if (*buf)
117118
strcat(buf, " ");
118-
sprintf(temp, "%d day%s", n, n == 1 ? "" : "s");
119+
snprintf(temp, sizeof(temp), "%d day%s", n, n == 1 ? "" : "s");
119120
strcat(buf, temp);
120121
}
121122

122123
if (datetime_get_hour(dt, &n) == 0) {
123124
if (*buf)
124125
strcat(buf, " ");
125-
sprintf(temp, "%d hour%s", n, n == 1 ? "" : "s");
126+
snprintf(temp, sizeof(temp), "%d hour%s", n, n == 1 ? "" : "s");
126127
strcat(buf, temp);
127128
}
128129

129130
if (datetime_get_minute(dt, &n) == 0) {
130131
if (*buf)
131132
strcat(buf, " ");
132-
sprintf(temp, "%d minute%s", n, n == 1 ? "" : "s");
133+
snprintf(temp, sizeof(temp), "%d minute%s", n, n == 1 ? "" : "s");
133134
strcat(buf, temp);
134135
}
135136

@@ -138,8 +139,8 @@ int datetime_format(const DateTime *dt, char *buf)
138139
strcat(buf, " ");
139140
if (datetime_get_fracsec(dt, &n) != 0)
140141
n = 0;
141-
sprintf(temp, "%.*f second%s", n, sec,
142-
(sec == 1.0 && n == 0) ? "" : "s");
142+
snprintf(temp, sizeof(temp), "%.*f second%s", n, sec,
143+
(sec == 1.0 && n == 0) ? "" : "s");
143144
strcat(buf, temp);
144145
}
145146
}

lib/db/dbmi_base/datetime.c

+39-32
Original file line numberDiff line numberDiff line change
@@ -58,91 +58,96 @@ int db_convert_value_datetime_into_string(dbValue *value, int sqltype,
5858
case DB_YEAR:
5959
switch (to) {
6060
case DB_YEAR:
61-
sprintf(buf, "%d", year);
61+
snprintf(buf, sizeof(buf), "%d", year);
6262
break;
6363
case DB_MONTH:
64-
sprintf(buf, "%d%c%02d", year, ds, month);
64+
snprintf(buf, sizeof(buf), "%d%c%02d", year, ds, month);
6565
break;
6666
case DB_DAY:
67-
sprintf(buf, "%d%c%02d%c%02d", year, ds, month, ds, day);
67+
snprintf(buf, sizeof(buf), "%d%c%02d%c%02d", year, ds, month, ds,
68+
day);
6869
break;
6970
case DB_HOUR:
70-
sprintf(buf, "%d%c%02d%c%02d %02d", year, ds, month, ds, day, hour);
71+
snprintf(buf, sizeof(buf), "%d%c%02d%c%02d %02d", year, ds, month,
72+
ds, day, hour);
7173
break;
7274
case DB_MINUTE:
73-
sprintf(buf, "%d%c%02d%c%02d %02d%c%02d", year, ds, month, ds, day,
74-
hour, ts, minute);
75+
snprintf(buf, sizeof(buf), "%d%c%02d%c%02d %02d%c%02d", year, ds,
76+
month, ds, day, hour, ts, minute);
7577
break;
7678
case DB_SECOND:
7779
case DB_FRACTION:
78-
sprintf(buf, "%d%c%02d%c%02d %02d%c%02d%c%s%.10g", year, ds, month,
79-
ds, day, hour, ts, minute, ts, xs, seconds);
80+
snprintf(buf, sizeof(buf), "%d%c%02d%c%02d %02d%c%02d%c%s%.10g",
81+
year, ds, month, ds, day, hour, ts, minute, ts, xs,
82+
seconds);
8083
break;
8184
}
8285
break;
8386
case DB_MONTH:
8487
switch (to) {
8588
case DB_MONTH:
86-
sprintf(buf, "%d", month);
89+
snprintf(buf, sizeof(buf), "%d", month);
8790
break;
8891
case DB_DAY:
89-
sprintf(buf, "%02d%c%02d", month, ds, day);
92+
snprintf(buf, sizeof(buf), "%02d%c%02d", month, ds, day);
9093
break;
9194
case DB_HOUR:
92-
sprintf(buf, "%02d%c%02d %02d", month, ds, day, hour);
95+
snprintf(buf, sizeof(buf), "%02d%c%02d %02d", month, ds, day, hour);
9396
break;
9497
case DB_MINUTE:
95-
sprintf(buf, "%02d%c%02d %02d%c%02d", month, ds, day, hour, ts,
96-
minute);
98+
snprintf(buf, sizeof(buf), "%02d%c%02d %02d%c%02d", month, ds, day,
99+
hour, ts, minute);
97100
break;
98101
case DB_SECOND:
99102
case DB_FRACTION:
100-
sprintf(buf, "%02d%c%02d %02d%c%02d%c%s%.10g", month, ds, day, hour,
101-
ts, minute, ts, xs, seconds);
103+
snprintf(buf, sizeof(buf), "%02d%c%02d %02d%c%02d%c%s%.10g", month,
104+
ds, day, hour, ts, minute, ts, xs, seconds);
102105
break;
103106
}
104107
break;
105108
case DB_DAY:
106109
switch (to) {
107110
case DB_DAY:
108-
sprintf(buf, "%02d", day);
111+
snprintf(buf, sizeof(buf), "%02d", day);
109112
break;
110113
case DB_HOUR:
111-
sprintf(buf, "%02d %02d", day, hour);
114+
snprintf(buf, sizeof(buf), "%02d %02d", day, hour);
112115
break;
113116
case DB_MINUTE:
114-
sprintf(buf, "%02d %02d%c%02d", day, hour, ts, minute);
117+
snprintf(buf, sizeof(buf), "%02d %02d%c%02d", day, hour, ts,
118+
minute);
115119
break;
116120
case DB_SECOND:
117121
case DB_FRACTION:
118-
sprintf(buf, "%02d %02d%c%02d%c%s%.10g", day, hour, ts, minute, ts,
119-
xs, seconds);
122+
snprintf(buf, sizeof(buf), "%02d %02d%c%02d%c%s%.10g", day, hour,
123+
ts, minute, ts, xs, seconds);
120124
break;
121125
}
122126
break;
123127
case DB_HOUR:
124128
switch (to) {
125129
case DB_HOUR:
126-
sprintf(buf, "%02d", hour);
130+
snprintf(buf, sizeof(buf), "%02d", hour);
127131
break;
128132
case DB_MINUTE:
129-
sprintf(buf, "%02d%c%02d", hour, ts, minute);
133+
snprintf(buf, sizeof(buf), "%02d%c%02d", hour, ts, minute);
130134
break;
131135
case DB_SECOND:
132136
case DB_FRACTION:
133-
sprintf(buf, "%02d%c%02d%c%s%.10g", hour, ts, minute, ts, xs,
134-
seconds);
137+
snprintf(buf, sizeof(buf), "%02d%c%02d%c%s%.10g", hour, ts, minute,
138+
ts, xs, seconds);
135139
break;
136140
}
137141
break;
138142
case DB_MINUTE:
139143
switch (to) {
140144
case DB_MINUTE:
141-
sprintf(buf, "%02d", minute);
145+
snprintf(buf, sizeof(buf), "%02d", minute);
142146
break;
143147
case DB_SECOND:
144148
case DB_FRACTION:
145-
sprintf(buf, "%02d%c%s%.10g", minute, ts, xs, seconds);
149+
snprintf(buf, sizeof(buf), "%02d%c%s%.10g", minute, ts, xs,
150+
seconds);
146151
break;
147152
}
148153
break;
@@ -151,22 +156,24 @@ int db_convert_value_datetime_into_string(dbValue *value, int sqltype,
151156
switch (to) {
152157
case DB_SECOND:
153158
case DB_FRACTION:
154-
sprintf(buf, "%g", seconds);
159+
snprintf(buf, sizeof(buf), "%g", seconds);
155160
break;
156161
}
157162
break;
158163
default:
159164
switch (sqltype) {
160165
case DB_SQL_TYPE_DATE:
161-
sprintf(buf, "%d%c%02d%c%02d", year, ds, month, ds, day);
166+
snprintf(buf, sizeof(buf), "%d%c%02d%c%02d", year, ds, month, ds,
167+
day);
162168
break;
163169
case DB_SQL_TYPE_TIME:
164-
sprintf(buf, "%02d%c%02d%c%s%.10g", hour, ts, minute, ts, xs,
165-
seconds);
170+
snprintf(buf, sizeof(buf), "%02d%c%02d%c%s%.10g", hour, ts, minute,
171+
ts, xs, seconds);
166172
break;
167173
case DB_SQL_TYPE_TIMESTAMP:
168-
sprintf(buf, "%d%c%02d%c%02d %02d%c%02d%c%s%.10g", year, ds, month,
169-
ds, day, hour, ts, minute, ts, xs, seconds);
174+
snprintf(buf, sizeof(buf), "%d%c%02d%c%02d %02d%c%02d%c%s%.10g",
175+
year, ds, month, ds, day, hour, ts, minute, ts, xs,
176+
seconds);
170177
break;
171178
}
172179
}

lib/db/dbmi_base/dbmscap.c

+8-6
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ dbDbmscap *db_read_dbmscap(void)
105105
char comment[1024];
106106
int line;
107107
*/
108-
char *dirpath;
108+
char *dirpath = NULL;
109109
DIR *dir;
110110
struct dirent *ent;
111111

@@ -157,8 +157,9 @@ dbDbmscap *db_read_dbmscap(void)
157157

158158
/* opend db drivers directory */
159159
#ifdef _WIN32
160-
dirpath = G_malloc(strlen("\\driver\\db\\") + strlen(G_gisbase()) + 1);
161-
sprintf(dirpath, "%s\\driver\\db\\", G_gisbase());
160+
size_t len = (strlen("\\driver\\db\\") + strlen(G_gisbase()) + 1);
161+
dirpath = G_malloc(len);
162+
snprintf(dirpath, len, "%s\\driver\\db\\", G_gisbase());
162163
G_convert_dirseps_to_host(dirpath);
163164
#else
164165
G_asprintf(&dirpath, "%s/driver/db/", G_gisbase());
@@ -189,9 +190,10 @@ dbDbmscap *db_read_dbmscap(void)
189190
name = G_str_replace(ent->d_name, ".exe", "");
190191

191192
#ifdef _WIN32
192-
dirpath = G_malloc(strlen("\\driver\\db\\") + strlen(G_gisbase()) +
193-
strlen(ent->d_name) + 1);
194-
sprintf(dirpath, "%s\\driver\\db\\%s", G_gisbase(), ent->d_name);
193+
len = strlen("\\driver\\db\\") + strlen(G_gisbase()) +
194+
strlen(ent->d_name) + 1;
195+
dirpath = G_malloc(len);
196+
snprintf(dirpath, len, "%s\\driver\\db\\%s", G_gisbase(), ent->d_name);
195197
G_convert_dirseps_to_host(dirpath);
196198
#else
197199
G_asprintf(&dirpath, "%s/driver/db/%s", G_gisbase(), ent->d_name);

lib/db/dbmi_base/default_name.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ int db_set_default_connection(void)
110110
connection.databaseName = "$GISDBASE/$LOCATION_NAME/$MAPSET/dbf/";
111111
db_set_connection(&connection);
112112

113-
sprintf(buf, "%s/%s/dbf", G_location_path(), G_mapset());
113+
snprintf(buf, sizeof(buf), "%s/%s/dbf", G_location_path(), G_mapset());
114114
G_make_mapset_object_group("dbf");
115115
}
116116
else if (strcmp(DB_DEFAULT_DRIVER, "sqlite") == 0) {

lib/db/dbmi_base/dirent.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ dbDirent *db_dirent(const char *dirname, int *n)
7777
}
7878
rewinddir(dp);
7979

80-
path = db_malloc(strlen(dirname) + max + 2); /* extra 2 for / and NULL */
80+
size_t path_len = strlen(dirname) + max + 2; // extra 2 for / and NULL
81+
path = db_malloc(path_len);
8182
if (path == NULL) {
8283
closedir(dp);
8384
return (dbDirent *)NULL;
@@ -95,7 +96,7 @@ dbDirent *db_dirent(const char *dirname, int *n)
9596

9697
if (DB_OK != db_set_string(&db_dirent[i].name, entry->d_name))
9798
break;
98-
sprintf(path, "%s/%s", dirname, entry->d_name);
99+
snprintf(path, path_len, "%s/%s", dirname, entry->d_name);
99100
db_dirent[i].perm = get_perm(path);
100101
db_dirent[i].isdir = (db_isdir(path) == DB_OK);
101102
}

0 commit comments

Comments
 (0)