aboutsummaryrefslogtreecommitdiffstats
path: root/libc/include/stdio.h
diff options
context:
space:
mode:
authorNick Kralevich <nnk@google.com>2012-07-03 11:45:31 -0700
committerNick Kralevich <nnk@google.com>2012-07-09 09:57:18 -0700
commit965dbc6405aa2c3170270cfc53a8d4416444fddb (patch)
tree7764d865c47bc84041f521ac11748ebe986a8871 /libc/include/stdio.h
parent2ddf77b37731dff3a271c1312fc0bef2e7d41473 (diff)
downloadandroid_bionic-965dbc6405aa2c3170270cfc53a8d4416444fddb.tar.gz
android_bionic-965dbc6405aa2c3170270cfc53a8d4416444fddb.tar.bz2
android_bionic-965dbc6405aa2c3170270cfc53a8d4416444fddb.zip
FORTIFY_SOURCE: add fgets support.
Change-Id: I8c3410a90c71a3336c4ac8581618fa9330edf5e3
Diffstat (limited to 'libc/include/stdio.h')
-rw-r--r--libc/include/stdio.h39
1 files changed, 39 insertions, 0 deletions
diff --git a/libc/include/stdio.h b/libc/include/stdio.h
index 18b19bf7c..c12ddb812 100644
--- a/libc/include/stdio.h
+++ b/libc/include/stdio.h
@@ -527,6 +527,45 @@ int sprintf(char *dest, const char *format, ...)
# endif /* !defined(__clang__) */
+extern char *__fgets_real(char *, int, FILE *)
+ __asm__(__USER_LABEL_PREFIX__ "fgets");
+extern void __fgets_too_big_error()
+ __attribute__((__error__("fgets called with size bigger than buffer")));
+extern void __fgets_too_small_error()
+ __attribute__((__error__("fgets called with size less than zero")));
+extern char *__fgets_chk(char *, int, FILE *, size_t);
+
+__BIONIC_FORTIFY_INLINE
+char *fgets(char *dest, int size, FILE *stream)
+{
+ size_t bos = __builtin_object_size(dest, 0);
+
+ // Compiler can prove, at compile time, that the passed in size
+ // is always negative. Force a compiler error.
+ if (__builtin_constant_p(size) && (size < 0)) {
+ __fgets_too_small_error();
+ }
+
+ // Compiler doesn't know destination size. Don't call __fgets_chk
+ if (bos == (size_t) -1) {
+ return __fgets_real(dest, size, stream);
+ }
+
+ // Compiler can prove, at compile time, that the passed in size
+ // is always <= the actual object size. Don't call __fgets_chk
+ if (__builtin_constant_p(size) && (size <= bos)) {
+ return __fgets_real(dest, size, stream);
+ }
+
+ // Compiler can prove, at compile time, that the passed in size
+ // is always > the actual object size. Force a compiler error.
+ if (__builtin_constant_p(size) && (size > bos)) {
+ __fgets_too_big_error();
+ }
+
+ return __fgets_chk(dest, size, stream, bos);
+}
+
#endif /* defined(__BIONIC_FORTIFY_INLINE) */
#endif /* _STDIO_H_ */