11

I'm debugging the command tex of TeX Live 2019.

enter image description here

Shown as above, if you type:

./tex -ini

h

I\&V

e

There will be a segment fault. Note that you need a h.tex under the same directory as tex and there is one character \ in h.tex.

My question is what does cmd I do? It seems it fails to open a file because there is an invalid sequence \&V after I. And what does cmd e do? I know It passes the file pointer to fclose() that failed to be initialized by I and the leads to the segment fault.

Thanks!

3
  • 1
    Welcome to TeX.SX! Donald already explained why the TeX errors happen. But to reproduce the segfault with tex (not -ini) you can write \? instead of \ and \&. More generally, the segfault happens if you are running TeX on a file, and press E in the menu while TeX is scanning the input of I. Interesting find! Jul 3, 2020 at 3:33
  • It seems at first glance like a bug in TeX. A have no time for studying this issue in TeX sources now. If it is true bug then congratulations: you have found the bug in TeX 3.14159265.
    – wipet
    Jul 3, 2020 at 6:58
  • @barbarabeeton May be you know the term when D. Knuth opens the folder with bug reports of TeX 3.14159265...
    – wipet
    Jul 3, 2020 at 7:09

4 Answers 4

7

Running TeX with the -ini flag means that you're running without any preloaded format. You don't get anything but primitive commands which means that \ and \& aren't defined. So that's why you're getting the errors. Do you really want to be running iniTeX?

i means insert the text following at the point that the error occurred. x is the usual quit command. e in theory will stop and then open an editor at the error point. I'm guessing this is missing some implementation/configuration which is why you're getting the segmentation fault. These are just some of the interactive error handling commands that TeX provides. See The TeXbook for more information about how these work. I've never really bothered with them. They made a lot of sense in the early 80s when computers were slow and compute time expensive but even by 1986 when I first started using TeX the program ran fast enough that it was generally more sensible to just use x to stop TeX, fix the error in the input file and continue.

8
  • The weird thing is that TeX seems to consistently segfault if you type e while reading the input of i... Jul 3, 2020 at 3:22
  • Thanks a lot. But I'm a little bit confused about "i". Will "i" insert the text to the file h.tex? Or do you mean "i" just insert a text to a buffer?
    – 潇洒张
    Jul 3, 2020 at 3:59
  • No, it inserts the characters into the stream being read by the current run, so is not useful for fixing an error. It could be useful for diagnosing an error that you aren't sure of, but as DH says, it usually makes more sense to edit the input file and run TeX again. Jul 3, 2020 at 5:18
  • @DonaldHosek Did you get sucked into this quagmire by Coronavirus lockdowns too? Jul 3, 2020 at 5:21
  • 2
    @DonaldArseneau don't think of it as being sucked into a quagmire, think of it as being rescued from the primordial swamp that is c.t.t Jul 3, 2020 at 8:30
7

Congratulations, you have found the bug in TeX.

Edit I did deeper inspection about this issue when I returned from my vacation and I found: the bug is in the C implementation of TeX, not in the TeX itself (coded by D. Knuth). So, the following paragraph (given by me a few days ago) is not explicitly true:

The last revision of TeX (version 3.14159265) was done by D. Knuth in 2014, next revision will be in the end of 2020. If you send this bug-report you can get a check for a small piece in dollars by D. Knuth. Of course, this is true only if your bug report will be processed as real bug of TeX. How to do the bug report is described at here in section Errata.

Description of the bug: If we invoke an error (undefined \ in your example), then the I option is possible. If we use I option with a line with next error (undefined \& in your example) and the I line is not closed at this place (the V follows in your example) then this second error offers all options including E option. But using E option causes that TeX crashes, because the file name is zero in this case.

In detail: I option in § 84 of TeX web runs the code from § 87 where the procedure begin_file_reading is processed. This procedure is in § 328 and here is: name := 0. The name is macro defined in § 302: name = cur_input.name_field. Now, we can return to § 84 where the E option is solved. It creates the file name (which may be sent to an editor) from input_stack[base_ptr].name_field but this is the same as cur_input.name_field and it was set to zero. We have no file name here but zero. TeX implementation based on C crashes with segmentation fault because we want to open the file with buggy file name.

Edit: When I did experiments with tex.web code and I deactivated the system dependent code given after E option then the correct answer is here: You want to edit file <correct name> at line <correct number>. The bug is in the lib/texmfmp.c at line 2579 where the opened files are closed and the maximal index of opened files are read from inopen global variable. This variable is incremented when I option is used but no new file is opened in such case. So, we cannot close non-existent file in lib/texmf.c. Knuth says in §304: "The global variable in_open is equal to the value of the highest non-token-list level." It means that it is not always the number of currently opened files. Knuth's code and his comments in the code are correct here, bug is in the system dependent implementation.

11
  • At the point of the first error message, \jobname has been set to h.
    – egreg
    Jul 3, 2020 at 13:07
  • @egreg the file name h is irrelevant. You can insert \bla in your document (the name of the document and the format are irrelevant). When the prompt about undefined \bla occurs then inset i\buff X. When the prompt about undefined \buff occurs then insert e. The result is segmentation fault.
    – wipet
    Jul 3, 2020 at 13:13
  • Maybe you can make it clear in your answer.
    – egreg
    Jul 3, 2020 at 13:29
  • But it crashed on fclose() with uninitialized file pointer as the argument. In this case there is only one file: h.tex. But inopen was 2 when it tried to close all files. So it tried to close inputfile[1] and inputfile[2] in a for loop and inputfile[2] was uninitialized.
    – 潇洒张
    Jul 3, 2020 at 14:23
  • 1
    @潇洒张 I corrected the term of bug report: the end of this year. Please, tell me if you prepare this bug report. If no, then I will try to do it. But you found this bug, this is much more correct if the honor will go to you. I have only analyzed this big from technical point of view. But I wouldn't think of trying the E option in the context of I option.
    – wipet
    Jul 3, 2020 at 20:24
5

Original poster: as with your other report ... if you ever see this, please email me, karl at tug.org, so we can give you proper credit in our report to Don Knuth. (We believe there is a bug in tex.web as well as in web2c; the tex.web code can be induced to print a bogus filename in the "You want to edit ..." message.)

Regarding begin_file_reading, it is, shall we say, suboptimally named. What it really does is "begin a level of input" - either from a file or from terminal interaction.

I just committed a fix (hopefully) for this to TeX Live, r55857 (http://tug.org/svn/texlive/trunk/Build/source/texk/web2c/lib/texmfmp.c?r1=53078&r2=55857&pathrev=55857). It involves having to look at all elements of input_stack[] to discern whether a given entry in input_file[] is actually a file, or a non-file resulting from terminal interaction. Before, we simply closed everything in input_file[], but that was wrong.

Regarding "the end of this year" - please send any and all Knuthian bug reports to me or tex-k at tug.org as soon as possible! Do not wait for December 31. My unofficial deadline is November 1. All reports have to be vetted before they go into the pile for Knuth, and it takes time. More info: https://tug.org/texmfbug.

Thanks to all.

0

Apply this change to original tex.web and you will see that there is no bug in TeX:

@x
@.You want to edit file x@>
  slow_print(input_stack[base_ptr].name_field);
  print(" at line "); print_int(line);
@y
@.You want to edit file x@>
  slow_print(input_stack[base_ptr].name_field);
  print(" which has string number ");
print_int(input_stack[base_ptr].name_field);
@z

The output:

You want to edit file h.tex which has string number 1346

(i.e., input_stack[base_ptr].name_field is not zero)

Therefore, the relevant change entry is totally useless and serves no purpose:

431. Don't exit to editor if no input file is at the bottom line
(Xiaosa Zhang, 03 July 2020)
@x module 84
"E": if base_ptr>0 then
@y
"E": if base_ptr>0 then if input_stack[base_ptr].name_field>=256 then
@z
@x module 85
if base_ptr>0 then print("E to edit your file,");
@y
if base_ptr>0 then if input_stack[base_ptr].name_field>=256 then
  print("E to edit your file,");
@z

(all the calamities are due to web2c)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .