| 1 |
% $Id: unbalanced-braces.ch 71417 2024-06-03 22:32:13Z karl $ |
| 2 |
% Fix overrun/underrun of \write and \output. David Fuchs, 2024. |
| 3 |
% Public domain. |
| 4 |
% |
| 5 |
% Changes for full defense against over-running (or under-running) |
| 6 |
% an \output or \write and ending up in all sorts of ill-defined |
| 7 |
% trouble. (Including the case of \output=\toks with no braces.) |
| 8 |
% |
| 9 |
% For some problematic input texts, see tests/unbalanced-braces.test |
| 10 |
% (not a runnable test). References to bug reports are also there. |
| 11 |
% |
| 12 |
% The idea is that when it's time to run/evaluate each \output or \write, |
| 13 |
% they have to come to an end exactly as expected: at the right brace that |
| 14 |
% came from when they were first scanned to begin with; no amount of |
| 15 |
% monkey business with "funny braces" or \noexpand or \expandafter or \let |
| 16 |
% or whatever other trick you can think of should be able to get around |
| 17 |
% it. And you can't get away with going even one token past where you were |
| 18 |
% supposed to stop. |
| 19 |
% |
| 20 |
% To reiterate, fatal errors from this result in the terminal show just: |
| 21 |
% ! Emergency stop. |
| 22 |
% with no details on what/why. You have to look in the .log file for |
| 23 |
% the (terse) specifics: |
| 24 |
% Unbalanced output routine |
| 25 |
% |
| 26 |
% It's rather draconian, calling fatal_error when there's a problem, |
| 27 |
% but users really have no business trying to get anywhere near this |
| 28 |
% sort of thing on purpose. I sure hope nobody has found any use for |
| 29 |
% such undefined behaviors. |
| 30 |
|
| 31 |
% The changes assume locnull-optimize.ch has been applied. Unfortunately |
| 32 |
% the semantically-related changes have to be broken up to apply in |
| 33 |
% tex.web order. |
| 34 |
|
| 35 |
% emacs-page |
| 36 |
%% Catch extra left braces in \output right when finished scanning it. |
| 37 |
% |
| 38 |
@x [23.324] l.7000 p.B139 |
| 39 |
if token_type=macro then {parameters must be flushed} |
| 40 |
while param_ptr>param_start do |
| 41 |
begin decr(param_ptr); |
| 42 |
flush_list(param_stack[param_ptr]); |
| 43 |
end; |
| 44 |
@y |
| 45 |
if token_type=macro then {parameters must be flushed} |
| 46 |
while param_ptr>param_start do |
| 47 |
begin decr(param_ptr); |
| 48 |
flush_list(param_stack[param_ptr]); |
| 49 |
end |
| 50 |
else if (token_type=output_text)and(not output_can_end) then |
| 51 |
fatal_error("Unbalanced output routine"); |
| 52 |
@.Unbalanced output routine@> |
| 53 |
@z |
| 54 |
|
| 55 |
%% Catch extra right braces in the \output routine. |
| 56 |
% <Resume the page builder...> tried, but needs to be more robust against |
| 57 |
% a backed-up right (funny) brace in the middle of \output masquerading |
| 58 |
% as the end-of-\output right brace. |
| 59 |
% |
| 60 |
% Reorder these so that end_token_list sees output_active=false. |
| 61 |
% 1) In back_input: |
| 62 |
@x [23.325] l.7025 p.B139 |
| 63 |
begin while (loc=null)and(token_type<>v_template) do |
| 64 |
end_token_list; {conserve stack space} |
| 65 |
@y |
| 66 |
begin while (loc=null)and(token_type<>v_template) |
| 67 |
and(token_type<>output_text) do |
| 68 |
end_token_list; {conserve stack space} |
| 69 |
@z |
| 70 |
|
| 71 |
%% Catch extra left braces finishing scanning a \write_text. |
| 72 |
% |
| 73 |
% In <Input from token list...> don't allow end_write while we're |
| 74 |
% still scanning through the write_text. |
| 75 |
@x [24.357] l.7488 p.B150 |
| 76 |
else check_outer_validity; |
| 77 |
@y |
| 78 |
else |
| 79 |
begin |
| 80 |
if (cur_cs=end_write)and(mode=0) then |
| 81 |
fatal_error("Unbalanced write command"); |
| 82 |
check_outer_validity; |
| 83 |
end; |
| 84 |
@z |
| 85 |
|
| 86 |
%% Returning to catching extra right braces. |
| 87 |
% |
| 88 |
% 2) In <Feed the macro body and its parameters to the scanner>: |
| 89 |
@x [25.390] l.7983 p.B161 |
| 90 |
while (loc=null)and(token_type<>v_template) do |
| 91 |
end_token_list; {conserve stack space} |
| 92 |
@y |
| 93 |
while (loc=null)and(token_type<>v_template) |
| 94 |
and(token_type<>output_text) do |
| 95 |
end_token_list; {conserve stack space} |
| 96 |
@z |
| 97 |
|
| 98 |
% We know we've just scanned a right brace that seems to be the end |
| 99 |
% of the \output routine. But maybe it had been backed-up over, |
| 100 |
% and we've lost the output_text in the call to back_input. So, |
| 101 |
% the checking gets sloppy. But now we're sure to keep the |
| 102 |
% (finished) output_text level around, so we can always check |
| 103 |
% that we were just finished with it, so it's where the brace |
| 104 |
% came from. |
| 105 |
|
| 106 |
@x [45.989] l.19364 p.B417 |
| 107 |
@!output_active:boolean; {are we in the midst of an output routine?} |
| 108 |
@y |
| 109 |
@!output_active:boolean; {are we in the midst of an output routine?} |
| 110 |
@!output_can_end:boolean; {is this an auspicious time for it to end?} |
| 111 |
@z |
| 112 |
|
| 113 |
@x [45.989] l.19367 p.B417 |
| 114 |
output_active:=false; insert_penalties:=0; |
| 115 |
@y |
| 116 |
output_active:=false; output_can_end:=false; insert_penalties:=0; |
| 117 |
@z |
| 118 |
|
| 119 |
% In <Resume the page builder after an output routine has come to an end>: |
| 120 |
@x [45.1026] l.19938 p.B432 |
| 121 |
begin if (loc<>null) or |
| 122 |
((token_type<>output_text)and(token_type<>backed_up)) then |
| 123 |
@<Recover from an unbalanced output routine@>; |
| 124 |
@y |
| 125 |
begin |
| 126 |
while (state=token_list)and(loc=null)and(token_type=backed_up) do |
| 127 |
end_token_list; {output-ending brace may have been backed-up} |
| 128 |
if (state<>token_list)or(loc<>null)or(token_type<>output_text) then |
| 129 |
@<Recover from an unbalanced output routine@>; |
| 130 |
@z |
| 131 |
|
| 132 |
% In <Resume the page builder after an output routine has come to an end>: |
| 133 |
@x [45.1026] l.19941 p.B432 |
| 134 |
end_token_list; {conserve stack space in case more outputs are triggered} |
| 135 |
end_graf; unsave; output_active:=false; insert_penalties:=0;@/ |
| 136 |
@y |
| 137 |
output_can_end:=true; |
| 138 |
end_token_list; {conserve stack space in case more outputs are triggered} |
| 139 |
output_can_end:=false; |
| 140 |
end_graf; unsave; output_active:=false; insert_penalties:=0;@/ |
| 141 |
@z |
| 142 |
|
| 143 |
% <Expand macros in the token list and...> had set mode:=0 while |
| 144 |
% expanding the \write token list. |
| 145 |
|
| 146 |
% Reorder these statements so that the final get_token that's supposed to |
| 147 |
% scan off the end_write_token will have mode<>0 if everything lined up: |
| 148 |
@x [53.1371] l.24884 p.B546 |
| 149 |
get_token;@+if cur_tok<>end_write_token then |
| 150 |
@<Recover from an unbalanced write command@>; |
| 151 |
mode:=old_mode; |
| 152 |
@y |
| 153 |
mode:=old_mode; |
| 154 |
get_token;@+if cur_tok<>end_write_token then |
| 155 |
@<Recover from an unbalanced write command@>; |
| 156 |
@z |