Skip to content

Commit 087d85e

Browse files
lib/string/: Use array notation with forward declarations
GNU C has an extension which allows to forward-declare parameters, so that array notation can be used with sizes that are defined after the array itself. This improves the safety of such arrays, by telling the compiler the bounds of the array. This feature has been proposed for standardization in C2y as n3394. Link: n3394 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3394.pdf> Cc: Martin Uecker <uecker@tugraz.at> Signed-off-by: Alejandro Colomar <alx@kernel.org>
1 parent e6ba045 commit 087d85e

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

lib/string/sprintf/snprintf.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <stddef.h>
1111

1212

13-
extern inline int snprintf_(char *restrict s, size_t size,
14-
const char *restrict fmt, ...);
15-
extern inline int vsnprintf_(char *restrict s, size_t size,
16-
const char *restrict fmt, va_list ap);
13+
extern inline int snprintf_(size_t size;
14+
char s[restrict size], size_t size, const char *restrict fmt, ...);
15+
extern inline int vsnprintf_(size_t size;
16+
char s[restrict size], size_t size, const char *restrict fmt, va_list ap);

lib/string/sprintf/snprintf.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@
2323

2424

2525
format_attr(printf, 3, 4)
26-
inline int snprintf_(char *restrict s, size_t size, const char *restrict fmt,
27-
...);
26+
inline int snprintf_(size_t size;
27+
char s[restrict size], size_t size, const char *restrict fmt, ...);
2828
format_attr(printf, 3, 0)
29-
inline int vsnprintf_(char *restrict s, size_t size, const char *restrict fmt,
30-
va_list ap);
29+
inline int vsnprintf_(size_t size;
30+
char s[restrict size], size_t size, const char *restrict fmt, va_list ap);
3131

3232

3333
inline int
34-
snprintf_(char *restrict s, size_t size, const char *restrict fmt, ...)
34+
snprintf_(size_t size;
35+
char s[restrict size], size_t size, const char *restrict fmt, ...)
3536
{
3637
int len;
3738
va_list ap;
@@ -45,7 +46,8 @@ snprintf_(char *restrict s, size_t size, const char *restrict fmt, ...)
4546

4647

4748
inline int
48-
vsnprintf_(char *restrict s, size_t size, const char *restrict fmt, va_list ap)
49+
vsnprintf_(size_t size;
50+
char s[restrict size], size_t size, const char *restrict fmt, va_list ap)
4951
{
5052
int len;
5153

lib/string/strcpy/strtcpy.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
#include "string/strcpy/strtcpy.h"
1111

1212

13-
extern inline ssize_t strtcpy(char *restrict dst, const char *restrict src,
14-
size_t dsize);
13+
extern inline ssize_t strtcpy(size_t dsize;
14+
char dst[restrict dsize], const char *restrict src, size_t dsize);

lib/string/strcpy/strtcpy.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@
4848

4949

5050
ATTR_STRING(2)
51-
inline ssize_t strtcpy(char *restrict dst, const char *restrict src,
52-
size_t dsize);
51+
inline ssize_t strtcpy(size_t dsize;
52+
char dst[restrict dsize], const char *restrict src, size_t dsize);
5353

5454

5555
inline ssize_t
56-
strtcpy(char *restrict dst, const char *restrict src, size_t dsize)
56+
strtcpy(size_t dsize;
57+
char dst[restrict dsize], const char *restrict src, size_t dsize)
5758
{
5859
bool trunc;
5960
size_t dlen, slen;

0 commit comments

Comments
 (0)