author Mike Conley <>
Fri, 10 Apr 2015 13:45:22 -0400
changeset 257771 40a21375b99be21a55df2a7c415ccf240715922f
parent 233685 9dffe0f511c9366173592c09ebf5d1f432320d2f
child 259758 bf1d0c9242d9ac334c7f7521789840a7c711b418
permissions -rw-r--r--
Bug 1152864 - Disable unsafe CPOW warnings when running mochitests by default, and add option to re-enable. r=ahal. --enable-cpow-warnings can now be passed when running a mochitest to re-enable the warnings, should one wish to do that.

/* 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 */

// This file is a .cpp file meant to be included in nsBrowserApp.cpp and other
// similar bootstrap code. It converts wide-character windows wmain into UTF-8
// narrow-character strings.

#ifndef XP_WIN
#error This file only makes sense on Windows.

#include "nsUTF8Utils.h"

#include "nsSetDllDirectory.h"

#if defined(MOZ_METRO) || defined(__GNUC__)

#include "WindowsCrtPatch.h"

#ifdef __MINGW32__

/* MingW currently does not implement a wide version of the
   startup routines.  Workaround is to implement something like
   it ourselves.  See bug 411826 */

#include <shellapi.h>

int wmain(int argc, WCHAR **argv);

int main(int argc, char **argv)
  LPWSTR commandLine = GetCommandLineW();
  int argcw = 0;
  LPWSTR *argvw = CommandLineToArgvW(commandLine, &argcw);
  if (!argvw)
    return 127;

  int result = wmain(argcw, argvw);
  return result;
#endif /* __MINGW32__ */

#define main NS_internal_main

int main(int argc, char **argv);
int main(int argc, char **argv, char **envp);

static char*
AllocConvertUTF16toUTF8(char16ptr_t arg)
  // be generous... UTF16 units can expand up to 3 UTF8 units
  int len = wcslen(arg);
  char *s = new char[len * 3 + 1];
  if (!s)
    return nullptr;

  ConvertUTF16toUTF8 convert(s);
  convert.write(arg, len);
  return s;

static void
FreeAllocStrings(int argc, char **argv)
  while (argc) {
    delete [] argv[argc];

  delete [] argv;

int wmain(int argc, WCHAR **argv)
#if !defined(XRE_DONT_SUPPORT_XPSP2)


  char **argvConverted = new char*[argc + 1];
  if (!argvConverted)
    return 127;

  for (int i = 0; i < argc; ++i) {
    argvConverted[i] = AllocConvertUTF16toUTF8(argv[i]);
    if (!argvConverted[i]) {
      return 127;
  argvConverted[argc] = nullptr;

  // need to save argvConverted copy for later deletion.
  char **deleteUs = new char*[argc+1];
  if (!deleteUs) {
    FreeAllocStrings(argc, argvConverted);
    return 127;
  for (int i = 0; i < argc; i++)
    deleteUs[i] = argvConverted[i];
  int result = main(argc, argvConverted);
  // Force creation of the multibyte _environ variable.
  int result = main(argc, argvConverted, _environ);

  delete[] argvConverted;
  FreeAllocStrings(argc, deleteUs);

  return result;