Bug 1302233 - Introduce VsprintfLiteral to mfbt. r=froydnj
authorTom Schuster <evilpies@gmail.com>
Wed, 14 Sep 2016 20:48:45 +0200
changeset 314036 7bb1a3059c9100917db0240fd817f5695a8b0f76
parent 314035 6e355b66362685b8ff16f6cec365b359d4db3dab
child 314037 850159471c686be76390a2ee8dae12afbac7cc32
push id30705
push userkwierso@gmail.com
push dateThu, 15 Sep 2016 23:29:32 +0000
treeherdermozilla-central@7ad3c6ebc11a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1302233
milestone51.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1302233 - Introduce VsprintfLiteral to mfbt. r=froydnj
mfbt/Sprintf.h
--- a/mfbt/Sprintf.h
+++ b/mfbt/Sprintf.h
@@ -7,25 +7,36 @@
 /* Provides a safer sprintf for printing to fixed-size character arrays. */
 
 #ifndef mozilla_Sprintf_h_
 #define mozilla_Sprintf_h_
 
 #include <stdio.h>
 #include <stdarg.h>
 
+#include "mozilla/Assertions.h"
+
 #ifdef __cplusplus
+
+template <size_t N>
+int VsprintfLiteral(char (&buffer)[N], const char* format, va_list args)
+{
+    MOZ_ASSERT(format != buffer);
+    int result = vsnprintf(buffer, N, format, args);
+    buffer[N - 1] = '\0';
+    return result;
+}
+
 template <size_t N>
 #if defined(__GNUC__)
   __attribute__((format(printf, 2, 3)))
 #endif
 int SprintfLiteral(char (&buffer)[N], const char* format, ...)
 {
   va_list args;
   va_start(args, format);
-  int result = vsnprintf(buffer, N, format, args);
+  int result = VsprintfLiteral(buffer, format, args);
   va_end(args);
-  buffer[N - 1] = '\0';
   return result;
 }
+
 #endif
-
 #endif  /* mozilla_Sprintf_h_ */