Skip to content

Commit f5b7fe1

Browse files
authored
Use snprintf instead of sprintf (#1086)
`sprintf` has been deprecated on macOS and a warning is issued when using it. `-Werror` turns the warning into an error, consequently tests don't pass on macOS. This fixes this by using `snprintf` instead.
1 parent d7512a3 commit f5b7fe1

File tree

6 files changed

+26
-20
lines changed

6 files changed

+26
-20
lines changed

exercises/practice/beer-song/.meta/example.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,33 @@
22
#include <stdint.h>
33
#include <stdio.h>
44

5+
static const size_t MAX_LINE_LENGTH = 1024;
6+
57
static uint16_t get_verse(uint8_t bottles, char **verse)
68
{
79
uint16_t lines_written = 0;
810

911
if (bottles == 0) {
10-
sprintf(*verse,
11-
"No more bottles of beer on the wall, no more bottles of beer.");
12-
sprintf(
13-
*++verse,
12+
snprintf(*verse, MAX_LINE_LENGTH,
13+
"No more bottles of beer on the wall, no more bottles of beer.");
14+
snprintf(
15+
*++verse, MAX_LINE_LENGTH,
1416
"Go to the store and buy some more, 99 bottles of beer on the wall.");
1517
lines_written = 2;
1618
} else if (bottles == 1) {
17-
sprintf(*verse, "%u bottle of beer on the wall, %u bottle of beer.",
18-
bottles, bottles);
19-
sprintf(
20-
*++verse,
19+
snprintf(*verse, MAX_LINE_LENGTH,
20+
"%u bottle of beer on the wall, %u bottle of beer.", bottles,
21+
bottles);
22+
snprintf(
23+
*++verse, MAX_LINE_LENGTH,
2124
"Take it down and pass it around, no more bottles of beer on the wall.");
2225
lines_written = 3;
2326
} else {
24-
sprintf(*verse, "%u bottles of beer on the wall, %u bottles of beer.",
25-
bottles, bottles);
26-
sprintf(
27-
*++verse,
27+
snprintf(*verse, MAX_LINE_LENGTH,
28+
"%u bottles of beer on the wall, %u bottles of beer.", bottles,
29+
bottles);
30+
snprintf(
31+
*++verse, MAX_LINE_LENGTH,
2832
"Take one down and pass it around, %u bottle%sof beer on the wall.",
2933
bottles - 1, bottles - 1 == 1 ? " " : "s ");
3034
lines_written = 3;

exercises/practice/clock/.meta/example.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ clock_t clock_create(int hour, int minute)
2626
{
2727
clock_t clock = { { 0 } };
2828
normalize_clock(&hour, &minute);
29-
sprintf(clock.text, CLOCK_FORMAT, hour, minute);
29+
snprintf(clock.text, MAX_STR_LEN, CLOCK_FORMAT, hour, minute);
3030
return clock;
3131
}
3232

exercises/practice/nucleotide-count/.meta/example.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ char *count(const char *dna_strand)
1212
size_t nucleotide_c_count = 0;
1313
size_t nucleotide_g_count = 0;
1414
size_t nucleotide_t_count = 0;
15-
char *count_results = calloc(1, 50);
15+
static const int max_length = 50;
16+
char *count_results = calloc(1, max_length);
1617

1718
for (index = 0; (index < strlen(dna_strand)) && (invalid_char == false);
1819
index++) {
@@ -36,8 +37,9 @@ char *count(const char *dna_strand)
3637
}
3738

3839
if (!invalid_char) {
39-
sprintf(count_results, "A:%zu C:%zu G:%zu T:%zu", nucleotide_a_count,
40-
nucleotide_c_count, nucleotide_g_count, nucleotide_t_count);
40+
snprintf(count_results, max_length, "A:%zu C:%zu G:%zu T:%zu",
41+
nucleotide_a_count, nucleotide_c_count, nucleotide_g_count,
42+
nucleotide_t_count);
4143
}
4244
return count_results;
4345
}

exercises/practice/raindrops/.meta/example.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void convert(char result[], int drops)
1616
}
1717
if (strlen(result) == 0) {
1818
char drops_string[12] = "\0";
19-
sprintf(drops_string, "%d", drops);
19+
snprintf(drops_string, sizeof(drops_string), "%d", drops);
2020
strcat(result, drops_string);
2121
}
2222
}

exercises/practice/run-length-encoding/.meta/example.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static size_t enc_helper(char *encoded, const char *text,
3131
const size_t cdigits = count_digits(count);
3232
enc_len += cdigits;
3333
if (mode == WRITE) {
34-
sprintf(encoded, "%zu", count);
34+
snprintf(encoded, cdigits + 1, "%zu", count);
3535
encoded += cdigits;
3636
}
3737
}

exercises/practice/series/.meta/example.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ slices_t slices(char *input_text, unsigned int substring_length)
1616
for (int index = 0; index < substring_count; index++) {
1717
results.substring[index] =
1818
malloc(sizeof(char) * (substring_length + 1));
19-
sprintf(results.substring[index], "%.*s", substring_length,
20-
&input_text[index]);
19+
snprintf(results.substring[index], MAX_SERIES_RESULTS, "%.*s",
20+
substring_length, &input_text[index]);
2121
}
2222
}
2323
return results;

0 commit comments

Comments
 (0)