Bug 1530560 - Change NS_tsnprintf to return a bool to indicate success and failure. r=mhowell
authorRobert Strong <robert.bugzilla@gmail.com>
Tue, 26 Feb 2019 18:28:45 +0000
changeset 519126 dfb53709e5fdb77ef09ff281309afb8eb845a83f
parent 519125 2f5d1c8ec6f3df50a6a452e96f74e8f3033134e3
child 519127 44b0b51ef2143bbb81d47d60fd3ed8a6451751ec
push id10862
push userffxbld-merge
push dateMon, 11 Mar 2019 13:01:11 +0000
treeherdermozilla-beta@a2e7f5c935da [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmhowell
bugs1530560
milestone67.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 1530560 - Change NS_tsnprintf to return a bool to indicate success and failure. r=mhowell Differential Revision: https://phabricator.services.mozilla.com/D21163
toolkit/mozapps/update/common/updatedefines.h
--- a/toolkit/mozapps/update/common/updatedefines.h
+++ b/toolkit/mozapps/update/common/updatedefines.h
@@ -1,24 +1,24 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef UPDATEDEFINES_H
 #define UPDATEDEFINES_H
 
+#include <stdio.h>
+#include <stdarg.h>
 #include "readstrings.h"
 
 #if defined(XP_WIN)
 #  include <windows.h>
 #  include <shlwapi.h>
 #  include <direct.h>
 #  include <io.h>
-#  include <stdio.h>
-#  include <stdarg.h>
 
 #  ifndef F_OK
 #    define F_OK 00
 #  endif
 #  ifndef W_OK
 #    define W_OK 02
 #  endif
 #  ifndef R_OK
@@ -36,28 +36,17 @@
 #  define DELETE_DIR L"tobedeleted"
 #  define CALLBACK_BACKUP_EXT L".moz-callback"
 
 #  define LOG_S "%S"
 #  define NS_CONCAT(x, y) x##y
 // The extra layer of indirection here allows this macro to be passed macros
 #  define NS_T(str) NS_CONCAT(L, str)
 #  define NS_SLASH NS_T('\\')
-
-static inline int mywcsprintf(WCHAR* dest, size_t count, const WCHAR* fmt,
-                              ...) {
-  size_t _count = count - 1;
-  va_list varargs;
-  va_start(varargs, fmt);
-  int result = _vsnwprintf(dest, count - 1, fmt, varargs);
-  va_end(varargs);
-  dest[_count] = L'\0';
-  return result;
-}
-#  define NS_tsnprintf mywcsprintf
+#  define NS_tvsnprintf _vsnwprintf
 #  define NS_taccess _waccess
 #  define NS_tatoi _wtoi64
 #  define NS_tchdir _wchdir
 #  define NS_tchmod _wchmod
 #  define NS_tfopen _wfopen
 #  define NS_tmkdir(path, perms) _wmkdir(path)
 #  define NS_tpid __int64
 #  define NS_tremove _wremove
@@ -95,17 +84,17 @@ static inline int mywcsprintf(WCHAR* des
 
 #  ifdef XP_MACOSX
 #    include <sys/time.h>
 #  endif
 
 #  define LOG_S "%s"
 #  define NS_T(str) str
 #  define NS_SLASH NS_T('/')
-#  define NS_tsnprintf snprintf
+#  define NS_tvsnprintf vsnprintf
 #  define NS_taccess access
 #  define NS_tatoi atoi
 #  define NS_tchdir chdir
 #  define NS_tchmod chmod
 #  define NS_tfopen fopen
 #  define NS_tmkdir mkdir
 #  define NS_tpid int
 #  define NS_tremove remove
@@ -140,9 +129,21 @@ static inline int mywcsprintf(WCHAR* des
 #    define MAXPATHLEN _MAX_PATH
 #  elif defined(CCHMAXPATH)
 #    define MAXPATHLEN CCHMAXPATH
 #  else
 #    define MAXPATHLEN 1024
 #  endif
 #endif
 
+static inline bool NS_tsnprintf(NS_tchar* dest, size_t count,
+                                const NS_tchar* fmt, ...) {
+  va_list varargs;
+  va_start(varargs, fmt);
+  int result = NS_tvsnprintf(dest, count - 1, fmt, varargs);
+  va_end(varargs);
+  dest[count - 1] = NS_T('\0');
+  // The size_t cast of result is safe because result can only be positive after
+  // the first check.
+  return result >= 0 && (size_t)result < count;
+}
+
 #endif