Skip to content

Commit d2e3e76

Browse files
committed
CCBC-1596: replace unsafe sprintf with snprintf
Change-Id: I26ecf01764cb8870456bef8c7666dfe262a9951d Reviewed-on: https://review.couchbase.org/c/libcouchbase/+/190187 Tested-by: Build Bot <build@couchbase.com> Reviewed-by: Trond Norbye <trond.norbye@couchbase.com>
1 parent c40a0dc commit d2e3e76

28 files changed

Lines changed: 68 additions & 57 deletions

File tree

contrib/cJSON/cJSON.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,15 @@ static char *print_number(cJSON *item)
270270
double d = item->valuedouble;
271271
if (fabs(((double)item->valueint) - d) <= DBL_EPSILON && d <= INT_MAX && d >= INT_MIN) {
272272
str = (char *)cJSON_malloc(21); /* 2^64+1 can be represented in 21 chars. */
273-
sprintf(str, "%" PRId64, item->valueint);
273+
snprintf(str, 21, "%" PRId64, item->valueint);
274274
} else {
275275
str = (char *)cJSON_malloc(64); /* This is a nice tradeoff. */
276276
if (fabs(floor(d) - d) <= DBL_EPSILON)
277-
sprintf(str, "%.0f", d);
277+
snprintf(str, 64, "%.0f", d);
278278
else if (fabs(d) < 1.0e-6 || fabs(d) > 1.0e9)
279-
sprintf(str, "%e", d);
279+
snprintf(str, 64, "%e", d);
280280
else
281-
sprintf(str, "%f", d);
281+
snprintf(str, 64, "%f", d);
282282
}
283283
return str;
284284
}

contrib/cliopts/cliopts.c

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -539,22 +539,31 @@ parse_option(struct cliopts_priv *ctx,
539539
return WANT_VALUE;
540540
}
541541

542+
static size_t bytes_left(const char *buf, size_t capacity)
543+
{
544+
size_t len = strlen(buf);
545+
if (len >= capacity - 1) {
546+
return 0;
547+
}
548+
return capacity - len;
549+
}
550+
542551
static char *
543-
get_option_name(cliopts_entry *entry, char *buf)
552+
get_option_name(cliopts_entry *entry, char *buf, size_t capacity)
544553
{
545554
/* [-s,--option] */
546555
char *bufp = buf;
547-
bufp += sprintf(buf, "[");
556+
bufp += snprintf(buf, bytes_left(buf, capacity), "[");
548557
if (entry->kshort) {
549-
bufp += sprintf(bufp, "-%c", entry->kshort);
558+
bufp += snprintf(bufp, bytes_left(buf, capacity), "-%c", entry->kshort);
550559
}
551560
if (entry->klong) {
552561
if (entry->kshort) {
553-
bufp += sprintf(bufp, ",");
562+
bufp += snprintf(bufp, bytes_left(buf, capacity), ",");
554563
}
555-
bufp += sprintf(bufp, "--%s", entry->klong);
564+
bufp += snprintf(bufp, bytes_left(buf, capacity), "--%s", entry->klong);
556565
}
557-
sprintf(bufp, "]");
566+
snprintf(bufp, bytes_left(buf, capacity), "]");
558567
return buf;
559568
}
560569

@@ -576,12 +585,12 @@ static int get_terminal_width(void)
576585

577586
static char*
578587
format_option_help(cliopts_entry *entry,
579-
char *buf,
588+
char *buf, size_t capacity,
580589
struct cliopts_extra_settings *settings)
581590
{
582591
char *bufp = buf;
583592
if (entry->kshort) {
584-
bufp += sprintf(bufp, " -%c ", entry->kshort);
593+
bufp += snprintf(bufp, bytes_left(buf, capacity), " -%c ", entry->kshort);
585594
}
586595

587596
#define _advance_margin(offset) \
@@ -595,11 +604,11 @@ format_option_help(cliopts_entry *entry,
595604
_advance_margin(4)
596605

597606
if (entry->klong) {
598-
bufp += sprintf(bufp, " --%s ", entry->klong);
607+
bufp += snprintf(bufp, bytes_left(buf, capacity), " --%s ", entry->klong);
599608
}
600609

601610
if (entry->vdesc) {
602-
bufp += sprintf(bufp, " <%s> ", entry->vdesc);
611+
bufp += snprintf(bufp, bytes_left(buf, capacity), " <%s> ", entry->vdesc);
603612
}
604613

605614
_advance_margin(35)
@@ -664,7 +673,7 @@ print_help(struct cliopts_priv *ctx, struct cliopts_extra_settings *settings)
664673
}
665674

666675
memset(helpbuf, 0, sizeof(helpbuf));
667-
format_option_help(cur, helpbuf, settings);
676+
format_option_help(cur, helpbuf, sizeof(helpbuf), settings);
668677
fprintf(stderr, INDENT "%s", helpbuf);
669678

670679

@@ -728,7 +737,7 @@ print_help(struct cliopts_priv *ctx, struct cliopts_extra_settings *settings)
728737
}
729738
memset(helpbuf, 0, sizeof(helpbuf));
730739
fprintf(stderr, INDENT "%s\n",
731-
format_option_help(&helpent, helpbuf, settings));
740+
format_option_help(&helpent, helpbuf, sizeof(helpbuf), settings));
732741

733742
}
734743

@@ -747,7 +756,7 @@ dump_error(struct cliopts_priv *ctx)
747756
} else if (ctx->errnum == CLIOPTS_ERR_ISSWITCH) {
748757
char optbuf[64] = { 0 };
749758
fprintf(stderr, "Option %s takes no arguments",
750-
get_option_name(ctx->prev, optbuf));
759+
get_option_name(ctx->prev, optbuf, sizeof(optbuf)));
751760
}
752761
fprintf(stderr, "\n");
753762

