From f0469d829a8c30777b4db8325bf3100bae213a3b Mon Sep 17 00:00:00 2001 From: Kurtis Rader Date: Wed, 15 Mar 2017 13:55:53 -0700 Subject: [PATCH] string repeat of an empty string is an error Fixes #3898 --- src/builtin_string.cpp | 2 +- tests/string.in | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/builtin_string.cpp b/src/builtin_string.cpp index 38b0ffc35..f19706a2d 100644 --- a/src/builtin_string.cpp +++ b/src/builtin_string.cpp @@ -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); diff --git a/tests/string.in b/tests/string.in index ed07b9318..3ab8199db 100644 --- a/tests/string.in +++ b/tests/string.in @@ -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 \ No newline at end of file +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