Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/quoted-case-arguments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"just-bash": patch
---

Preserve argument boundaries when applying Bash case modifiers to quoted array elements and positional parameters.
6 changes: 3 additions & 3 deletions packages/just-bash/src/interpreter/expansion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ import {
computeIsEmpty,
handleArrayKeys,
handleAssignDefault,
handleCaseModification,
handleDefaultValue,
handleErrorIfUnset,
handleIndirection,
Expand All @@ -72,6 +71,7 @@ import {
handleTransform,
handleUseAlternative,
handleVarNamePrefix,
prepareCaseModification,
} from "./expansion/parameter-ops.js";
import {
expandVariablesInPattern,
Expand Down Expand Up @@ -1112,13 +1112,13 @@ async function expandParameterAsync(
return handleSubstring(ctx, parameter, value, operation);

case "CaseModification": {
const result = await handleCaseModification(
const transform = await prepareCaseModification(
ctx,
value,
operation,
expandWordPartsAsync,
expandParameterAsync,
);
const result = transform(value);
checkStringLength(
result,
ctx.limits.maxStringLength,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { describe, expect, it } from "vitest";
import { Bash } from "../../Bash.js";

describe("quoted case modification of argument lists", () => {
it.each([
['a=(ab "cd ef" "" gh); set -- "${a[@]^}"', "4\n<Ab>\n<Cd ef>\n<>\n<Gh>\n"],
['set -- ab "cd ef" "" gh; set -- "${@^}"', "4\n<Ab>\n<Cd ef>\n<>\n<Gh>\n"],
['IFS=:; a=(AB CDE); set -- "${a[*],}"', "1\n<aB:cDE>\n"],
['IFS=:; set -- AB CDE; set -- "${*,}"', "1\n<aB:cDE>\n"],
['a=(abc bcd cde); set -- "${a[@]^^[ab]}"', "3\n<ABc>\n<Bcd>\n<cde>\n"],
['a=(); set -- "${a[@]^}"', "0\n"],
['set --; set -- "${@^}"', "0\n"],
['a=(); set -- "${a[*]^}"', "1\n<>\n"],
['set --; set -- "${*^}"', "1\n<>\n"],
['a=ab; set -- "${a[@]^}" "${a[0]^}" "${a^}"', "3\n<Ab>\n<Ab>\n<Ab>\n"],
['a=(ab cd); declare -n ref=a; set -- "${ref[@]^}"', "2\n<Ab>\n<Cd>\n"],
['a=(ab cd); declare -n ref=a[1]; set -- "${ref[@]^}"', "0\n"],
['a=(ab cd); set -- "pre${a[@]^}post"', "2\n<preAb>\n<Cdpost>\n"],
['set -- ab cd; set -- "pre${@^}post"', "2\n<preAb>\n<Cdpost>\n"],
['a=(); set -- "pre${a[@]^}post"', "1\n<prepost>\n"],
['IFS=:; a=(ab cd); set -- "pre${a[*]^}post"', "1\n<preAb:Cdpost>\n"],
['a=(ab ba); set -- "${a[@]^[b]}"', "2\n<ab>\n<Ba>\n"],
['a=AB BA=BA; set -- "${a,[B]}" "${BA,[B]}"', "2\n<AB>\n<bA>\n"],
['a=(ab cd); set -- pre"${a[@]^}"post', "2\n<preAb>\n<Cdpost>\n"],
[
'a=(ab cd); b=(ef gh); set -- "${a[@]^}-${b[@]^}"',
"3\n<Ab>\n<Cd-Ef>\n<Gh>\n",
],
[
'a=(ab cd); b=(EF GH); set -- "${a[@]^}""${b[@],}"',
"3\n<Ab>\n<CdeF>\n<gH>\n",
],
['a=(ab cd); set -- ef gh; set -- "$@${a[@]^}"', "3\n<ef>\n<ghAb>\n<Cd>\n"],
[
'a=("a*" cd); : > preAxpost; set -- pre"${a[@]^}"post',
"2\n<preA*>\n<Cdpost>\n",
],
['a=(ab cd); : > Cd-end; set -- pre"${a[@]^}"*', "2\n<preAb>\n<Cd-end>\n"],
['shopt -s nullglob; a=(ab cd); set -- pre"${a[@]^}"*', "1\n<preAb>\n"],
['a=(ab cd); x="tail "; set -- $x"${a[@]^}"', "3\n<tail>\n<Ab>\n<Cd>\n"],
['a=(ab cd); x=" tail"; set -- "${a[@]^}"$x', "3\n<Ab>\n<Cd>\n<tail>\n"],
['a=(ab cd); x=" "; set -- pre$x"${a[@]^}"', "3\n<pre>\n<Ab>\n<Cd>\n"],
['a=(ab cd); x="tail"; set -- $x"${a[@]^}"', "2\n<tailAb>\n<Cd>\n"],
])("matches Bash for %s", async (script, stdout) => {
const result = await new Bash().exec(
`${script}; echo "$#"; while [ "$#" -gt 0 ]; do printf '<%s>\\n' "$1"; shift; done`,
);
expect(result).toMatchObject({ stdout, stderr: "", exitCode: 0 });
});

it("keeps the aggregate string limit when splitting case-modified words", async () => {
const bash = new Bash({ executionLimits: { maxStringLength: 10 } });
const result = await bash.exec(
'a=(abcdef ghijkl); set -- "${a[@]^}"; echo reached',
);
expect(result.exitCode).not.toBe(0);
expect(result.stderr).toContain(
"case modification: string length limit exceeded",
);
expect(result.stdout).toBe("");
});

it("checks the total number of arguments across adjacent expansions", async () => {
const bash = new Bash({ executionLimits: { maxArrayElements: 3 } });
const result = await bash.exec(
'a=(ab cd); set -- "${a[@]^}${a[@]^}${a[@]^}"; echo reached',
);
expect(result.exitCode).not.toBe(0);
expect(result.stderr).toContain("word expansion");
expect(result.stdout).toBe("");
});
});
36 changes: 16 additions & 20 deletions packages/just-bash/src/interpreter/expansion/parameter-ops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,15 +484,14 @@ export async function handleSubstring(
}

/**
* Handle CaseModification operation: ${param^pattern}, ${param,pattern}
* Prepare the case transformation once, including any pattern expansion: ${param^pattern}, ${param,pattern}
*/
export async function handleCaseModification(
export async function prepareCaseModification(
ctx: InterpreterContext,
value: string,
operation: CaseModificationOp,
expandWordPartsAsync: ExpandWordPartsAsyncFn,
expandParameterAsync: ExpandParameterAsyncFn,
): Promise<string> {
): Promise<(value: string) => string> {
ctx.coverage?.hit("bash:expansion:case_modification");
if (operation.pattern) {
const extglob = ctx.state.shoptOptions.extglob;
Expand All @@ -518,29 +517,26 @@ export async function handleCaseModification(
? (c: string) => c.toUpperCase()
: (c: string) => c.toLowerCase();

let result = "";
let converted = false;
for (const char of value) {
if (!operation.all && converted) {
result += char;
} else if (charPattern.test(char)) {
result += transform(char);
converted = true;
} else {
result += char;
return (value) => {
let result = "";
for (const char of value) {
result += charPattern.test(char) ? transform(char) : char;
if (!operation.all) {
return result + value.slice(char.length);
}
}
}
return result;
return result;
};
}

if (operation.direction === "upper") {
return operation.all
? value.toUpperCase()
: value.charAt(0).toUpperCase() + value.slice(1);
? (value) => value.toUpperCase()
: (value) => value.charAt(0).toUpperCase() + value.slice(1);
}
return operation.all
? value.toLowerCase()
: value.charAt(0).toLowerCase() + value.slice(1);
? (value) => value.toLowerCase()
: (value) => value.charAt(0).toLowerCase() + value.slice(1);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export type ExpandWordPartsAsyncFn = (
/**
* Get positional parameters from context
*/
function getPositionalParams(ctx: InterpreterContext): string[] {
export function getPositionalParams(ctx: InterpreterContext): string[] {
const numParams = Number.parseInt(ctx.state.env.get("#") || "0", 10);
if (numParams > ctx.limits.maxArrayElements) {
throw new ExecutionLimitError(
Expand Down
Loading