Merge pull request #1554 from 1234binpig:fix-help-output

PiperOrigin-RevId: 1029840889
diff --git a/.github/workflows/build_test.yml b/.github/workflows/build_test.yml
index dcb1184..14af2b9 100644
--- a/.github/workflows/build_test.yml
+++ b/.github/workflows/build_test.yml
@@ -225,7 +225,7 @@
     steps:
 
     - name: Harden Runner
-      uses: step-security/harden-runner@dc2769abcc0fcfbadfc1df54081c1608579ef6b2 # v2.22.2
+      uses: step-security/harden-runner@aebdcf6156ab6691adf63218240e476564a1ed4c # v2.23.0
       with:
         egress-policy: audit
 
@@ -367,7 +367,7 @@
         # cd integration
         # mvn -B verify
 
-    - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
+    - uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
       if: ${{ matrix.build_system == 'python' }}
       with:
         python-version: ${{ matrix.python_version }}
@@ -390,13 +390,13 @@
         os: [ubuntu-latest, ubuntu-24.04-arm, macos-latest, windows-latest, windows-11-arm]
     steps:
       - name: Harden Runner
-        uses: step-security/harden-runner@dc2769abcc0fcfbadfc1df54081c1608579ef6b2
+        uses: step-security/harden-runner@aebdcf6156ab6691adf63218240e476564a1ed4c
         with:
           egress-policy: audit
       - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1
         with:
           persist-credentials: false
-      - uses: pypa/cibuildwheel@f11181315571b24e4a3c67f1a828532793f2df55
+      - uses: pypa/cibuildwheel@f9b966cfadf4109556cadc5bb39ddb72d0ce664e
         env:
           CIBW_BUILD: cp315-*
           CIBW_ARCHS_MACOS: x86_64 arm64
diff --git a/.github/workflows/build_test_wasm.yml b/.github/workflows/build_test_wasm.yml
index c04f773..7efebd2 100644
--- a/.github/workflows/build_test_wasm.yml
+++ b/.github/workflows/build_test_wasm.yml
@@ -35,7 +35,7 @@
     steps:
 
     - name: Harden Runner
-      uses: step-security/harden-runner@dc2769abcc0fcfbadfc1df54081c1608579ef6b2 # v2.22.2
+      uses: step-security/harden-runner@aebdcf6156ab6691adf63218240e476564a1ed4c # v2.23.0
       with:
         egress-policy: audit
 
diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml
index 9c15bf8..8a0ae69 100644
--- a/.github/workflows/codeql.yml
+++ b/.github/workflows/codeql.yml
@@ -35,7 +35,7 @@
     steps:
 
     - name: Harden Runner
-      uses: step-security/harden-runner@dc2769abcc0fcfbadfc1df54081c1608579ef6b2 # v2.22.2
+      uses: step-security/harden-runner@aebdcf6156ab6691adf63218240e476564a1ed4c # v2.23.0
       with:
         egress-policy: audit
 
@@ -46,7 +46,7 @@
 
     # Initializes the CodeQL tools for scanning.
     - name: Initialize CodeQL
-      uses: github/codeql-action/init@6f2ac81f5cd81fe351a197ba0b00d91d03cd2aee # v4.39.0
+      uses: github/codeql-action/init@33d7b85da1a04171e211befe2c739303b9a4c066 # v4.39.3
       with:
         languages: ${{ matrix.language }}
         # CodeQL is currently crashing on files with large lists:
@@ -58,7 +58,7 @@
 
     - if: matrix.language == 'cpp'
       name: Build CPP
-      uses: github/codeql-action/autobuild@6f2ac81f5cd81fe351a197ba0b00d91d03cd2aee # v4.39.0
+      uses: github/codeql-action/autobuild@33d7b85da1a04171e211befe2c739303b9a4c066 # v4.39.3
 
     - if: matrix.language == 'cpp' || matrix.language == 'java'
       name: Build Java
@@ -72,7 +72,7 @@
 
     - if: matrix.language == 'javascript'
       name: Build JS
