Title: [267917] trunk
Revision
267917
Author
[email protected]
Date
2020-10-02 23:00:27 -0700 (Fri, 02 Oct 2020)

Log Message

std::once_flag must be allocated in static storage
https://bugs.webkit.org/show_bug.cgi?id=217271

Reviewed by Mark Lam.

Source/WebCore:

If the std::once_flag is a non-static variable, it will not prevent us from calling it multiple times.

* platform/text/TextCodecSingleByte.cpp:
(WebCore::tableForEncoding):

Tools:

Added cpplint rule for non-static std::once_flag / dispatch_once_t.

* Scripts/webkitpy/style/checkers/cpp.py:
(check_once_flag):
(check_style):
(CppChecker):
* Scripts/webkitpy/style/checkers/cpp_unittest.py:
(WebKitStyleTest.test_once_flag):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (267916 => 267917)


--- trunk/Source/WebCore/ChangeLog	2020-10-03 03:55:47 UTC (rev 267916)
+++ trunk/Source/WebCore/ChangeLog	2020-10-03 06:00:27 UTC (rev 267917)
@@ -1,3 +1,15 @@
+2020-10-02  Yusuke Suzuki  <[email protected]>
+
+        std::once_flag must be allocated in static storage
+        https://bugs.webkit.org/show_bug.cgi?id=217271
+
+        Reviewed by Mark Lam.
+
+        If the std::once_flag is a non-static variable, it will not prevent us from calling it multiple times.
+
+        * platform/text/TextCodecSingleByte.cpp:
+        (WebCore::tableForEncoding):
+
 2020-10-02  Wenson Hsieh  <[email protected]>
 
         [ iOS ] ASSERTION FAILED: ScriptDisallowedScope::InMainThread::isEventDispatchAllowedInSubtree

Modified: trunk/Source/WebCore/platform/text/TextCodecSingleByte.cpp (267916 => 267917)


--- trunk/Source/WebCore/platform/text/TextCodecSingleByte.cpp	2020-10-03 03:55:47 UTC (rev 267916)
+++ trunk/Source/WebCore/platform/text/TextCodecSingleByte.cpp	2020-10-03 06:00:27 UTC (rev 267917)
@@ -177,7 +177,7 @@
     // FIXME: With the C++20 version of std::count, we should be able to change this from const to constexpr and compute the size at compile time.
     static const auto size = std::size(decodeTable) - std::count(std::begin(decodeTable), std::end(decodeTable), replacementCharacter);
     static const SingleByteEncodeTableEntry* entries;
