summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md1
-rw-r--r--include/unistd.h26
-rw-r--r--tests/Makefile4
-rw-r--r--tests/test_swab_dynamic_read.c18
-rw-r--r--tests/test_swab_dynamic_write.c18
-rw-r--r--tests/test_swab_overwrite_over.c17
-rw-r--r--tests/test_swab_overwrite_under.c17
-rw-r--r--tests/test_swab_static_read.c18
-rw-r--r--tests/test_swab_static_write.c18
9 files changed, 137 insertions, 0 deletions
diff --git a/README.md b/README.md
index e187776..8f8824a 100644
--- a/README.md
+++ b/README.md
@@ -116,6 +116,7 @@ At this point, the program will safely and loudly crash.
116- `strncat` 116- `strncat`
117- `strncpy` 117- `strncpy`
118- `strrchr` 118- `strrchr`
119- `swab`
119- `tmpfile` 120- `tmpfile`
120- `ttyname_r` 121- `ttyname_r`
121- `umask` 122- `umask`
diff --git a/include/unistd.h b/include/unistd.h
index f6d4e87..b91dc56 100644
--- a/include/unistd.h
+++ b/include/unistd.h
@@ -178,6 +178,32 @@ _FORTIFY_FN(readlinkat) ssize_t readlinkat(int __f, const char *__p,
178 return __orig_readlinkat(__f, __p, __s, __n); 178 return __orig_readlinkat(__f, __p, __s, __n);
179} 179}
180 180
181#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
182#undef swab
183
184__fh_access(read_only, 1, 3)
185__fh_access(write_only, 2, 3)
186#if __has_builtin(__builtin_swab)
187__diagnose_as_builtin(__builtin_swab, 1, 2, 3)
188#endif
189_FORTIFY_FN(swab) void swab(const void *restrict _FORTIFY_POS0 __os,
190 void *restrict _FORTIFY_POS0 __od, ssize_t __n)
191__error_if((__fh_bos(__od, 0) < __n), "'swab' called with `n` bigger than the size of `d`.")
192{
193 __fh_size_t __bs = __fh_bos(__os, 0);
194 __fh_size_t __bd = __fh_bos(__od, 0);
195
196#if defined FORTIFY_PEDANTIC_CHECKS
197 if (__n > 0 && __fh_overlap(__os, __n, __od, __n))
198 __builtin_trap();
199#endif
200 if (__n > __bs || __n > __bd)
201 __builtin_trap();
202 return __orig_swab(__os, __od, __n);
203}
204
205#endif
206
181__fh_access(write_only, 2, 3) 207__fh_access(write_only, 2, 3)
182#if __has_builtin(__builtin_ttyname_r) 208#if __has_builtin(__builtin_ttyname_r)
183__diagnose_as_builtin(__builtin_ttyname_r, 1, 2, 3) 209__diagnose_as_builtin(__builtin_ttyname_r, 1, 2, 3)
diff --git a/tests/Makefile b/tests/Makefile
index e9e52dc..b4ea242 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -126,6 +126,10 @@ RUNTIME_TARGETS= \
126 test_strncpy_static_write \ 126 test_strncpy_static_write \
127 test_strrchr_dynamic_read \ 127 test_strrchr_dynamic_read \
128 test_strrchr_static_read \ 128 test_strrchr_static_read \
129 test_swab_dynamic_read \
130 test_swab_dynamic_write \
131 test_swab_overwrite_over \
132 test_swab_static_read \
129 test_ttyname_r_dynamic \ 133 test_ttyname_r_dynamic \
130 test_ttyname_r_static \ 134 test_ttyname_r_static \
131 test_umask \ 135 test_umask \
diff --git a/tests/test_swab_dynamic_read.c b/tests/test_swab_dynamic_read.c
new file mode 100644
index 0000000..402dc4e
--- /dev/null
+++ b/tests/test_swab_dynamic_read.c
@@ -0,0 +1,18 @@
1#define _XOPEN_SOURCE
2
3#include "common.h"
4
5#include <unistd.h>
6
7int main(int argc, char** argv) {
8 char buffer[12] = {0};
9 swab("1234567890", buffer, sizeof(buffer) - 1);
10 puts(buffer);
11
12 CHK_FAIL_START
13 swab("123456", buffer, argc);
14 CHK_FAIL_END
15
16 puts(buffer);
17 return ret;
18}
diff --git a/tests/test_swab_dynamic_write.c b/tests/test_swab_dynamic_write.c
new file mode 100644
index 0000000..21725b7
--- /dev/null
+++ b/tests/test_swab_dynamic_write.c
@@ -0,0 +1,18 @@
1#define _XOPEN_SOURCE
2
3#include "common.h"
4
5#include <unistd.h>
6
7int main(int argc, char** argv) {
8 char buffer[8] = {0};
9 swab("1234567890", buffer, sizeof(buffer) - 1);
10 puts(buffer);
11
12 CHK_FAIL_START
13 swab("1234567890", buffer, argc);
14 CHK_FAIL_END
15
16 puts(buffer);
17 return ret;
18}
diff --git a/tests/test_swab_overwrite_over.c b/tests/test_swab_overwrite_over.c
new file mode 100644
index 0000000..997f6d1
--- /dev/null
+++ b/tests/test_swab_overwrite_over.c
@@ -0,0 +1,17 @@
1#define _XOPEN_SOURCE
2
3#include "common.h"
4
5#include <unistd.h>
6
7int main(int argc, char** argv) {
8 char buffer[9] = {'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', '\0'};
9 puts(buffer);
10
11 CHK_FAIL_START
12 swab(buffer+1, buffer, 5);
13 CHK_FAIL_END
14
15 puts(buffer);
16 return ret;
17}
diff --git a/tests/test_swab_overwrite_under.c b/tests/test_swab_overwrite_under.c
new file mode 100644
index 0000000..1d69545
--- /dev/null
+++ b/tests/test_swab_overwrite_under.c
@@ -0,0 +1,17 @@
1#define _XOPEN_SOURCE
2
3#include "common.h"
4
5#include <unistd.h>
6
7int main(int argc, char** argv) {
8 char buffer[9] = {'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', '\0'};
9 puts(buffer);
10
11 CHK_FAIL_START
12 swab(buffer-1, buffer, 5);
13 CHK_FAIL_END
14
15 puts(buffer);
16 return ret;
17}
diff --git a/tests/test_swab_static_read.c b/tests/test_swab_static_read.c
new file mode 100644
index 0000000..f7fffcb
--- /dev/null
+++ b/tests/test_swab_static_read.c
@@ -0,0 +1,18 @@
1#define _XOPEN_SOURCE
2
3#include "common.h"
4
5#include <unistd.h>
6
7int main(int argc, char** argv) {
8 char buffer[8] = {0};
9 swab("123456", buffer, 4);
10 puts(buffer);
11
12 CHK_FAIL_START
13 swab("123456", buffer, sizeof(buffer));
14 CHK_FAIL_END
15
16 puts(buffer);
17 return ret;
18}
diff --git a/tests/test_swab_static_write.c b/tests/test_swab_static_write.c
new file mode 100644
index 0000000..b5a63c6
--- /dev/null
+++ b/tests/test_swab_static_write.c
@@ -0,0 +1,18 @@
1#define _XOPEN_SOURCE
2
3#include "common.h"
4
5#include <unistd.h>
6
7int main(int argc, char** argv) {
8 char buffer[8] = {0};
9 swab("1234567890", buffer, sizeof(buffer) - 1);
10 puts(buffer);
11
12 CHK_FAIL_START
13 swab("1234567890", buffer, sizeof(buffer) + 1);
14 CHK_FAIL_END
15
16 puts(buffer);
17 return ret;
18}