summaryrefslogtreecommitdiff
path: root/Libraries/LibC/stdio.cpp
diff options
context:
space:
mode:
authorVincent Sanders <vince@kyllikki.org>2019-10-24 13:12:37 +0100
committerAndreas Kling <awesomekling@gmail.com>2019-10-24 14:12:37 +0200
commit1be4c6e9cfc6060f77efe1aaa061680f9f23fc83 (patch)
tree6483b8687f98e6e822555317353ddcf5fdb49b56 /Libraries/LibC/stdio.cpp
parentbddabad3d9c1dce986d9d71538ecd376bdb286a0 (diff)
downloadserenity-1be4c6e9cfc6060f77efe1aaa061680f9f23fc83.zip
LibC: Stop stdio from adding null terminators out of bounds (#685)
When using the bounded string operations (e.g. snprintf), the null terminator was always being written even if there was no space for it (or indeed any valid buffer at all) This overwriting caused segmentation faults and memory corruption
Diffstat (limited to 'Libraries/LibC/stdio.cpp')
-rw-r--r--Libraries/LibC/stdio.cpp6
1 files changed, 3 insertions, 3 deletions
diff --git a/Libraries/LibC/stdio.cpp b/Libraries/LibC/stdio.cpp
index 3da19ac12f..ae9872c877 100644
--- a/Libraries/LibC/stdio.cpp
+++ b/Libraries/LibC/stdio.cpp
@@ -395,7 +395,6 @@ int sprintf(char* buffer, const char* fmt, ...)
va_list ap;
va_start(ap, fmt);
int ret = vsprintf(buffer, fmt, ap);
- buffer[ret] = '\0';
va_end(ap);
return ret;
}
@@ -413,7 +412,9 @@ int vsnprintf(char* buffer, size_t size, const char* fmt, va_list ap)
{
__vsnprintf_space_remaining = size;
int ret = printf_internal(sized_buffer_putch, buffer, fmt, ap);
- buffer[ret] = '\0';
+ if (__vsnprintf_space_remaining) {
+ buffer[ret] = '\0';
+ }
return ret;
}
@@ -422,7 +423,6 @@ int snprintf(char* buffer, size_t size, const char* fmt, ...)
va_list ap;
va_start(ap, fmt);
int ret = vsnprintf(buffer, size, fmt, ap);
- buffer[ret] = '\0';
va_end(ap);
return ret;
}