@@ -865,7 +874,7 @@ cliopts_parse_options(cliopts_entry *entries,
865874
}
866875

867876
fprintf(stderr, "Required option %s missing\n",
868-
get_option_name(cur_ent, entbuf));
877+
get_option_name(cur_ent, entbuf, sizeof(entbuf)));
869878
}
870879
}
871880

example/db/vb.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ int main(int argc, char *argv[])
223223
char design_path[64] = {0};
224224
char doc[256] = {0};
225225
lcb_CMDHTTP *cmd;
226-
sprintf(design_path, "_design/%s", design);
227-
sprintf(doc, "{\"views\":{\"all\":{\"map\":\"function(doc,meta){if(meta.id=='%s'){emit(meta.id)}}\"}}}", key);
226+
snprintf(design_path, sizeof(design_path), "_design/%s", design);
227+
snprintf(doc, sizeof(doc), "{\"views\":{\"all\":{\"map\":\"function(doc,meta){if(meta.id=='%s'){emit(meta.id)}}\"}}}", key);
228228

229229
lcb_cmdhttp_create(&cmd, LCB_HTTP_TYPE_VIEW);
230230
lcb_cmdhttp_path(cmd, design_path, strlen(design_path));

example/subdoc/subdoc-multi.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ int main(int argc, char **argv)
144144
std::string &val = bufs[(ii * 2) + 1];
145145
char pbuf[24], vbuf[24];
146146

147-
sprintf(pbuf, "pth%d", ii);
148-
sprintf(vbuf, "\"Value_%d\"", ii);
147+
snprintf(pbuf, sizeof(pbuf), "pth%d", ii);
148+
snprintf(vbuf, sizeof(vbuf), "\"Value_%d\"", ii);
149149
path = pbuf;
150150
val = vbuf;
151151

@@ -166,7 +166,7 @@ int main(int argc, char **argv)
166166
for (int ii = 0; ii < 5; ii++) {
167167
char pbuf[24];
168168
std::string &path = bufs[ii];
169-
sprintf(pbuf, "pth%d", ii);
169+
snprintf(pbuf, sizeof(pbuf), "pth%d", ii);
170170
path = pbuf;
171171

172172
lcb_subdocspecs_get(specs, ii, 0, path.c_str(), path.size());

src/analytics/analytics_handle.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ lcb_ANALYTICS_HANDLE_::lcb_ANALYTICS_HANDLE_(lcb_INSTANCE *obj, void *user_cooki
194194
// Set the default timeout as the server-side query timeout if no
195195
// other timeout is used.
196196
char buf[64] = {0};
197-
sprintf(buf, "%uus", LCBT_SETTING(obj, analytics_timeout));
197+
snprintf(buf, sizeof(buf), "%uus", LCBT_SETTING(obj, analytics_timeout));
198198
tmoval = buf;
199199
timeout_ = LCBT_SETTING(obj, analytics_timeout);
200200
} else if (tmoval.isString()) {

src/config_static.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115

116116
#if defined(_MSC_VER) && _MSC_VER < 1900
117117
#define snprintf _snprintf
118+
#define vsnprintf _vsnprintf
118119
#endif
119120

120121
#define strcasecmp(a, b) _stricmp(a, b)

src/hostlist.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ lcb_STATUS lcb_host_parse(lcb_host_t *host, const char *spec, int speclen, int d
142142
if (*port_s) {
143143
strcpy(host->port, port_s);
144144
} else {
145-
sprintf(host->port, "%d", deflport);
145+
snprintf(host->port, sizeof(host->port), "%d", deflport);
146146
}
147147
host->ipv6 = ipv6;
148148

src/http/http.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ lcb_STATUS Request::setup_inputs(const lcb_CMDHTTP *cmd)
712712

713713
if (!body.empty()) {
714714
char lenbuf[64];
715-
sprintf(lenbuf, "%lu", (unsigned long int)body.size());
715+
snprintf(lenbuf, sizeof(lenbuf), "%lu", (unsigned long int)body.size());
716716
add_header("Content-Length", lenbuf);
717717
if (cmd->content_type) {
718718
add_header("Content-Type", cmd->content_type);

src/n1ql/query_handle.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ lcb_QUERY_HANDLE_::lcb_QUERY_HANDLE_(lcb_INSTANCE *obj, void *user_cookie, const
534534
Json::Value &tmoval = json["timeout"];
535535
if (tmoval.isNull()) {
536536
char buf[64] = {0};
537-
sprintf(buf, "%uus", timeout);
537+
snprintf(buf, sizeof(buf), "%uus", timeout);
538538
tmoval = buf;
539539
json["timeout"] = buf;
540540
} else if (tmoval.isString()) {

src/operations/stats.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ lcb_STATUS lcb_stats(lcb_INSTANCE *instance, void *cookie, const lcb_CMDSTATS *c
187187
return LCB_ERR_INVALID_ARGUMENT;
188188
}
189189
}
190-
sprintf(ksbuf, "key %.*s %d", (int)kbuf_in->nbytes, (const char *)kbuf_in->bytes, vbid);
190+
snprintf(ksbuf, sizeof(ksbuf), "key %.*s %d", (int)kbuf_in->nbytes, (const char *)kbuf_in->bytes, vbid);
191191
kbuf_out.contig.nbytes = strlen(ksbuf);
192192
kbuf_out.contig.bytes = ksbuf;
193193
} else {

0 commit comments

Comments
 (0)