-    std::once_flag once;
+    static std::once_flag once;
     std::call_once(once, [&] {
         auto* mutableEntries = new SingleByteEncodeTableEntry[size];
         size_t j = 0;

Modified: trunk/Tools/ChangeLog (267916 => 267917)


--- trunk/Tools/ChangeLog	2020-10-03 03:55:47 UTC (rev 267916)
+++ trunk/Tools/ChangeLog	2020-10-03 06:00:27 UTC (rev 267917)
@@ -1,3 +1,19 @@
+2020-10-02  Yusuke Suzuki  <[email protected]>
+
+        std::once_flag must be allocated in static storage
+        https://bugs.webkit.org/show_bug.cgi?id=217271
+
+        Reviewed by Mark Lam.
+
+        Added cpplint rule for non-static std::once_flag / dispatch_once_t.
+
+        * Scripts/webkitpy/style/checkers/cpp.py:
+        (check_once_flag):
+        (check_style):
+        (CppChecker):
+        * Scripts/webkitpy/style/checkers/cpp_unittest.py:
+        (WebKitStyleTest.test_once_flag):
+
 2020-10-02  Kate Cheney  <[email protected]>
 
         Multiple calls to suspend media playback for the same page may result in resuming media playback too soon

Modified: trunk/Tools/Scripts/webkitpy/style/checkers/cpp.py (267916 => 267917)


--- trunk/Tools/Scripts/webkitpy/style/checkers/cpp.py	2020-10-03 03:55:47 UTC (rev 267916)
+++ trunk/Tools/Scripts/webkitpy/style/checkers/cpp.py	2020-10-03 06:00:27 UTC (rev 267917)
@@ -2506,6 +2506,29 @@
               'enum members should use InterCaps with an initial capital letter or initial \'k\' for C-style enums.')
 
 
+def check_once_flag(clean_lines, line_number, enum_state, error):
+    """Looks for non-static std::once_flag / dispatch_once_t.
+
+    Args:
+      clean_lines: A CleansedLines instance containing the file.
+      line_number: The number of the line to check.
+      enum_state: A _EnumState instance which maintains enum declaration state.
+      error: The function to call with any errors found.
+    """
+
+    line = clean_lines.elided[line_number]  # Get rid of comments and strings.
+
+    using_std_once_flag = search(r'std::once_flag|dispatch_once_t', line)
+    if not using_std_once_flag:
+        return
+
+    using_std_once_flag_with_static = search(r'static\s+(?:std::once_flag|dispatch_once_t)', line)
+    if using_std_once_flag_with_static:
+        return
+
+    error(line_number, 'runtime/once_flag', 4, "std::once_flag / dispatch_once_t should be in `static` storage.")
+
+
 def check_directive_indentation(clean_lines, line_number, file_state, error):
     """Looks for indentation of preprocessor directives.
 
@@ -3342,6 +3365,7 @@
     check_soft_link_class_alloc(clean_lines, line_number, error)
     check_indentation_amount(clean_lines, line_number, error)
     check_enum_casing(clean_lines, line_number, enum_state, error)
+    check_once_flag(clean_lines, line_number, file_state, error)
     check_min_versions_of_wk_api_available(clean_lines, line_number, error)
 
 
@@ -4514,6 +4538,7 @@
         'runtime/lock_guard',
         'runtime/max_min_macros',
         'runtime/memset',
+        'runtime/once_flag',
         'runtime/printf',
         'runtime/printf_format',
         'runtime/references',

Modified: trunk/Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py (267916 => 267917)


--- trunk/Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py	2020-10-03 03:55:47 UTC (rev 267916)
+++ trunk/Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py	2020-10-03 06:00:27 UTC (rev 267917)
@@ -5623,6 +5623,75 @@
             "  [runtime/lock_guard] [4]",
             'foo.mm')
 
+    def test_once_flag(self):
+        self.assert_lint(
+            'static std::once_flag onceKey;',
+            '',
+            'foo.cpp')
+
+        self.assert_lint(
+            'std::once_flag onceKey;',
+            "std::once_flag / dispatch_once_t should be in `static` storage."
+            "  [runtime/once_flag] [4]",
+            'foo.cpp')
+
+        self.assert_lint(
+            '    std::once_flag onceKey;',
+            "std::once_flag / dispatch_once_t should be in `static` storage."
+            "  [runtime/once_flag] [4]",
+            'foo.cpp')
+
+        self.assert_lint(
+            'static std::once_flag onceKey;',
+            '',
+            'foo.mm')
+
+        self.assert_lint(
+            'std::once_flag onceKey;',
+            "std::once_flag / dispatch_once_t should be in `static` storage."
+            "  [runtime/once_flag] [4]",
+            'foo.mm')
+
+        self.assert_lint(
+            '    std::once_flag onceKey;',
+            "std::once_flag / dispatch_once_t should be in `static` storage."
+            "  [runtime/once_flag] [4]",
+            'foo.mm')
+
+        self.assert_lint(
+            'static dispatch_once_t onceKey;',
+            '',
+            'foo.cpp')
+
+        self.assert_lint(
+            'dispatch_once_t onceKey;',
+            "std::once_flag / dispatch_once_t should be in `static` storage."
+            "  [runtime/once_flag] [4]",
+            'foo.cpp')
+
+        self.assert_lint(
+            '    dispatch_once_t onceKey;',
+            "std::once_flag / dispatch_once_t should be in `static` storage."
+            "  [runtime/once_flag] [4]",
+            'foo.cpp')
+
+        self.assert_lint(
+            'static dispatch_once_t onceKey;',
+            '',
+            'foo.mm')
+
+        self.assert_lint(
+            'dispatch_once_t onceKey;',
+            "std::once_flag / dispatch_once_t should be in `static` storage."
+            "  [runtime/once_flag] [4]",
+            'foo.mm')
+
+        self.assert_lint(
+            '    dispatch_once_t onceKey;',
+            "std::once_flag / dispatch_once_t should be in `static` storage."
+            "  [runtime/once_flag] [4]",
+            'foo.mm')
+
     def test_ctype_fucntion(self):
         self.assert_lint(
             'int i = isascii(8);',
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to