mirror of
https://github.com/ivuorinen/tree-sitter-shellspec.git
synced 2026-01-26 11:43:59 +00:00
feat: add post-generation script to preserve buffer overflow fix
Created scripts/post-generate.sh that automatically re-applies the critical buffer overflow fix to parser.h after tree-sitter generate runs. This fix prevents undefined behavior in set_contains() when accessing an empty array. The script is automatically executed after tree-sitter generate via the npm generate script. Added generate:only for cases where post-processing should be skipped.
This commit is contained in:
33
scripts/post-generate.sh
Executable file
33
scripts/post-generate.sh
Executable file
@@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env bash
|
||||
set -e
|
||||
|
||||
echo "Running post-generation fixes..."
|
||||
|
||||
# Apply critical safety fixes that get overwritten during generation
|
||||
echo " - Applying critical safety fixes..."
|
||||
|
||||
# Fix 1: Buffer overflow prevention in parser.h
|
||||
# The set_contains function needs a len==0 check to prevent accessing ranges[0]
|
||||
# This fix gets overwritten every time tree-sitter generate runs
|
||||
if ! grep -q "if (len == 0) return false;" src/tree_sitter/parser.h; then
|
||||
# Insert the safety check right after the function opening
|
||||
# Target: static inline bool set_contains(...) {
|
||||
# Insert: if (len == 0) return false;
|
||||
# Before: uint32_t index = 0;
|
||||
|
||||
# Use perl for cross-platform compatibility (macOS and Linux)
|
||||
perl -i -pe '
|
||||
BEGIN { $in_func = 0; $done = 0; }
|
||||
if (/static inline bool set_contains/) { $in_func = 1; }
|
||||
if ($in_func && /^\s+uint32_t index = 0;/ && !$done) {
|
||||
print " if (len == 0) return false;\n";
|
||||
$done = 1;
|
||||
}
|
||||
if (/^}/ && $in_func) { $in_func = 0; }
|
||||
' src/tree_sitter/parser.h
|
||||
echo " ✓ Applied buffer overflow fix to parser.h"
|
||||
else
|
||||
echo " ✓ Buffer overflow fix already present"
|
||||
fi
|
||||
|
||||
echo "Post-generation fixes complete!"
|
||||
Reference in New Issue
Block a user