Skip to content

Commit

Permalink
Ensure StrCat means StrCat
Browse files Browse the repository at this point in the history
undefine StrCat from StrCatW after including windows headers that redefine it
Add preprocessor checks StrCat is not defined to something else.
Add presubmit check headers that include shlwapi.h that redefines StrCat are wrapped

Bug: chromium:856536, chromium:817738
Change-Id: Ief9303cbf6fa4f55f671861e49fb1fc747ed59aa
Reviewed-on: https://chromium-review.googlesource.com/1117180
Reviewed-by: Scott Violet <sky@chromium.org>
Reviewed-by: Dirk Pranke <dpranke@chromium.org>
Reviewed-by: Bruce Dawson <brucedawson@chromium.org>
Commit-Queue: Danil Chapovalov <danilchap@chromium.org>
Cr-Commit-Position: refs/heads/master@{#582449}
  • Loading branch information
Danil Chapovalov authored and Commit Bot committed Aug 11, 2018
1 parent 856d335 commit 3518f36
Show file tree
Hide file tree
Showing 60 changed files with 234 additions and 100 deletions.
22 changes: 22 additions & 0 deletions PRESUBMIT.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,28 @@ def _CheckNoIOStreamInHeaders(input_api, output_api):
files) ]
return []

def _CheckNoStrCatRedefines(input_api, output_api):
"""Checks no windows headers with StrCat redefined are included directly."""
files = []
pattern_deny = input_api.re.compile(
r'^#include\s*[<"](shlwapi|atlbase|propvarutil|sphelper).h[">]',
input_api.re.MULTILINE)
pattern_allow = input_api.re.compile(
r'^#include\s"base/win/windows_defines.inc"',
input_api.re.MULTILINE)
for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
contents = input_api.ReadFile(f)
if pattern_deny.search(contents) and not pattern_allow.search(contents):
files.append(f.LocalPath())

if len(files):
return [output_api.PresubmitError(
'Do not #include shlwapi.h, atlbase.h, propvarutil.h or sphelper.h '
'directly since they pollute code with StrCat macro. Instead, '
'include matching header from base/win. See http://crbug.com/856536',
files) ]
return []


def _CheckNoUNIT_TESTInSourceFiles(input_api, output_api):
"""Checks to make sure no source files use UNIT_TEST."""
Expand Down
36 changes: 36 additions & 0 deletions PRESUBMIT_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1858,6 +1858,42 @@ def testFalsePositives(self):
results = PRESUBMIT._CheckUniquePtr(mock_input_api, MockOutputApi())
self.assertEqual(0, len(results))

class CheckNoDirectIncludesHeadersWhichRedefineStrCat(unittest.TestCase):
def testBlocksDirectIncludes(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockFile('dir/foo_win.cc', ['#include "shlwapi.h"']),
MockFile('dir/bar.h', ['#include <propvarutil.h>']),
MockFile('dir/baz.h', ['#include <atlbase.h>']),
MockFile('dir/jumbo.h', ['#include "sphelper.h"']),
]
results = PRESUBMIT._CheckNoStrCatRedefines(mock_input_api, MockOutputApi())
self.assertEquals(1, len(results))
self.assertEquals(4, len(results[0].items))
self.assertTrue('StrCat' in results[0].message)
self.assertTrue('foo_win.cc' in results[0].items[0])
self.assertTrue('bar.h' in results[0].items[1])
self.assertTrue('baz.h' in results[0].items[2])
self.assertTrue('jumbo.h' in results[0].items[3])

def testAllowsToIncludeWrapper(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockFile('dir/baz_win.cc', ['#include "base/win/shlwapi.h"']),
MockFile('dir/baz-win.h', ['#include "base/win/atl.h"']),
]
results = PRESUBMIT._CheckNoStrCatRedefines(mock_input_api, MockOutputApi())
self.assertEquals(0, len(results))

def testAllowsToCreateWrapper(self):
mock_input_api = MockInputApi()
mock_input_api.files = [
MockFile('base/win/shlwapi.h', [
'#include <shlwapi.h>',
'#include "base/win/windows_defines.inc"']),
]
results = PRESUBMIT._CheckNoStrCatRedefines(mock_input_api, MockOutputApi())
self.assertEquals(0, len(results))

class TranslationScreenshotsTest(unittest.TestCase):
# An empty grd file.
Expand Down
7 changes: 7 additions & 0 deletions base/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,7 @@ jumbo_component("base") {
"vlog.cc",
"vlog.h",
"win/async_operation.h",
"win/atl.h",
"win/com_init_check_hook.cc",
"win/com_init_check_hook.h",
"win/com_init_util.cc",
Expand Down Expand Up @@ -1088,6 +1089,7 @@ jumbo_component("base") {
"win/patch_util.h",
"win/process_startup_helper.cc",
"win/process_startup_helper.h",
"win/propvarutil.h",
"win/reference.h",
"win/registry.cc",
"win/registry.h",
Expand Down Expand Up @@ -1116,8 +1118,10 @@ jumbo_component("base") {
"win/scoped_windows_thread_environment.h",
"win/scoped_winrt_initializer.cc",
"win/scoped_winrt_initializer.h",
"win/shlwapi.h",
"win/shortcut.cc",
"win/shortcut.h",
"win/sphelper.h",
"win/startup_information.cc",
"win/startup_information.h",
"win/typed_event_handler.h",
Expand All @@ -1127,6 +1131,9 @@ jumbo_component("base") {
"win/wait_chain.h",
"win/win_util.cc",
"win/win_util.h",
"win/windows_defines.inc",
"win/windows_types.h",
"win/windows_undefines.inc",
"win/windows_version.cc",
"win/windows_version.h",
"win/winrt_storage_util.cc",
Expand Down
2 changes: 1 addition & 1 deletion base/files/file_enumerator_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

#include "base/files/file_enumerator.h"

#include <shlwapi.h>
#include <stdint.h>
#include <string.h>

#include "base/logging.h"
#include "base/threading/thread_restrictions.h"
#include "base/win/shlwapi.h"

namespace base {

Expand Down
5 changes: 3 additions & 2 deletions base/strings/strcat.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
#include "build/build_config.h"

#if defined(OS_WIN)
// To resolve a conflict with Win32 API StrCat macro.
#include "base/win/windows_types.h"
// Guard against conflict with Win32 API StrCat macro:
// check StrCat wasn't and will not be redefined.
#define StrCat StrCat
#endif

namespace base {
Expand Down
2 changes: 1 addition & 1 deletion base/test/test_file_util_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "base/test/test_file_util.h"

#include <aclapi.h>
#include <shlwapi.h>
#include <stddef.h>
#include <wchar.h>
#include <windows.h>
Expand All @@ -20,6 +19,7 @@
#include "base/strings/string_split.h"
#include "base/threading/platform_thread.h"
#include "base/win/scoped_handle.h"
#include "base/win/shlwapi.h"

namespace base {

Expand Down
28 changes: 28 additions & 0 deletions base/win/atl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BASE_WIN_ATL_H_
#define BASE_WIN_ATL_H_

// Check no prior poisonous defines were made.
#include "base/win/windows_defines.inc"
// Undefine before windows header will make the poisonous defines
#include "base/win/windows_undefines.inc"

#ifndef _ATL_NO_EXCEPTIONS
#define _ATL_NO_EXCEPTIONS
#endif

#include <atlbase.h>
#include <atlcom.h>
#include <atlctl.h>
#include <atlhost.h>
#include <atlsecurity.h>
#include <atlwin.h>

// Undefine the poisonous defines
#include "base/win/windows_undefines.inc"
// Check no poisonous defines follow this include
#include "base/win/windows_defines.inc"

#endif // BASE_WIN_ATL_H_
19 changes: 19 additions & 0 deletions base/win/propvarutil.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BASE_WIN_PROPVARUTIL_H_
#define BASE_WIN_PROPVARUTIL_H_

// Check no prior poisonous defines were made.
#include "base/win/windows_defines.inc"
// Undefine before windows header will make the poisonous defines
#include "base/win/windows_undefines.inc"

#include <propvarutil.h>

// Undefine the poisonous defines
#include "base/win/windows_undefines.inc"
// Check no poisonous defines follow this include
#include "base/win/windows_defines.inc"

#endif // BASE_WIN_PROPVARUTIL_H_
2 changes: 1 addition & 1 deletion base/win/registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

#include "base/win/registry.h"

#include <shlwapi.h>
#include <stddef.h>
#include <algorithm>

#include "base/logging.h"
#include "base/macros.h"
#include "base/strings/string_util.h"
#include "base/threading/thread_restrictions.h"
#include "base/win/shlwapi.h"
#include "base/win/windows_version.h"

namespace base {
Expand Down
19 changes: 19 additions & 0 deletions base/win/shlwapi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BASE_WIN_SHLWAPI_H_
#define BASE_WIN_SHLWAPI_H_

// Check no prior poisonous defines were made.
#include "base/win/windows_defines.inc"
// Undefine before windows header will make the poisonous defines
#include "base/win/windows_undefines.inc"

#include <shlwapi.h>

// Undefine the poisonous defines
#include "base/win/windows_undefines.inc"
// Check no poisonous defines follow this include
#include "base/win/windows_defines.inc"

#endif // BASE_WIN_SHLWAPI_H_
19 changes: 19 additions & 0 deletions base/win/sphelper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BASE_WIN_SPHELPER_H_
#define BASE_WIN_SPHELPER_H_

// Check no prior poisonous defines were made.
#include "base/win/windows_defines.inc"
// Undefine before windows header will make the poisonous defines
#include "base/win/windows_undefines.inc"

#include <sphelper.h>

// Undefine the poisonous defines
#include "base/win/windows_undefines.inc"
// Check no poisonous defines follow this include
#include "base/win/windows_defines.inc"

#endif // BASE_WIN_SPHELPER_H_
4 changes: 2 additions & 2 deletions base/win/win_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
#include <mdmregistration.h>
#include <objbase.h>
#include <propkey.h>
#include <propvarutil.h>
#include <psapi.h>
#include <roapi.h>
#include <sddl.h>
#include <setupapi.h>
#include <shellscalingapi.h>
#include <shlwapi.h>
#include <signal.h>
#include <stddef.h>
#include <stdlib.h>
Expand All @@ -45,11 +43,13 @@
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_restrictions.h"
#include "base/win/core_winrt_util.h"
#include "base/win/propvarutil.h"
#include "base/win/registry.h"
#include "base/win/scoped_co_mem.h"
#include "base/win/scoped_handle.h"
#include "base/win/scoped_hstring.h"
#include "base/win/scoped_propvariant.h"
#include "base/win/shlwapi.h"
#include "base/win/win_client_metrics.h"
#include "base/win/windows_version.h"

Expand Down
12 changes: 12 additions & 0 deletions base/win/windows_defines.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This file verifies no poisonous defines from windows headers are present.
// If you add more macros here, add them to windows_undefines.inc too

// This will generate an error if it was defined to something different, or
// if it is defined to something different after.
// Preprocessor will also nicely point to header that defined it differently.
#define StrCat StrCat

1 change: 0 additions & 1 deletion base/win/windows_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,5 @@ WINBASEAPI VOID WINAPI SetLastError(_In_ DWORD dwErrCode);
#define SendMessageCallback SendMessageCallbackW
#define SetCurrentDirectory SetCurrentDirectoryW
#define StartService StartServiceW
#define StrCat StrCatW

#endif // BASE_WIN_WINDOWS_TYPES_H
9 changes: 9 additions & 0 deletions base/win/windows_undefines.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This file undefines poisonous defines from windows headers.
// If you add more macros here, add them to windows_defines.inc too

#undef StrCat

2 changes: 1 addition & 1 deletion chrome/app/chrome_main_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@
#include "ui/base/ui_base_switches.h"

#if defined(OS_WIN)
#include <atlbase.h>
#include <malloc.h>

#include <algorithm>

#include "base/debug/close_handle_hook_win.h"
#include "base/win/atl.h"
#include "chrome/browser/downgrade/user_data_downgrade.h"
#include "chrome/child/v8_breakpad_support_win.h"
#include "chrome/common/child_process_logging.h"
Expand Down
2 changes: 1 addition & 1 deletion chrome/app/main_dll_loader_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "chrome/app/main_dll_loader_win.h"

#include <windows.h> // NOLINT
#include <shlwapi.h> // NOLINT
#include <stddef.h>
#include <stdint.h>
#include <userenv.h> // NOLINT
Expand All @@ -28,6 +27,7 @@
#include "base/strings/utf_string_conversions.h"
#include "base/trace_event/trace_event.h"
#include "base/win/scoped_handle.h"
#include "base/win/shlwapi.h"
#include "base/win/windows_version.h"
#include "chrome/app/chrome_watcher_client_win.h"
#include "chrome/app/chrome_watcher_command_line_win.h"
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/conflicts/uninstall_application_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include "chrome/browser/conflicts/uninstall_application_win.h"

#include <atlbase.h>
#include <wrl/client.h>

#include <memory>
Expand All @@ -22,6 +21,7 @@
#include "base/strings/string_util.h"
#include "base/synchronization/lock.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "base/win/atl.h"
#include "base/win/scoped_variant.h"
#include "chrome/browser/win/automation_controller.h"
#include "chrome/browser/win/ui_automation_util.h"
Expand Down
3 changes: 1 addition & 2 deletions chrome/browser/google/google_update_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

#include "chrome/browser/google/google_update_win.h"

#include <atlbase.h>
#include <atlcom.h>
#include <objbase.h>
#include <stdint.h>
#include <string.h>
Expand Down Expand Up @@ -33,6 +31,7 @@
#include "base/threading/sequenced_task_runner_handle.h"
#include "base/time/time.h"
#include "base/version.h"
#include "base/win/atl.h"
#include "base/win/scoped_bstr.h"
#include "chrome/common/url_constants.h"
#include "chrome/grit/generated_resources.h"
Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/google/google_update_win_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
#include "chrome/browser/google/google_update_win.h"

#include <windows.h>
#include <atlbase.h>
#include <atlcom.h>

#include <wrl/client.h>

#include <memory>
Expand All @@ -24,6 +23,7 @@
#include "base/test/test_simple_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/version.h"
#include "base/win/atl.h"
#include "base/win/registry.h"
#include "chrome/common/chrome_version.h"
#include "chrome/install_static/test/scoped_install_details.h"
Expand Down
Loading

0 comments on commit 3518f36

Please sign in to comment.