string repeat of an empty string is an error

Fixes #3898
This commit is contained in:
Kurtis Rader 2017-03-15 13:55:53 -07:00
parent 6fd8dc44fb
commit f0469d829a
2 changed files with 10 additions and 2 deletions

View file

@ -1046,7 +1046,7 @@ static int string_repeat(parser_t &parser, io_streams_t &streams, int argc, wcha
wcstring storage;
bool is_empty = true;
if ((to_repeat = string_get_arg(&i, argv, &storage, streams)) != NULL) {
if ((to_repeat = string_get_arg(&i, argv, &storage, streams)) != NULL && *to_repeat) {
const wcstring word(to_repeat);
const bool rep_until = (0 < max && word.length()*count > max) || !count;
const wcstring repeated = rep_until ? wcsrepeat_until(word, max) : wcsrepeat(word, count);

View file

@ -132,4 +132,12 @@ echo 'stdin' | string repeat -n1 'and arg'; or echo "exit 2"
string repeat -n; or echo "exit 2"
string repeat -l fakearg 2>&1 | head -n1 1>&2
string repeat -l fakearg 2>&1 | head -n1 1>&2
string repeat ''
and echo string repeat empty string did not fail
string repeat -n3 ''
and echo string repeat empty string did not fail
exit 0