Skip to content

Commit 9d9a952

Browse files
practicalswiftrustyrussell
authored andcommitted
Use snprintf(...) instead of sprintf(...)
1 parent 0f7b11b commit 9d9a952

File tree

7 files changed

+12
-12
lines changed

7 files changed

+12
-12
lines changed

channeld/full_channel.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,7 @@ const char *channel_add_err_name(enum channel_add_err e)
10821082
if (enum_channel_add_err_names[i].v == e)
10831083
return enum_channel_add_err_names[i].name;
10841084
}
1085-
sprintf(invalidbuf, "INVALID %i", e);
1085+
snprintf(invalidbuf, sizeof(invalidbuf), "INVALID %i", e);
10861086
return invalidbuf;
10871087
}
10881088

@@ -1094,6 +1094,6 @@ const char *channel_remove_err_name(enum channel_remove_err e)
10941094
if (enum_channel_remove_err_names[i].v == e)
10951095
return enum_channel_remove_err_names[i].name;
10961096
}
1097-
sprintf(invalidbuf, "INVALID %i", e);
1097+
snprintf(invalidbuf, sizeof(invalidbuf), "INVALID %i", e);
10981098
return invalidbuf;
10991099
}

cli/lightning-cli.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ int main(int argc, char *argv[])
232232
* not need to have lightningd running in this case. */
233233
if (streq(method, "help") && format == HUMAN && argc >= 3) {
234234
char command[strlen(argv[2]) + sizeof("lightning-")];
235-
sprintf(command, "lightning-%s", argv[2]);
235+
snprintf(command, sizeof(command), "lightning-%s", argv[2]);
236236
exec_man(command);
237237
}
238238

common/json_escaped.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ static struct json_escaped *escape(const tal_t *ctx,
8989
break;
9090
default:
9191
if ((unsigned)str[i] < ' ' || str[i] == 127) {
92-
sprintf(esc->s + n, "\\u%04X", str[i]);
92+
snprintf(esc->s + n, 7, "\\u%04X", str[i]);
9393
n += 5;
9494
continue;
9595
}

lightningd/bitcoind.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ static void do_one_estimatefee(struct bitcoind *bitcoind,
350350
{
351351
char blockstr[STR_MAX_CHARS(u32)];
352352

353-
sprintf(blockstr, "%u", efee->blocks[efee->i]);
353+
snprintf(blockstr, sizeof(blockstr), "%u", efee->blocks[efee->i]);
354354
start_bitcoin_cli(bitcoind, NULL, process_estimatefee, false, NULL, efee,
355355
"estimatesmartfee", blockstr, efee->estmode[efee->i],
356356
NULL);
@@ -682,7 +682,7 @@ void bitcoind_getblockhash_(struct bitcoind *bitcoind,
682682
void *arg)
683683
{
684684
char str[STR_MAX_CHARS(height)];
685-
sprintf(str, "%u", height);
685+
snprintf(str, sizeof(str), "%u", height);
686686

687687
start_bitcoin_cli(bitcoind, NULL, process_getblockhash, true, cb, arg,
688688
"getblockhash", str, NULL);

lightningd/log.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,12 +360,12 @@ static void log_one_line(unsigned int skipped,
360360
char buf[101];
361361

362362
if (skipped) {
363-
sprintf(buf, "%s... %u skipped...", data->prefix, skipped);
363+
snprintf(buf, sizeof(buf), "%s... %u skipped...", data->prefix, skipped);
364364
write_all(data->fd, buf, strlen(buf));
365365
data->prefix = "\n";
366366
}
367367

368-
sprintf(buf, "%s+%lu.%09u %s%s: ",
368+
snprintf(buf, sizeof(buf), "%s+%lu.%09u %s%s: ",
369369
data->prefix,
370370
(unsigned long)diff.ts.tv_sec,
371371
(unsigned)diff.ts.tv_nsec,
@@ -501,7 +501,7 @@ static void log_dump_to_file(int fd, const struct log_book *lr)
501501
}
502502

503503
start = lr->init_time.ts.tv_sec;
504-
len = sprintf(buf, "%zu bytes, %s", lr->mem_used, ctime(&start));
504+
len = snprintf(buf, sizeof(buf), "%zu bytes, %s", lr->mem_used, ctime(&start));
505505
write_all(fd, buf, len);
506506

507507
/* ctime includes \n... WTF? */
@@ -579,7 +579,7 @@ static void json_add_time(struct json_result *result, const char *fieldname,
579579
{
580580
char timebuf[100];
581581

582-
sprintf(timebuf, "%lu.%09u",
582+
snprintf(timebuf, sizeof(timebuf), "%lu.%09u",
583583
(unsigned long)ts.tv_sec,
584584
(unsigned)ts.tv_nsec);
585585
json_add_string(result, fieldname, timebuf);

lightningd/memdump.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ static void json_add_ptr(struct json_result *response, const char *name,
1515
const void *ptr)
1616
{
1717
char ptrstr[STR_MAX_CHARS(void *)];
18-
sprintf(ptrstr, "%p", ptr);
18+
snprintf(ptrstr, sizeof(ptrstr), "%p", ptr);
1919
json_add_string(response, name, ptrstr);
2020
}
2121

tools/generate-wire.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ def find_message_with_option(messages, optional_messages, name, option):
703703
\t{cases}
704704
\t}}
705705
706-
\tsprintf(invalidbuf, "INVALID %i", e);
706+
\tsnprintf(invalidbuf, sizeof(invalidbuf), "INVALID %i", e);
707707
\treturn invalidbuf;
708708
}}
709709

0 commit comments

Comments
 (0)