From 902d1abac0383d0e2f43b760f309e99cab278692 Mon Sep 17 00:00:00 2001 From: Flo Date: Mon, 30 Mar 2026 23:45:04 +0200 Subject: [PATCH] fix: force base-10 in version_gte to prevent octal interpretation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use 10#$var arithmetic prefix to avoid bash interpreting leading zeros as octal (e.g., 08 or 09 would cause "value too great for base" errors). 2 new tests (64 total). Closes: #9 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- git-harden.sh | 15 ++++++++------- test/git-harden.bats | 12 ++++++++++++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/git-harden.sh b/git-harden.sh index c1924da..7236f31 100755 --- a/git-harden.sh +++ b/git-harden.sh @@ -209,14 +209,15 @@ version_gte() { # shellcheck disable=SC2086 set -- $1 $2 IFS="$IFS_SAVE" - local a1="${1:-0}" a2="${2:-0}" a3="${3:-0}" - local b1="${4:-0}" b2="${5:-0}" b3="${6:-0}" + # Force base-10 interpretation to avoid octal issues with leading zeros + local a1=$((10#${1:-0})) a2=$((10#${2:-0})) a3=$((10#${3:-0})) + local b1=$((10#${4:-0})) b2=$((10#${5:-0})) b3=$((10#${6:-0})) - if [ "$a1" -gt "$b1" ] 2>/dev/null; then return 0; fi - if [ "$a1" -lt "$b1" ] 2>/dev/null; then return 1; fi - if [ "$a2" -gt "$b2" ] 2>/dev/null; then return 0; fi - if [ "$a2" -lt "$b2" ] 2>/dev/null; then return 1; fi - if [ "$a3" -ge "$b3" ] 2>/dev/null; then return 0; fi + if [ "$a1" -gt "$b1" ]; then return 0; fi + if [ "$a1" -lt "$b1" ]; then return 1; fi + if [ "$a2" -gt "$b2" ]; then return 0; fi + if [ "$a2" -lt "$b2" ]; then return 1; fi + if [ "$a3" -ge "$b3" ]; then return 0; fi return 1 } diff --git a/test/git-harden.bats b/test/git-harden.bats index 84d9f9e..46797ef 100755 --- a/test/git-harden.bats +++ b/test/git-harden.bats @@ -108,6 +108,18 @@ source_functions() { assert_failure } +@test "version_gte: handles leading zeros without octal error" { + source_functions + run version_gte "2.08.0" "2.07.0" + assert_success +} + +@test "version_gte: leading zero comparison works correctly" { + source_functions + run version_gte "2.09.1" "2.09.0" + assert_success +} + # =========================================================================== # Version extraction (grep-based, not sed) # ===========================================================================