|
2 | 2 | #include <stdint.h> |
3 | 3 | #include <stdio.h> |
4 | 4 |
|
| 5 | +static const size_t MAX_LINE_LENGTH = 1024; |
| 6 | + |
5 | 7 | static uint16_t get_verse(uint8_t bottles, char **verse) |
6 | 8 | { |
7 | 9 | uint16_t lines_written = 0; |
8 | 10 |
|
9 | 11 | 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, |
14 | 16 | "Go to the store and buy some more, 99 bottles of beer on the wall."); |
15 | 17 | lines_written = 2; |
16 | 18 | } 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, |
21 | 24 | "Take it down and pass it around, no more bottles of beer on the wall."); |
22 | 25 | lines_written = 3; |
23 | 26 | } 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, |
28 | 32 | "Take one down and pass it around, %u bottle%sof beer on the wall.", |
29 | 33 | bottles - 1, bottles - 1 == 1 ? " " : "s "); |
30 | 34 | lines_written = 3; |
|
0 commit comments