summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/unistd.h2
-rw-r--r--tests/Makefile1
-rw-r--r--tests/test_swab_negative.c19
3 files changed, 21 insertions, 1 deletions
diff --git a/include/unistd.h b/include/unistd.h
index a2b3105..a73279b 100644
--- a/include/unistd.h
+++ b/include/unistd.h
@@ -177,7 +177,7 @@ _FORTIFY_FN(swab) void swab(const void * _FORTIFY_POS0 __os,
177 size_t __bs = __bos(__os, 0); 177 size_t __bs = __bos(__os, 0);
178 size_t __bd = __bos(__od, 0); 178 size_t __bd = __bos(__od, 0);
179 179
180 if ((size_t)__n > __bs || (size_t)__n > __bd) 180 if (__n > 0 && ((size_t)__n > __bs || (size_t)__n > __bd))
181 __builtin_trap(); 181 __builtin_trap();
182 return __orig_swab(__os, __od, __n); 182 return __orig_swab(__os, __od, __n);
183} 183}
diff --git a/tests/Makefile b/tests/Makefile
index fbfd178..9fc8287 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -87,6 +87,7 @@ RUNTIME_TARGETS= \
87 test_strncpy_static_write \ 87 test_strncpy_static_write \
88 test_swab_dynamic_read \ 88 test_swab_dynamic_read \
89 test_swab_dynamic_write \ 89 test_swab_dynamic_write \
90 test_swab_negative \
90 test_swab_static_read \ 91 test_swab_static_read \
91 test_ttyname_r_dynamic \ 92 test_ttyname_r_dynamic \
92 test_ttyname_r_static \ 93 test_ttyname_r_static \
diff --git a/tests/test_swab_negative.c b/tests/test_swab_negative.c
new file mode 100644
index 0000000..f9c182b
--- /dev/null
+++ b/tests/test_swab_negative.c
@@ -0,0 +1,19 @@
1#include "common.h"
2
3#include <unistd.h>
4
5int main(int argc, char** argv) {
6 char src[8] = "ABCDEFG";
7 char dst[8] = {0};
8
9 /* Positive case: normal swab works */
10 swab(src, dst, 6);
11 puts(dst);
12
13 /* Negative n: POSIX says swab does nothing, must NOT trap */
14 swab(src, dst, -1);
15 swab(src, dst, -100);
16
17 puts(dst);
18 return ret;
19}