Skip to content
Myr

[Fix] `nvm_resolve_local_alias`: avoid using variable as printf format string

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

Using a variable as the format string means `%` characters in alias names would be interpreted as format specifiers. Use `%b` format with the variable as an argument to safely interpret `\n` escapes. Bug introduced in https://github.com/nvm-sh/nvm/commit/9b91734f0b6a210f0c6655b0cd25ea288d9ae376.

authored by Jordan Harband · committed by Jordan Harband · parent 3c48015 · Browse files · 5 files+43 added−1 removed
This commit

State

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

Diff stats

5 files+43 added−1 removed

  • tests:+42 added−0 removed
  • other:+1 added−1 removed

5 files+43 added−1 removed

added:test/fast/Aliases/percent in alias name/nvm_resolve_alias+17 added
1#!/bin/sh2\. ../../../common.sh34die () { echo "$@" ; exit 1; }56: nvm.sh7\. ../../../../nvm.sh89# An alias whose name contains % should resolve directly10try nvm_resolve_alias 'test-%s-alias'11[ "$CAPTURED_EXIT_CODE" = "0" ] || die "nvm_resolve_alias test-%s-alias failed with exit code $CAPTURED_EXIT_CODE"12[ "$CAPTURED_STDOUT" = "v0.0.1" ] || die "nvm_resolve_alias test-%s-alias was not v0.0.1; got $CAPTURED_STDOUT"1314# An alias chain that passes through a %-containing alias name should resolve15try nvm_resolve_alias test-pct-chain16[ "$CAPTURED_EXIT_CODE" = "0" ] || die "nvm_resolve_alias test-pct-chain failed with exit code $CAPTURED_EXIT_CODE"17[ "$CAPTURED_STDOUT" = "v0.0.1" ] || die "nvm_resolve_alias test-pct-chain was not v0.0.1; got $CAPTURED_STDOUT"
added:test/fast/Aliases/percent in alias name/nvm_resolve_local_alias+17 added
1#!/bin/sh2\. ../../../common.sh34die () { echo "$@" ; exit 1; }56: nvm.sh7\. ../../../../nvm.sh89# An alias whose name contains % should resolve directly10try nvm_resolve_local_alias 'test-%s-alias'11[ "$CAPTURED_EXIT_CODE" = "0" ] || die "nvm_resolve_local_alias test-%s-alias failed with exit code $CAPTURED_EXIT_CODE"12[ "$CAPTURED_STDOUT" = "v0.0.1" ] || die "nvm_resolve_local_alias test-%s-alias was not v0.0.1; got $CAPTURED_STDOUT"1314# An alias chain that passes through a %-containing alias name should resolve15try nvm_resolve_local_alias test-pct-chain16[ "$CAPTURED_EXIT_CODE" = "0" ] || die "nvm_resolve_local_alias test-pct-chain failed with exit code $CAPTURED_EXIT_CODE"17[ "$CAPTURED_STDOUT" = "v0.0.1" ] || die "nvm_resolve_local_alias test-pct-chain was not v0.0.1; got $CAPTURED_STDOUT"
added:test/fast/Aliases/percent in alias name/setup+4 added
1#!/bin/sh23echo v0.0.1 > ../../../../alias/test-%s-alias4echo test-%s-alias > ../../../../alias/test-pct-chain
added:test/fast/Aliases/percent in alias name/teardown+4 added
1#!/bin/sh23rm -f ../../../../alias/test-%s-alias4rm -f ../../../../alias/test-pct-chain
modified:nvm.sh+1 added-1 removed
1366 unmodified lines
1367 if [ -z "${ALIAS_TEMP}" ]; then1368 break1369 fi13701371 if command printf "${SEEN_ALIASES}" | nvm_grep -q -e "^${ALIAS_TEMP}$"; then1371 if command printf '%b' "${SEEN_ALIASES}" | nvm_grep -q -e "^${ALIAS_TEMP}$"; then1372 ALIAS="∞"1373 break1374 fi1375
3442 unmodified lines