Skip to content

Prevent arguments leak #150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 8 additions & 5 deletions src/sprintf.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@
}

function sprintf(key) {
// `arguments` is not an array, but should be fine for this call
return sprintf_format(sprintf_parse(key), arguments)
var leakGuard = new Array(arguments.length-1)
for (var i=0; i<leakGuard.length; ++i) {
leakGuard[i] = arguments[i+1]
}
return vsprintf(key, leakGuard)
}

function vsprintf(fmt, argv) {
return sprintf.apply(null, [fmt].concat(argv || []))
return sprintf_format(sprintf_parse(fmt), argv || [])
}

function sprintf_format(parse_tree, argv) {
var cursor = 1, tree_length = parse_tree.length, arg, output = '', i, k, ph, pad, pad_character, pad_length, is_positive, sign
var cursor = 0, tree_length = parse_tree.length, arg, output = '', i, k, ph, pad, pad_character, pad_length, is_positive, sign
for (i = 0; i < tree_length; i++) {
if (typeof parse_tree[i] === 'string') {
output += parse_tree[i]
Expand All @@ -48,7 +51,7 @@
}
}
else if (ph.param_no) { // positional argument (explicit)
arg = argv[ph.param_no]
arg = argv[ph.param_no-1]
}
else { // positional argument (implicit)
arg = argv[cursor++]
Expand Down