summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2014-04-24 19:51:45 +0000
committerrubidium <rubidium@openttd.org>2014-04-24 19:51:45 +0000
commite61fe2123728dd4cd2eba3ed628905b337ddff19 (patch)
treeaedd5865dbb78dc700d89d3f9478c73d90bcaf60 /src
parentfad2d3c709a6d468024284d9e6b7830f7c62e917 (diff)
downloadopenttd-e61fe2123728dd4cd2eba3ed628905b337ddff19.tar.xz
(svn r26506) -Codechange: replace most of vsnprintf with vseprintf
Diffstat (limited to 'src')
-rw-r--r--src/console.cpp2
-rw-r--r--src/debug.cpp2
-rw-r--r--src/game/game_text.cpp6
-rw-r--r--src/misc/str.hpp2
-rw-r--r--src/network/network_chat_gui.cpp2
-rw-r--r--src/newgrf.cpp2
-rw-r--r--src/newgrf_debug_gui.cpp2
-rw-r--r--src/openttd.cpp6
-rw-r--r--src/script/squirrel.cpp3
-rw-r--r--src/settingsgen/settingsgen.cpp2
-rw-r--r--src/strgen/strgen.cpp8
-rw-r--r--src/string.cpp5
-rw-r--r--src/string_func.h1
-rw-r--r--src/textbuf.cpp2
14 files changed, 25 insertions, 20 deletions
diff --git a/src/console.cpp b/src/console.cpp
index 29bab2f74..2274da64f 100644
--- a/src/console.cpp
+++ b/src/console.cpp
@@ -137,7 +137,7 @@ void CDECL IConsolePrintF(TextColour colour_code, const char *format, ...)
char buf[ICON_MAX_STREAMSIZE];
va_start(va, format);
- vsnprintf(buf, sizeof(buf), format, va);
+ vseprintf(buf, lastof(buf), format, va);
va_end(va);
IConsolePrint(colour_code, buf);
diff --git a/src/debug.cpp b/src/debug.cpp
index ad99dc38d..16eecadad 100644
--- a/src/debug.cpp
+++ b/src/debug.cpp
@@ -163,7 +163,7 @@ void CDECL debug(const char *dbg, const char *format, ...)
va_list va;
va_start(va, format);
- vsnprintf(buf, lengthof(buf), format, va);
+ vseprintf(buf, lastof(buf), format, va);
va_end(va);
debug_print(dbg, buf);
diff --git a/src/game/game_text.cpp b/src/game/game_text.cpp
index 83dc9a604..30e630495 100644
--- a/src/game/game_text.cpp
+++ b/src/game/game_text.cpp
@@ -31,7 +31,7 @@ void CDECL strgen_warning(const char *s, ...)
char buf[1024];
va_list va;
va_start(va, s);
- vsnprintf(buf, lengthof(buf), s, va);
+ vseprintf(buf, lastof(buf), s, va);
va_end(va);
DEBUG(script, 0, "%s:%d: warning: %s", _file, _cur_line, buf);
_warnings++;
@@ -42,7 +42,7 @@ void CDECL strgen_error(const char *s, ...)
char buf[1024];
va_list va;
va_start(va, s);
- vsnprintf(buf, lengthof(buf), s, va);
+ vseprintf(buf, lastof(buf), s, va);
va_end(va);
DEBUG(script, 0, "%s:%d: error: %s", _file, _cur_line, buf);
_errors++;
@@ -53,7 +53,7 @@ void NORETURN CDECL strgen_fatal(const char *s, ...)
char buf[1024];
va_list va;
va_start(va, s);
- vsnprintf(buf, lengthof(buf), s, va);
+ vseprintf(buf, lastof(buf), s, va);
va_end(va);
DEBUG(script, 0, "%s:%d: FATAL: %s", _file, _cur_line, buf);
throw std::exception();
diff --git a/src/misc/str.hpp b/src/misc/str.hpp
index b109b4e82..b982913fc 100644
--- a/src/misc/str.hpp
+++ b/src/misc/str.hpp
@@ -100,7 +100,7 @@ struct CStrA : public CBlobT<char>
int err = 0;
for (;;) {
char *buf = MakeFreeSpace(addSize);
- ret = vsnprintf(buf, base::GetReserve(), format, args);
+ ret = vseprintf(buf, buf + base::GetReserve() - 1, format, args);
if (ret >= (int)base::GetReserve()) {
/* Greater return than given count means needed buffer size. */
addSize = ret + 1;
diff --git a/src/network/network_chat_gui.cpp b/src/network/network_chat_gui.cpp
index f8a0867f3..b1a5dcb55 100644
--- a/src/network/network_chat_gui.cpp
+++ b/src/network/network_chat_gui.cpp
@@ -86,7 +86,7 @@ void CDECL NetworkAddChatMessage(TextColour colour, uint duration, const char *m
va_list va;
va_start(va, message);
- vsnprintf(buf, lengthof(buf), message, va);
+ vseprintf(buf, lastof(buf), message, va);
va_end(va);
Utf8TrimString(buf, DRAW_STRING_BUFFER);
diff --git a/src/newgrf.cpp b/src/newgrf.cpp
index 90fd24bf1..079c12162 100644
--- a/src/newgrf.cpp
+++ b/src/newgrf.cpp
@@ -377,7 +377,7 @@ void CDECL grfmsg(int severity, const char *str, ...)
va_list va;
va_start(va, str);
- vsnprintf(buf, sizeof(buf), str, va);
+ vseprintf(buf, lastof(buf), str, va);
va_end(va);
DEBUG(grf, severity, "[%s:%d] %s", _cur.grfconfig->filename, _cur.nfo_line, buf);
diff --git a/src/newgrf_debug_gui.cpp b/src/newgrf_debug_gui.cpp
index d3b5ddd16..6d5312514 100644
--- a/src/newgrf_debug_gui.cpp
+++ b/src/newgrf_debug_gui.cpp
@@ -400,7 +400,7 @@ struct NewGRFInspectWindow : Window {
va_list va;
va_start(va, format);
- vsnprintf(buf, lengthof(buf), format, va);
+ vseprintf(buf, lastof(buf), format, va);
va_end(va);
offset -= this->vscroll->GetPosition();
diff --git a/src/openttd.cpp b/src/openttd.cpp
index 4dcd49fbf..57e32489d 100644
--- a/src/openttd.cpp
+++ b/src/openttd.cpp
@@ -93,7 +93,7 @@ void CDECL usererror(const char *s, ...)
char buf[512];
va_start(va, s);
- vsnprintf(buf, lengthof(buf), s, va);
+ vseprintf(buf, lastof(buf), s, va);
va_end(va);
ShowOSErrorBox(buf, false);
@@ -113,7 +113,7 @@ void CDECL error(const char *s, ...)
char buf[512];
va_start(va, s);
- vsnprintf(buf, lengthof(buf), s, va);
+ vseprintf(buf, lastof(buf), s, va);
va_end(va);
ShowOSErrorBox(buf, true);
@@ -132,7 +132,7 @@ void CDECL ShowInfoF(const char *str, ...)
va_list va;
char buf[1024];
va_start(va, str);
- vsnprintf(buf, lengthof(buf), str, va);
+ vseprintf(buf, lastof(buf), str, va);
va_end(va);
ShowInfo(buf);
}
diff --git a/src/script/squirrel.cpp b/src/script/squirrel.cpp
index 9ead866e7..99e5ae1c0 100644
--- a/src/script/squirrel.cpp
+++ b/src/script/squirrel.cpp
@@ -21,9 +21,10 @@
/* Due to the different characters for Squirrel, the scsnprintf might be a simple
* snprint which triggers the safeguard. But it isn't always a simple snprintf.
- * Likewise for scstrcat. */
+ * Likewise for scvsnprintf and scstrcat. */
#include "../safeguards.h"
#undef snprintf
+#undef vsnprintf
#undef strcat
void Squirrel::CompileError(HSQUIRRELVM vm, const SQChar *desc, const SQChar *source, SQInteger line, SQInteger column)
diff --git a/src/settingsgen/settingsgen.cpp b/src/settingsgen/settingsgen.cpp
index 9f8cc1050..dd92e2627 100644
--- a/src/settingsgen/settingsgen.cpp
+++ b/src/settingsgen/settingsgen.cpp
@@ -42,7 +42,7 @@ void NORETURN CDECL error(const char *s, ...)
char buf[1024];
va_list va;
va_start(va, s);
- vsnprintf(buf, lengthof(buf), s, va);
+ vseprintf(buf, lastof(buf), s, va);
va_end(va);
fprintf(stderr, "FATAL: %s\n", buf);
exit(1);
diff --git a/src/strgen/strgen.cpp b/src/strgen/strgen.cpp
index 2fb89f088..4cd2454f4 100644
--- a/src/strgen/strgen.cpp
+++ b/src/strgen/strgen.cpp
@@ -53,7 +53,7 @@ void CDECL strgen_warning(const char *s, ...)
char buf[1024];
va_list va;
va_start(va, s);
- vsnprintf(buf, lengthof(buf), s, va);
+ vseprintf(buf, lastof(buf), s, va);
va_end(va);
fprintf(stderr, LINE_NUM_FMT("warning"), _file, _cur_line, buf);
_warnings++;
@@ -64,7 +64,7 @@ void CDECL strgen_error(const char *s, ...)
char buf[1024];
va_list va;
va_start(va, s);
- vsnprintf(buf, lengthof(buf), s, va);
+ vseprintf(buf, lastof(buf), s, va);
va_end(va);
fprintf(stderr, LINE_NUM_FMT("error"), _file, _cur_line, buf);
_errors++;
@@ -75,7 +75,7 @@ void NORETURN CDECL strgen_fatal(const char *s, ...)
char buf[1024];
va_list va;
va_start(va, s);
- vsnprintf(buf, lengthof(buf), s, va);
+ vseprintf(buf, lastof(buf), s, va);
va_end(va);
fprintf(stderr, LINE_NUM_FMT("FATAL"), _file, _cur_line, buf);
#ifdef _MSC_VER
@@ -89,7 +89,7 @@ void NORETURN CDECL error(const char *s, ...)
char buf[1024];
va_list va;
va_start(va, s);
- vsnprintf(buf, lengthof(buf), s, va);
+ vseprintf(buf, lastof(buf), s, va);
va_end(va);
fprintf(stderr, LINE_NUM_FMT("FATAL"), _file, _cur_line, buf);
#ifdef _MSC_VER
diff --git a/src/string.cpp b/src/string.cpp
index 8a95d7b6f..63288e431 100644
--- a/src/string.cpp
+++ b/src/string.cpp
@@ -32,7 +32,10 @@
#include "gfx_func.h"
#endif /* WITH_ICU */
+/* The function vsnprintf is used internally to perform the required formatting
+ * tasks. As such this one must be allowed, and makes sure it's terminated. */
#include "safeguards.h"
+#undef vsnprintf
/**
* Safer implementation of vsnprintf; same as vsnprintf except:
@@ -44,7 +47,7 @@
* @param ap the list of arguments for the format
* @return the number of added characters
*/
-static int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap)
+int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap)
{
ptrdiff_t diff = last - str;
if (diff < 0) return 0;
diff --git a/src/string_func.h b/src/string_func.h
index 8354d67cd..474340a69 100644
--- a/src/string_func.h
+++ b/src/string_func.h
@@ -34,6 +34,7 @@ char *strecpy(char *dst, const char *src, const char *last);
char *stredup(const char *src, const char *last = NULL);
int CDECL seprintf(char *str, const char *last, const char *format, ...) WARN_FORMAT(3, 4);
+int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap);
char *CDECL str_fmt(const char *str, ...) WARN_FORMAT(1, 2);
diff --git a/src/textbuf.cpp b/src/textbuf.cpp
index c11e3c1bf..116d60ba8 100644
--- a/src/textbuf.cpp
+++ b/src/textbuf.cpp
@@ -419,7 +419,7 @@ void Textbuf::Print(const char *format, ...)
{
va_list va;
va_start(va, format);
- vsnprintf(this->buf, this->max_bytes, format, va);
+ vseprintf(this->buf, &this->buf[this->max_bytes - 1], format, va);
va_end(va);
this->UpdateSize();
}