summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Holland <samuel@sholland.org>2018-01-14 22:39:41 -0600
committerSamuel Holland <samuel@sholland.org>2018-01-15 00:02:54 -0600
commit101dbb13a1d0270b931291607962e6780ec856a5 (patch)
treec6aad3e51cdac4a44ef189eac9e38c3417ca5e6c
parentc735fc99ec9e96520da0ea938890eed8fe64b50e (diff)
downloadgcompat-101dbb13a1d0270b931291607962e6780ec856a5.tar.gz
gcompat-101dbb13a1d0270b931291607962e6780ec856a5.tar.bz2
gcompat-101dbb13a1d0270b931291607962e6780ec856a5.tar.xz
gcompat-101dbb13a1d0270b931291607962e6780ec856a5.zip
stdio: Clean up, add all of LSB stdio plus more
* Add all (non-wchar) stdio functions from LSB, plus those found in use in other applications. Document those functions from LSB as such. * Use a consistent structure and paramater names for all functions. * flag == 0 means FORTIFY_SOURCE=1, so the implemented checks should be unconditional. * Add all possible checks without parsing the format string. * Move functions from wchar.h to their own appropriately-named file. Signed-off-by: Samuel Holland <samuel@sholland.org>
-rw-r--r--Makefile3
-rw-r--r--libgcompat/stdio.c273
-rw-r--r--libgcompat/wchar.c40
3 files changed, 234 insertions, 82 deletions
diff --git a/Makefile b/Makefile
index 14c68f4..b5fda64 100644
--- a/Makefile
+++ b/Makefile
@@ -17,7 +17,8 @@ LIBGCOMPAT_SRC = \
libgcompat/string.c \
libgcompat/sysctl.c \
libgcompat/ucontext.c \
- libgcompat/version.c
+ libgcompat/version.c \
+ libgcompat/wchar.c
LIBGCOMPAT_OBJ = ${LIBGCOMPAT_SRC:.c=.o}
LIBGCOMPAT_SOVERSION = 0
LIBGCOMPAT_NAME = libgcompat.so.${LIBGCOMPAT_SOVERSION}
diff --git a/libgcompat/stdio.c b/libgcompat/stdio.c
index a44d94c..0ff5feb 100644
--- a/libgcompat/stdio.c
+++ b/libgcompat/stdio.c
@@ -1,125 +1,236 @@
-#include <assert.h>
-#include <stdarg.h>
-#include <stdio.h>
-#include <wchar.h>
-
-size_t __fread_chk(void *ptr, size_t size, size_t nmemb, FILE *stream)
+#define _GNU_SOURCE /* fgets_unlocked */
+#include <assert.h> /* assert */
+#include <stdarg.h> /* va_list, va_start, va_end */
+#include <stddef.h> /* NULL, size_t */
+#include <stdio.h> /* feof, fgets, fread, puts, v*printf */
+
+int __vasprintf_chk(char **strp, int flag, const char *format, va_list ap);
+int __vfprintf_chk(FILE *stream, int flag, const char *format, va_list ap);
+int __vsnprintf_chk(char *s, size_t n, int flag, size_t slen,
+ const char *format, va_list ap);
+int __vsprintf_chk(char *s, int flag, size_t slen, const char *format,
+ va_list ap);
+
+/**
+ * Test end-of-file indicator on a stream.
+ *
+ * LSB 5.0: LSB-Core-generic/baselib--io-feof-3.html
+ */
+int _IO_feof(FILE *stream)
{
- assert(ptr != NULL);
- assert(stream != NULL);
- return fread(ptr, size, nmemb, stream);
+ return feof(stream);
}
-int __printf_chk(int flag, const char *format, ...)
+/**
+ * Put a string on standard output.
+ *
+ * LSB 5.0: LSB-Core-generic/baselib--io-puts-3.html
+ */
+int _IO_puts(const char *c)
{
- va_list argp;
- int result;
+ return puts(c);
+}
- if (flag > 0) {
- assert(format != NULL);
- }
+/**
+ * Print to allocated string, with stack checking.
+ */
+int __asprintf_chk(char **strp, int flag, const char *format, ...)
+{
+ int ret;
+ va_list ap;
- va_start(argp, format);
- result = vprintf(format, argp);
- va_end(argp);
+ va_start(ap, format);
+ ret = __vasprintf_chk(strp, flag, format, ap);
+ va_end(ap);
- return result;
+ return ret;
}
-int __fprintf_chk(FILE *stream, int flag, const char *format, ...)
+/**
+ * String input, with buffer overflow checking.
+ *
+ * LSB 5.0: LSB-Core-generic/baselib---fgets-chk-1.html
+ */
+char *__fgets_chk(char *s, size_t slen, int n, FILE *stream)
{
- va_list argp;
- int result;
+ assert(s != NULL);
+ assert(slen >= (size_t) n);
+ assert(n > 0);
+ assert(stream != NULL);
- if (flag > 0) {
- assert(stream != NULL);
- assert(format != NULL);
- }
+ return fgets(s, n, stream);
+}
- va_start(argp, format);
- result = vfprintf(stream, format, argp);
- va_end(argp);
+/**
+ * Non-locking string input, with buffer overflow checking.
+ *
+ * LSB 5.0: LSB-Core-generic/baselib---fgets-unlocked-chk-1.html
+ */
+char *__fgets_unlocked_chk(char *s, size_t slen, int n, FILE *stream)
+{
+ assert(s != NULL);
+ assert(slen >= (size_t) n);
+ assert(n > 0);
+ assert(stream != NULL);
- return result;
+ return fgets_unlocked(s, n, stream);
}
-int __sprintf_chk(char *str, int flag, size_t strlen, const char *format, ...)
+/**
+ * Convert formatted output, with stack checking.
+ *
+ * LSB 5.0: LSB-Core-generic/baselib---fprintf-chk-1.html
+ */
+int __fprintf_chk(FILE *stream, int flag, const char *format, ...)
{
- va_list argp;
- int result;
-
- assert(strlen > 0);
+ int ret;
+ va_list ap;
- va_start(argp, format);
- result = vsnprintf(str, strlen, format, argp);
- va_end(argp);
+ va_start(ap, format);
+ ret = __vfprintf_chk(stream, flag, format, ap);
+ va_end(ap);
- return result;
+ return ret;
}
-int __snprintf_chk(char *str, size_t size, int flag, size_t strlen,
- const char *format, ...)
+/**
+ * Binary input, with stack checking.
+ */
+size_t __fread_chk(void *buf, size_t buflen, size_t size, size_t nitems,
+ FILE *stream)
{
- va_list argp;
- int result;
+ assert(buf != NULL);
+ assert(size > 0);
+ assert(buflen / size >= nitems);
+ assert(stream != NULL);
- if (flag > 0) {
- assert(format != NULL);
- }
- // must always be done per LFS
- assert(size <= strlen);
+ return fread(buf, size, nitems, stream);
+}
- va_start(argp, format);
- result = vsnprintf(str, size, format, argp);
- va_end(argp);
+/**
+ * Format and print data, with stack checking.
+ *
+ * LSB 5.0: LSB-Core-generic/baselib---printf-chk-1.html
+ */
+int __printf_chk(int flag, const char *format, ...)
+{
+ int ret;
+ va_list ap;
- return result;
+ va_start(ap, format);
+ ret = __vfprintf_chk(stdout, flag, format, ap);
+ va_end(ap);
+
+ return ret;
}
-int __swprintf_chk(wchar_t *wcs, size_t maxlen, int flag, size_t wcslen,
- const wchar_t *format, ...)
+/**
+ * Convert formatted output, with buffer overflow checking.
+ *
+ * LSB 5.0: LSB-Core-generic/baselib---snprintf-chk-1.html
+ */
+int __snprintf_chk(char *s, size_t n, int flag, size_t slen, const char *format,
+ ...)
{
- va_list argp;
- int result;
+ int ret;
+ va_list ap;
- if (flag > 0) {
- assert(format != NULL);
- }
- // must always be done per LFS
- assert(maxlen <= wcslen);
+ va_start(ap, format);
+ ret = __vsnprintf_chk(s, n, flag, slen, format, ap);
+ va_end(ap);
- va_start(argp, format);
- result = vswprintf(wcs, maxlen, format, argp);
- va_end(argp);
+ return ret;
+}
+
+/**
+ * Convert formatted output, with stack checking.
+ *
+ * LSB 5.0: LSB-Core-generic/baselib---sprintf-chk-1.html
+ */
+int __sprintf_chk(char *s, int flag, size_t slen, const char *format, ...)
+{
+ int ret;
+ va_list ap;
- return result;
+ va_start(ap, format);
+ ret = __vsprintf_chk(s, flag, slen, format, ap);
+ va_end(ap);
+
+ return ret;
}
-int __vasprintf_chk(char **strp, int flag, const char *fmt, va_list ap)
+/**
+ * Print to allocated string, with stack checking.
+ */
+int __vasprintf_chk(char **strp, int flag, const char *format, va_list ap)
{
- if (flag > 0) {
- assert(strp != NULL);
- assert(fmt != NULL);
- }
- return vasprintf(strp, fmt, ap);
+ assert(strp != NULL);
+ assert(format != NULL);
+
+ return vasprintf(strp, format, ap);
}
+/**
+ * Convert formatted output, with stack checking.
+ *
+ * LSB 5.0: LSB-Core-generic/baselib---vfprintf-chk-1.html
+ */
int __vfprintf_chk(FILE *stream, int flag, const char *format, va_list ap)
{
- if (flag > 0) {
- assert(stream != NULL);
- assert(format != NULL);
- }
+ assert(stream != NULL);
+ assert(format != NULL);
+
return vfprintf(stream, format, ap);
}
-int __vsnprintf_chk(char *str, size_t size, int flag, size_t strlen,
+/**
+ * Convert formatted output, with stack checking.
+ *
+ * LSB 5.0: LSB-Core-generic/baselib---vprintf-chk-1.html
+ */
+int __vprintf_chk(int flag, const char *format, va_list ap)
+{
+ return __vfprintf_chk(stdout, flag, format, ap);
+}
+
+/**
+ * Convert formatted output, with stack checking.
+ *
+ * LSB 5.0: LSB-Core-generic/baselib---vsnprintf-chk-1.html
+ */
+int __vsnprintf_chk(char *s, size_t n, int flag, size_t slen,
const char *format, va_list ap)
{
- if (flag > 0) {
- assert(format != NULL);
+ assert(s != NULL || n == 0);
+ assert(slen >= n);
+ assert(format != NULL);
+
+ return vsnprintf(s, n, format, ap);
+}
+
+/**
+ * Convert formatted output, with stack checking.
+ *
+ * LSB 5.0: LSB-Core-generic/baselib---vsprintf-chk-1.html
+ */
+int __vsprintf_chk(char *s, int flag, size_t slen, const char *format,
+ va_list ap)
+{
+ assert(s != NULL);
+ assert(slen > 0);
+ assert(format != NULL);
+
+ return vsprintf(s, format, ap);
+}
+
+/**
+ * Create a name for a temporary file.
+ */
+char *tmpnam_r(char *s)
+{
+ if (s == NULL) {
+ return NULL;
}
- // must always be done per LFS
- assert(size <= strlen);
- return vsnprintf(str, size, format, ap);
+
+ return tmpnam(s);
}
diff --git a/libgcompat/wchar.c b/libgcompat/wchar.c
new file mode 100644
index 0000000..1ddaa1f
--- /dev/null
+++ b/libgcompat/wchar.c
@@ -0,0 +1,40 @@
+#include <assert.h> /* assert */
+#include <stdarg.h> /* va_list, va_start, va_end */
+#include <stddef.h> /* size_t */
+#include <wchar.h> /* wchar_t, *wprintf */
+
+int __vswprintf_chk(wchar_t *s, size_t n, int flag, size_t slen,
+ const wchar_t *format, va_list ap);
+
+/**
+ * Convert formatted wide-character output, with stack checking
+ *
+ * LSB 5.0: LSB-Core-generic/baselib---swprintf-chk-1.html
+ */
+int __swprintf_chk(wchar_t *s, size_t n, int flag, size_t slen,
+ const wchar_t *format, ...)
+{
+ int ret;
+ va_list ap;
+
+ va_start(ap, format);
+ ret = __vswprintf_chk(s, n, flag, slen, format, ap);
+ va_end(ap);
+
+ return ret;
+}
+
+/**
+ * Convert formatted wide-character output, with stack checking
+ *
+ * LSB 5.0: LSB-Core-generic/baselib---vswprintf-chk-1.html
+ */
+int __vswprintf_chk(wchar_t *s, size_t n, int flag, size_t slen,
+ const wchar_t *format, va_list ap)
+{
+ assert(s != NULL || n == 0);
+ assert(slen >= n);
+ assert(format != NULL);
+
+ return vswprintf(s, n, format, ap);
+}