Safe C

clang/utils/safe-c/juliet_port.h

Raw file ยท Directory on GitHub

1/* Safe C port helpers for NIST Juliet, included after std_testcase.h by
2 * juliet-port.ts. The C library's strcpy family is unavailable under bounds
3 * safety (it cannot see the destination's size). These replacements copy
4 * through bounds-checked pointers, so a copy past the end of the destination,
5 * or a read past the end of the source, traps: the bug is still caught, not
6 * hidden (strlcpy would truncate instead). Parameters are _Nonnull, as the
7 * C library's are (C17 7.1.4): this header is included as a system header,
8 * where unannotated pointers would not be checked for null. */
9#ifndef JULIET_PORT_H
10#define JULIET_PORT_H
11#include <stddef.h>
12#include <wchar.h>
13
14static inline char *juliet_strcpy(char *_Nonnull __bidi_indexable d,
15                                  const char *_Nonnull __bidi_indexable s) {
16  for (size_t i = 0;; i++) {
17    d[i] = s[i];
18    if (!s[i])
19      return d;
20  }
21}
22static inline char *juliet_strcat(char *_Nonnull __bidi_indexable d,
23                                  const char *_Nonnull __bidi_indexable s) {
24  size_t n = 0;
25  while (d[n])
26    n++;
27  juliet_strcpy(d + n, s);
28  return d;
29}
30static inline wchar_t *juliet_wcscpy(wchar_t *_Nonnull __bidi_indexable d,
31                                     const wchar_t *_Nonnull __bidi_indexable s) {
32  for (size_t i = 0;; i++) {
33    d[i] = s[i];
34    if (!s[i])
35      return d;
36  }
37}
38static inline wchar_t *juliet_wcscat(wchar_t *_Nonnull __bidi_indexable d,
39                                     const wchar_t *_Nonnull __bidi_indexable s) {
40  size_t n = 0;
41  while (d[n])
42    n++;
43  juliet_wcscpy(d + n, s);
44  return d;
45}
46static inline char *juliet_strncat(char *_Nonnull __bidi_indexable d,
47                                   const char *_Nonnull __bidi_indexable s, size_t n) {
48  size_t e = 0;
49  while (d[e])
50    e++;
51  size_t i = 0;
52  for (; i < n && s[i]; i++)
53    d[e + i] = s[i];
54  d[e + i] = 0;
55  return d;
56}
57static inline wchar_t *juliet_wcsncat(wchar_t *_Nonnull __bidi_indexable d,
58                                      const wchar_t *_Nonnull __bidi_indexable s,
59                                      size_t n) {
60  size_t e = 0;
61  while (d[e])
62    e++;
63  size_t i = 0;
64  for (; i < n && s[i]; i++)
65    d[e + i] = s[i];
66  d[e + i] = 0;
67  return d;
68}
69static inline char *juliet_strncpy(char *_Nonnull __bidi_indexable d,
70                                   const char *_Nonnull __bidi_indexable s,
71                                   size_t n) {
72  /* Exactly n bytes, zero-padded past the end of s, as C's strncpy. */
73  size_t i = 0;
74  for (; i < n && s[i]; i++)
75    d[i] = s[i];
76  for (; i < n; i++)
77    d[i] = 0;
78  return d;
79}
80static inline wchar_t *juliet_wcsncpy(wchar_t *_Nonnull __bidi_indexable d,
81                                      const wchar_t *_Nonnull __bidi_indexable s,
82                                      size_t n) {
83  size_t i = 0;
84  for (; i < n && s[i]; i++)
85    d[i] = s[i];
86  for (; i < n; i++)
87    d[i] = 0;
88  return d;
89}
90/* io.c's flags are set once and never written, but live in another file, so
91 * the compiler cannot see which branch of `if (globalTrue)` runs. A ported
92 * project would make them constants in the header; their values are io.c's,
93 * so every branch is taken exactly as before. */
94#ifndef JULIET_PORT_NO_CONST_FLAGS
95#define GLOBAL_CONST_TRUE 1
96#define GLOBAL_CONST_FALSE 0
97#define GLOBAL_CONST_FIVE 5
98#define globalTrue 1
99#define globalFalse 0
100#define globalFive 5
101#endif
102#define strcpy(d, s) juliet_strcpy((d), (s))
103#define strncpy(d, s, n) juliet_strncpy((d), (s), (n))
104#define wcsncpy(d, s, n) juliet_wcsncpy((d), (s), (n))
105#define strncat(d, s, n) juliet_strncat((d), (s), (n))
106#define wcsncat(d, s, n) juliet_wcsncat((d), (s), (n))
107#define strcat(d, s) juliet_strcat((d), (s))
108#define wcscpy(d, s) juliet_wcscpy((d), (s))
109#define wcscat(d, s) juliet_wcscat((d), (s))
110#endif
111