Repository: electron/electron
Pull request: build: re-enable unsafe buffer checks

#### Description of Change

Re-enable the unsafe-buffer checks that were disabled by https://github.com/electron/electron/pull/45334/commits/1e9acec7a0fc9bcece47075c31e7e5d8daaf979f in the [134.0.6988.0 Chromium roll (#45334)](https://github.com/electron/electron/pull/45334).

Fixes #45411.

#### Checklist

- [x] PR description included and stakeholders cc'd
- [x] `npm test` passes
- [x] [PR release notes](https://github.com/electron/clerk/blob/main/README.md) describe the change in a way relevant to app developers, and are [capitalized, punctuated, and past tense](https://github.com/electron/clerk/blob/main/README.md#examples).

#### Maintainer Notes

I'd never poked around in Chromium's Clang plugins before, so in case I (or another maintainer) has to do it again:  I tracked this down by patching `tools/clang/plugins/UnsafeBuffersPlugin.cpp` with [these new log statements](https://github.com/user-attachments/files/18925198/UnsafeBuffersPlugin.diff.gz) and then rebuilding clang by following [these instructions]( https://chromium.googlesource.com/chromium/src/+/main/docs/clang.md#using-plugins). Then I used that build of Clang to rebuild Electron. To ensure the patched clang got used, I passed the `--offline` flag to `reclient/rewrapper`.

Warnings in `net/cookies/parsed_cookie.cc`:

```
../../net/cookies/parsed_cookie.cc:90:45: error: unsafe pointer arithmetic [-Werror,-Wunsafe-buffer-usage]
   90 |   for (; *it != end && **it != character; ++(*it)) {
      |                                             ^~~~~
../../net/cookies/parsed_cookie.cc:90:45: note: See //docs/unsafe_buffers.md for help.
/home/charles/electron/gn/main/src/tools/clang/plugins/UnsafeBuffersPlugin.cpp:238 filename ../../net/cookies/parsed_cookie.cc
243 returning from cache: 1
../../net/cookies/parsed_cookie.cc:100:49: error: unsafe pointer arithmetic [-Werror,-Wunsafe-buffer-usage]
  100 |   for (; *it != end && !CharIsA(**it, chars); ++(*it)) {
      |                                                 ^~~~~
../../net/cookies/parsed_cookie.cc:100:49: note: See //docs/unsafe_buffers.md for help.
/home/charles/electron/gn/main/src/tools/clang/plugins/UnsafeBuffersPlugin.cpp:238 filename ../../net/cookies/parsed_cookie.cc
243 returning from cache: 1
../../net/cookies/parsed_cookie.cc:109:48: error: unsafe pointer arithmetic [-Werror,-Wunsafe-buffer-usage]
  109 |   for (; *it != end && CharIsA(**it, chars); ++(*it)) {
      |                                                ^~~~~
../../net/cookies/parsed_cookie.cc:109:48: note: See //docs/unsafe_buffers.md for help.
/home/charles/electron/gn/main/src/tools/clang/plugins/UnsafeBuffersPlugin.cpp:238 filename ../../net/cookies/parsed_cookie.cc
243 returning from cache: 1
../../net/cookies/parsed_cookie.cc:116:48: error: unsafe pointer arithmetic [-Werror,-Wunsafe-buffer-usage]
  116 |   for (; *it != end && CharIsA(**it, chars); --(*it)) {
      |                                                ^~~~~
../../net/cookies/parsed_cookie.cc:116:48: note: See //docs/unsafe_buffers.md for help.
/home/charles/electron/gn/main/src/tools/clang/plugins/UnsafeBuffersPlugin.cpp:238 filename ../../net/cookies/parsed_cookie.cc
243 returning from cache: 1
../../net/cookies/parsed_cookie.cc:308:13: error: unsafe pointer arithmetic [-Werror,-Wunsafe-buffer-usage]
  308 |     end = s.begin() + term_pos;
      |           ~~^~~~~~~
../../net/cookies/parsed_cookie.cc:308:13: note: See //docs/unsafe_buffers.md for help.
/home/charles/electron/gn/main/src/tools/clang/plugins/UnsafeBuffersPlugin.cpp:238 filename ../../net/cookies/parsed_cookie.cc
243 returning from cache: 1
../../net/cookies/parsed_cookie.cc:337:7: error: unsafe pointer arithmetic [-Werror,-Wunsafe-buffer-usage]
  337 |     --(*it);                  // Go back before the token separator.
      |       ^~~~~
../../net/cookies/parsed_cookie.cc:337:7: note: See //docs/unsafe_buffers.md for help.
/home/charles/electron/gn/main/src/tools/clang/plugins/UnsafeBuffersPlugin.cpp:238 filename ../../net/cookies/parsed_cookie.cc
243 returning from cache: 1
../../net/cookies/parsed_cookie.cc:341:7: error: unsafe pointer arithmetic [-Werror,-Wunsafe-buffer-usage]
  341 |     ++(*it);
      |       ^~~~~
../../net/cookies/parsed_cookie.cc:341:7: note: See //docs/unsafe_buffers.md for help.
/home/charles/electron/gn/main/src/tools/clang/plugins/UnsafeBuffersPlugin.cpp:238 filename ../../net/cookies/parsed_cookie.cc
243 returning from cache: 1
../../net/cookies/parsed_cookie.cc:371:7: error: unsafe pointer arithmetic [-Werror,-Wunsafe-buffer-usage]
  371 |     --(*value_end);
      |       ^~~~~~~~~~~~
../../net/cookies/parsed_cookie.cc:371:7: note: See //docs/unsafe_buffers.md for help.
/home/charles/electron/gn/main/src/tools/clang/plugins/UnsafeBuffersPlugin.cpp:238 filename ../../net/cookies/parsed_cookie.cc
243 returning from cache: 1
../../net/cookies/parsed_cookie.cc:375:7: error: unsafe pointer arithmetic [-Werror,-Wunsafe-buffer-usage]
  375 |     ++(*value_end);
      |       ^~~~~~~~~~~~
../../net/cookies/parsed_cookie.cc:375:7: note: See //docs/unsafe_buffers.md for help.
/home/charles/electron/gn/main/src/tools/clang/plugins/UnsafeBuffersPlugin.cpp:238 filename ../../net/cookies/parsed_cookie.cc
243 returning from cache: 1
../../net/cookies/parsed_cookie.cc:571:9: error: unsafe pointer arithmetic [-Werror,-Wunsafe-buffer-usage]
  571 |       ++it;  // Skip past the '='.
      |         ^~
../../net/cookies/parsed_cookie.cc:571:9: note: See //docs/unsafe_buffers.md for help.
/home/charles/electron/gn/main/src/tools/clang/plugins/UnsafeBuffersPlugin.cpp:238 filename ../../net/cookies/parsed_cookie.cc
243 returning from cache: 1
../../net/cookies/parsed_cookie.cc:634:9: error: unsafe pointer arithmetic [-Werror,-Wunsafe-buffer-usage]
  634 |       ++it;
      |         ^~
../../net/cookies/parsed_cookie.cc:634:9: note: See //docs/unsafe_buffers.md for help.
```

#### Release Notes

Notes: none.

Changed files:
filename: build/args/all.gn
patch: @@ -74,10 +74,4 @@ enterprise_cloud_content_analysis = false
 # https://issues.chromium.org/issues/40943039
 content_enable_legacy_ipc = true
 
-# Electron has its own unsafe-buffers enforcement directories.
-# TODO: clang_unsafe_buffers_paths = "//electron/electron_unsafe_buffers_paths.txt"
-#
-# Disables unsafe-buffers-usage plugin due to incompatibilities with our reclient implementation
-# Ref: https://chromium-review.googlesource.com/c/chromium/src/+/5426599
-# Ref: https://github.com/electron/electron/commit/8e20f16ea35eeaeb149ae63bad3703d782665f6a
-clang_unsafe_buffers_paths = ""
+clang_unsafe_buffers_paths = "//electron/electron_unsafe_buffers_paths.txt"

filename: patches/chromium/.patches
patch: @@ -140,3 +140,4 @@ ignore_parse_errors_for_pkey_appusermodel_toastactivatorclsid.patch
 feat_add_signals_when_embedder_cleanup_callbacks_run_for.patch
 feat_separate_content_settings_callback_for_sync_and_async_clipboard.patch
 fix_win32_synchronous_spellcheck.patch
+chore_remove_conflicting_allow_unsafe_libc_calls.patch

filename: patches/chromium/chore_remove_conflicting_allow_unsafe_libc_calls.patch
patch: @@ -0,0 +1,49 @@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: Charles Kerr <charles@charleskerr.com>
+Date: Sat, 22 Feb 2025 13:15:39 -0600
+Subject: chore: remove conflicting allow_unsafe_libc_calls
+
+We want builds to fail if a buffer warning comes from Electron code but
+not from code that we don't maintain (e.g. upstream Chromium code), so
+//electron/electron_unsafe_buffer_paths.txt turns off Chromium warnings.
+
+There are some upstream files that generate warnings *and* also have
+pragmas that override //electron/electron_unsafe_buffer_paths.txt,
+forcing them to be tested. This breaks our build.
+
+Files can be removed from this patch when upstream either removes the
+pragma or fixes the other warnings. This patch can be removed when no
+files are left.
+
+diff --git a/net/cookies/parsed_cookie.cc b/net/cookies/parsed_cookie.cc
+index 7d5d0106a3675b3fa21b0e00a755f5c0ed11c87b..d26c645d70b54b31815c8140954ee6d0a34fa8af 100644
+--- a/net/cookies/parsed_cookie.cc
++++ b/net/cookies/parsed_cookie.cc
+@@ -2,11 +2,6 @@
+ // Use of this source code is governed by a BSD-style license that can be
+ // found in the LICENSE file.
+ 
+-#ifdef UNSAFE_BUFFERS_BUILD
+-// TODO(crbug.com/390223051): Remove C-library calls to fix the errors.
+-#pragma allow_unsafe_libc_calls
+-#endif
+-
+ // Portions of this code based on Mozilla:
+ //   (netwerk/cookie/src/nsCookieService.cpp)
+ /* ***** BEGIN LICENSE BLOCK *****
+diff --git a/net/http/http_response_headers.cc b/net/http/http_response_headers.cc
+index 34ef2be4415825254c41d5970be33b1195a9a381..4a3462664c22f6d367e02ac03551c22c0db52a4d 100644
+--- a/net/http/http_response_headers.cc
++++ b/net/http/http_response_headers.cc
+@@ -2,11 +2,6 @@
+ // Use of this source code is governed by a BSD-style license that can be
+ // found in the LICENSE file.
+ 
+-#ifdef UNSAFE_BUFFERS_BUILD
+-// TODO(crbug.com/390223051): Remove C-library calls to fix the errors.
+-#pragma allow_unsafe_libc_calls
+-#endif
+-
+ // The rules for header parsing were borrowed from Firefox:
+ // http://lxr.mozilla.org/seamonkey/source/netwerk/protocol/http/src/nsHttpResponseHead.cpp
+ // The rules for parsing content-types were also borrowed from Firefox: