diff --git a/expand.cpp b/expand.cpp index c3cb95031..3851d6b2a 100644 --- a/expand.cpp +++ b/expand.cpp @@ -1051,22 +1051,27 @@ static size_t parse_slice(const wchar_t *in, wchar_t **end_ptr, std::vector &out, long last_idx, parse_error_list_t *errors) { - // We permit last_idx to be beyond the end of the string if and only if the string is empty - assert(instr.empty() || (last_idx >= 0 && (size_t)last_idx < instr.size())); - - // Make this explicit - if (instr.empty()) + const size_t insize = instr.size(); + + // last_idx may be 1 past the end of the string, but no further + assert(last_idx >= 0 && (size_t)last_idx <= insize); + + if (last_idx == 0) { append_completion(out, instr); return true; } - + bool is_ok = true; bool empty = false; - const size_t insize = instr.size(); wcstring var_tmp; @@ -1078,7 +1083,7 @@ static int expand_variables(parser_t &parser, const wcstring &instr, std::vector // CHECK( out, 0 ); - for (long i=last_idx; (i>=0) && is_ok && !empty; i--) + for (long i=last_idx-1; (i>=0) && is_ok && !empty; i--) { const wchar_t c = instr.at(i); if ((c == VARIABLE_EXPAND) || (c == VARIABLE_EXPAND_SINGLE)) @@ -1809,7 +1814,7 @@ int expand_string(const wcstring &input, std::vector &output, expa } else { - if (!expand_variables(parser, next, *out, next.size() - 1, errors)) + if (!expand_variables(parser, next, *out, next.size(), errors)) { return EXPAND_ERROR; }