-      uses: github/codeql-action/autobuild@6f2ac81f5cd81fe351a197ba0b00d91d03cd2aee # v4.39.0
+      uses: github/codeql-action/autobuild@33d7b85da1a04171e211befe2c739303b9a4c066 # v4.39.3
 
     - if: matrix.language == 'cpp' || matrix.language == 'python'
       name: Build Python
@@ -82,7 +82,7 @@
         python setup.py build_ext
 
     - name: Perform CodeQL Analysis
-      uses: github/codeql-action/analyze@6f2ac81f5cd81fe351a197ba0b00d91d03cd2aee # v4.39.0
+      uses: github/codeql-action/analyze@33d7b85da1a04171e211befe2c739303b9a4c066 # v4.39.3
       with:
         category: "/language:${{matrix.language}}"
         ref: "${{ github.ref != 'master' && github.ref || '/refs/heads/master' }}"
diff --git a/.github/workflows/fuzz.yml b/.github/workflows/fuzz.yml
index 053455d..0789dd7 100644
--- a/.github/workflows/fuzz.yml
+++ b/.github/workflows/fuzz.yml
@@ -23,7 +23,7 @@
     steps:
 
     - name: Harden Runner
-      uses: step-security/harden-runner@dc2769abcc0fcfbadfc1df54081c1608579ef6b2 # v2.22.2
+      uses: step-security/harden-runner@aebdcf6156ab6691adf63218240e476564a1ed4c # v2.23.0
       with:
         egress-policy: audit
 
diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml
index 078c72a..aaf0f21 100644
--- a/.github/workflows/lint.yml
+++ b/.github/workflows/lint.yml
@@ -30,7 +30,7 @@
     steps:
 
     - name: Harden Runner
-      uses: step-security/harden-runner@dc2769abcc0fcfbadfc1df54081c1608579ef6b2 # v2.22.2
+      uses: step-security/harden-runner@aebdcf6156ab6691adf63218240e476564a1ed4c # v2.23.0
       with:
         egress-policy: audit
 
diff --git a/.github/workflows/publish_to_bcr.yaml b/.github/workflows/publish_to_bcr.yaml
index 17b95fd..0bcc79b 100644
--- a/.github/workflows/publish_to_bcr.yaml
+++ b/.github/workflows/publish_to_bcr.yaml
@@ -18,7 +18,7 @@
 
 jobs:
   publish:
-    uses: bazel-contrib/publish-to-bcr/.github/workflows/publish.yaml@fa5bb3803983c621c90721669ee7857afe139836 # v1.5.1
+    uses: bazel-contrib/publish-to-bcr/.github/workflows/publish.yaml@1ae09341a6014e9bea052879c42392ac6b8b632c # v1.7.0
     with:
       tag_name: ${{ inputs.tag_name }}
       registry_fork: ${{ inputs.registry_fork }}
diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml
index c782280..5d49109 100644
--- a/.github/workflows/release.yaml
+++ b/.github/workflows/release.yaml
@@ -67,7 +67,7 @@
     steps:
 
     - name: Harden Runner
-      uses: step-security/harden-runner@dc2769abcc0fcfbadfc1df54081c1608579ef6b2 # v2.22.2
+      uses: step-security/harden-runner@aebdcf6156ab6691adf63218240e476564a1ed4c # v2.23.0
       with:
         egress-policy: audit
 
@@ -152,7 +152,7 @@
     steps:
 
     - name: Harden Runner
-      uses: step-security/harden-runner@dc2769abcc0fcfbadfc1df54081c1608579ef6b2 # v2.22.2
+      uses: step-security/harden-runner@aebdcf6156ab6691adf63218240e476564a1ed4c # v2.23.0
       with:
         egress-policy: audit
 
@@ -181,8 +181,8 @@
     runs-on: [ubuntu-latest]
     permissions:
       contents: write # upload assets
-      id-token: write
-      attestations: write
+      id-token: write # Request an OIDC token for artifact signing
+      attestations: write # Publish provenance attestations
 
     steps:
     - name: Checkout the source
