• R/O
  • SSH
  • HTTPS

yash: Commit


Commit MetaInfo

Revision3874 (tree)
Time2018-09-19 00:55:17
Authormagicant

Log Message

Always reject unclosed arithmetic expansion

According to POSIX, it is a syntax error if the shell reaches the end of
input before determining whether an expansion starting with "$((" is a
valid arithmetic expansion. Previously, yash treated such a case as if
it was determined not being an arithmetic expansion, then re-parsed it
as a command substitution. In most cases, the result was an invalid
command substitution, which caused a syntax error anyway. However, there
are corner cases where the expansion can be re-parsed as a valid command
substitution. Yash now correctly rejects it.

Change Summary

Incremental Difference

--- yash/branches/token_based_parser/parser.c (revision 3873)
+++ yash/branches/token_based_parser/parser.c (revision 3874)
@@ -2251,7 +2251,8 @@
22512251 ensure_buffer(ps, 1);
22522252 switch (ps->src.contents[ps->index]) {
22532253 case L'\0':
2254- goto fail;
2254+ serror(ps, Ngt("`%ls' is missing"), L"))");
2255+ goto end;
22552256 case L'\\':
22562257 if (ps->src.contents[ps->index + 1] != L'\0') {
22572258 assert(ps->src.contents[ps->index + 1] != L'\n');
@@ -2280,23 +2281,26 @@
22802281 break;
22812282 case L')':
22822283 nestparen--;
2283- if (nestparen < 0) {
2284- ensure_buffer(ps, 2);
2285- if (ps->src.contents[ps->index + 1] == L')')
2284+ if (nestparen >= 0)
2285+ break;
2286+ ensure_buffer(ps, 2);
2287+ switch (ps->src.contents[ps->index + 1]) {
2288+ case L')':
2289+ MAKE_WORDUNIT_STRING;
2290+ ps->index += 2;
22862291 goto end;
2287- else
2288- goto fail;
2292+ case L'\0':
2293+ serror(ps, Ngt("`%ls' is missing"), L")");
2294+ goto end;
2295+ default:
2296+ goto not_arithmetic_expansion;
22892297 }
2290- break;
22912298 default:
22922299 break;
22932300 }
22942301 ps->index++;
22952302 }
2296-end:
2297- MAKE_WORDUNIT_STRING;
2298- ps->index += 2;
2299-
2303+end:;
23002304 wordunit_T *result = xmalloc(sizeof *result);
23012305 result->next = NULL;
23022306 result->wu_type = WT_ARITH;
@@ -2303,7 +2307,7 @@
23032307 result->wu_arith = first;
23042308 return result;
23052309
2306-fail:
2310+not_arithmetic_expansion:
23072311 wordfree(first);
23082312 rewind_index(ps, saveindex);
23092313 return NULL;
Show on old repository browser