Skip to content
Myr

[Fix] prevent sed errors when pattern contains `#`

Before adoption: this commit came in with the history the project adopted, so no landing records it.

When `.nvmrc` or alias files contained comments (lines with `#`), the `#` character could end up in the search pattern passed to sed, causing "unterminated regular expression" errors because `#` is used as the sed address delimiter. This commit fixes the issue in two places: 1. `nvm_alias`: Strip comments from alias file contents before returning them, and trim trailing whitespace 2. `nvm_ls`: Escape `#` characters in SEARCH_PATTERN so they're treated as literal characters in the sed address Fixes #3761

authored by Jordan Harband · committed by Jordan Harband · parent 34d6d5d · Browse files · 3 files+58 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+58 added−2 removed

  • tests:+56 added−0 removed
  • other:+2 added−2 removed

3 files+58 added−2 removed

added:test/fast/Unit tests/nvm_alias handles comments+33 added
1#!/bin/sh23die () { echo "$@" ; cleanup ; exit 1; }45cleanup () {6 rm -rf ../../../alias/test-comment7 rm -rf ../../../alias/test-inline-comment8 rm -rf ../../../alias/test-comment-first9}1011\. ../../../nvm.sh1213# Test: alias file with comment on separate line14echo "v0.1015# this is a comment" > ../../../alias/test-comment16OUTPUT="$(nvm_alias test-comment)"17EXPECTED_OUTPUT="v0.10"18[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_alias test-comment' should ignore comment line; expected '$EXPECTED_OUTPUT', got '$OUTPUT'"1920# Test: alias file with inline comment21echo "v0.11 # inline comment" > ../../../alias/test-inline-comment22OUTPUT="$(nvm_alias test-inline-comment)"23EXPECTED_OUTPUT="v0.11"24[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_alias test-inline-comment' should strip inline comment; expected '$EXPECTED_OUTPUT', got '$OUTPUT'"2526# Test: alias file with comment as first line27echo "# comment first28v0.12" > ../../../alias/test-comment-first29OUTPUT="$(nvm_alias test-comment-first)"30EXPECTED_OUTPUT="v0.12"31[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] || die "'nvm_alias test-comment-first' should skip comment-only first line; expected '$EXPECTED_OUTPUT', got '$OUTPUT'"3233cleanup
added:test/fast/Unit tests/nvm_ls handles hash in pattern+23 added
1#!/bin/sh23die () { echo "$@" ; exit 1; }45\. ../../../nvm.sh67# Test: nvm_ls with pattern containing # should not cause sed error8# This is a regression test for https://github.com/nvm-sh/nvm/issues/3761910# The pattern with # should not cause sed "unterminated regular expression" error11# It's OK if it returns N/A (no match), but it should not produce sed errors12OUTPUT="$(nvm_ls 'foo#bar' 2>&1)"13EXIT_CODE=$?1415# Check that output doesn't contain sed error message16echo "$OUTPUT" | grep -q "unterminated regular expression" && \17 die "nvm_ls with # in pattern caused sed 'unterminated regular expression' error: $OUTPUT"18echo "$OUTPUT" | grep -q "invalid command code" && \19 die "nvm_ls with # in pattern caused sed 'invalid command code' error: $OUTPUT"2021# Should return N/A with exit code 3 (not found)22[ "$EXIT_CODE" = "3" ] || die "nvm_ls 'foo#bar' should exit with code 3, got $EXIT_CODE"23echo "$OUTPUT" | grep -q "N/A" || die "nvm_ls 'foo#bar' should output N/A, got: $OUTPUT"
modified:nvm.sh+2 added-2 removed
1292 unmodified lines
1293 nvm_err 'Alias does not exist.'1294 return 21295 fi12961297 command awk 'NF' "${NVM_ALIAS_PATH}"1297 command sed 's/#.*//; s/[[:space:]]*$//' "${NVM_ALIAS_PATH}" | command awk 'NF'1298}12991300nvm_ls_current() {1301 local NVM_LS_CURRENT_NODE_PATH
226 unmodified lines
1528 if [ -z "${PATTERN}" ]; then1529 PATTERN='v'1530 SEARCH_PATTERN='.*'1531 else1532 SEARCH_PATTERN="$(nvm_echo "${PATTERN}" | command sed 's#\.#\\\.#g;')"1532 SEARCH_PATTERN="$(nvm_echo "${PATTERN}" | command sed 's#\.#\\\.#g; s|#|\\#|g')"1533 fi1534 if [ -n "${NVM_DIRS_TO_SEARCH1}${NVM_DIRS_TO_SEARCH2}${NVM_DIRS_TO_SEARCH3}" ]; then1535 VERSIONS="$(command find "${NVM_DIRS_TO_SEARCH1}"/* "${NVM_DIRS_TO_SEARCH2}"/* "${NVM_DIRS_TO_SEARCH3}"/* -name . -o -type d -prune -o -path "${PATTERN}*" \1536 | command sed -e "
3126 unmodified lines