@@ -220,7 +220,7 @@
     steps:
 
     - name: Harden Runner
-      uses: step-security/harden-runner@dc2769abcc0fcfbadfc1df54081c1608579ef6b2 # v2.22.2
+      uses: step-security/harden-runner@aebdcf6156ab6691adf63218240e476564a1ed4c # v2.23.0
       with:
         egress-policy: audit
 
diff --git a/.github/workflows/scorecard.yml b/.github/workflows/scorecard.yml
index 46e9f8e..4336a59 100644
--- a/.github/workflows/scorecard.yml
+++ b/.github/workflows/scorecard.yml
@@ -32,7 +32,7 @@
 
     steps:
       - name: Harden Runner
-        uses: step-security/harden-runner@dc2769abcc0fcfbadfc1df54081c1608579ef6b2 # v2.22.2
+        uses: step-security/harden-runner@aebdcf6156ab6691adf63218240e476564a1ed4c # v2.23.0
         with:
           egress-policy: audit
 
@@ -72,6 +72,6 @@
 
       # Upload the results to GitHub's code scanning dashboard.
       - name: "Upload to code-scanning"
-        uses: github/codeql-action/upload-sarif@6f2ac81f5cd81fe351a197ba0b00d91d03cd2aee # v4.39.0
+        uses: github/codeql-action/upload-sarif@33d7b85da1a04171e211befe2c739303b9a4c066 # v4.39.3
         with:
           sarif_file: results.sarif
diff --git a/c/common/static_init.h b/c/common/static_init.h
index 12744de..7656fde 100644
--- a/c/common/static_init.h
+++ b/c/common/static_init.h
@@ -14,7 +14,8 @@
    In C or before C++11 it is possible to call `BrotliXxxLazyStaticInitInner`
    on start-up path and then `BrotliEncoderLazyStaticInit` is could be no-op;
    another option is to use available thread execution controls to meet the
-   requirements. For possible C++11 implementation see static_init_lazy.cc.
+   requirements. For possible C++11 implementations see enc/static_init_lazy.cc
+   and dec/dec_static_init_lazy.cc.
 */
 
 #ifndef THIRD_PARTY_BROTLI_COMMON_STATIC_INIT_H_
diff --git a/c/dec/dec_static_init.c b/c/dec/dec_static_init.c
index cc3fb1c..7e80998 100644
--- a/c/dec/dec_static_init.c
+++ b/c/dec/dec_static_init.c
@@ -44,6 +44,7 @@
 #elif (BROTLI_STATIC_INIT == BROTLI_STATIC_INIT_EARLY)
   return kEarlyInitOk;
 #else
+  BrotliDecoderLazyStaticInit();
   return kLazyInitOk;
 #endif
 }
diff --git a/c/dec/dec_static_init_lazy.cc b/c/dec/dec_static_init_lazy.cc
new file mode 100644
index 0000000..c865d7f
--- /dev/null
+++ b/c/dec/dec_static_init_lazy.cc
@@ -0,0 +1,32 @@
+/* Copyright 2025 Google Inc. All Rights Reserved.
+
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+#include "common/platform.h"
+#include "common/static_init.h"
+#include "dec_static_init.h"
+
+#if (BROTLI_STATIC_INIT != BROTLI_STATIC_INIT_LAZY)
+#error "BROTLI_STATIC_INIT should be BROTLI_STATIC_INIT_LAZY"
+#else
+void BrotliDecoderLazyStaticInit(void) {
+  /* From https://en.cppreference.com/w/cpp/language/storage_duration.html:
+     ### Static block variables ###
+     Block variables with static or thread (since C++11) storage duration are
+     initialized the first time control passes through their declaration...
+     On all further calls, the declaration is skipped...
+     If multiple threads attempt to initialize the same static local variable
+     concurrently, the initialization occurs exactly once...
+     Usual implementations of this feature use variants of the double-checked
+     locking pattern, which reduces runtime overhead for already-initialized
+     local statics to a single non-atomic boolean comparison.
+   */
+  static bool ok = [](){
+    BrotliDecoderLazyStaticInitInner();
+    return true;
+  }();
+  if (!ok) BROTLI_CRASH();
+}
+#endif  /* BROTLI_STATIC_INIT_LAZY */
diff --git a/c/dec/decode.c b/c/dec/decode.c
index aa73cc3..f746e16 100644
--- a/c/dec/decode.c
+++ b/c/dec/decode.c
@@ -15,7 +15,6 @@
 #include "common/transform.h"
 #include "common/version.h"
 #include "bit_reader.h"
-#include "dec_static_init.h"
 #include "huffman.h"
 #include "prefix.h"
 #include "state.h"
@@ -80,10 +79,6 @@
 BrotliDecoderState* BrotliDecoderCreateInstance(
     brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) {
   BrotliDecoderState* state = 0;
-  if (!BrotliDecoderEnsureStaticInit()) {
-    BROTLI_DUMP();
-    return 0;
-  }
   if (!alloc_func && !free_func) {
     state = (BrotliDecoderState*)malloc(sizeof(BrotliDecoderState));
   } else if (alloc_func && free_func) {
diff --git a/c/dec/state.c b/c/dec/state.c
index 493b753..2c5182b 100644
--- a/c/dec/state.c
+++ b/c/dec/state.c
@@ -8,6 +8,7 @@
 
 #include "common/dictionary.h"
 #include "common/platform.h"
+#include "dec_static_init.h"
 #include "huffman.h"
 
 #if defined(__cplusplus) || defined(c_plusplus)
@@ -31,6 +32,9 @@
 
 BROTLI_BOOL BrotliDecoderStateInit(BrotliDecoderState* s,
     brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) {
+  if (!BrotliDecoderEnsureStaticInit()) {
+    return BROTLI_FALSE;
+  }
   BROTLI_DECODER_ON_START(s);
   if (!alloc_func) {
     s->alloc_func = BrotliDefaultAllocFunc;
diff --git a/c/enc/enc_static_init.c b/c/enc/enc_static_init.c
index 5498cbc..905ee5f 100644
--- a/c/enc/enc_static_init.c
+++ b/c/enc/enc_static_init.c
@@ -50,6 +50,7 @@
 #elif (BROTLI_STATIC_INIT == BROTLI_STATIC_INIT_EARLY)
   return kEarlyInitOk;
 #else
+  BrotliEncoderLazyStaticInit();
   return kLazyInitOk;
 #endif
 }
diff --git a/c/tools/brotli.c b/c/tools/brotli.c
index 748ef40..31dbb15 100644
--- a/c/tools/brotli.c
+++ b/c/tools/brotli.c
@@ -1034,9 +1034,28 @@
   }
 }
 
+/* Checked before the output is opened with O_TRUNC. An inode of 0 (Windows)
+   means "cannot tell". */
+static BROTLI_BOOL IsSameFile(const char* input_path,
+                              const char* output_path) {
+  struct stat in_stat;
+  struct stat out_stat;
+  if (input_path == 0 || output_path == 0) return BROTLI_FALSE;
+  if (stat(input_path, &in_stat) != 0) return BROTLI_FALSE;
+  if (stat(output_path, &out_stat) != 0) return BROTLI_FALSE;
+  if (in_stat.st_ino == 0 || out_stat.st_ino == 0) return BROTLI_FALSE;
+  return TO_BROTLI_BOOL(in_stat.st_dev == out_stat.st_dev &&
+                        in_stat.st_ino == out_stat.st_ino);
+}
+
 static BROTLI_BOOL OpenFiles(Context* context) {
   BROTLI_BOOL is_ok = OpenInputFile(context->current_input_path, &context->fin);
   if (!context->test_integrity && is_ok) {
+    if (IsSameFile(context->current_input_path, context->current_output_path)) {
+      fprintf(stderr, "input and output are the same file [%s]\n",
+              PrintablePath(context->current_output_path));
+      return BROTLI_FALSE;
+    }
     is_ok = OpenOutputFile(
         context->current_output_path, &context->fout, context->force_overwrite);
   }