Skip to content

Commit d1e9ce5

Browse files
author
H. Peter Anvin (Intel)
committed
asm/preproc.c: merge end-of-input code
Normally, end of included files or special inputs should be handled by pp_tokline(), but under some conditions it might be handled by pp_cleanup_pass(). The latter case would assume the input was from a file, which would cause a crash if istk->fp is NULL. Although this should never happen, this is a good reason to merge these two pieces of code, which ought to be doing the same thing anyway. Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
1 parent 4bcfe7a commit d1e9ce5

1 file changed

Lines changed: 35 additions & 33 deletions

File tree

asm/preproc.c

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8642,6 +8642,36 @@ void pp_init(enum preproc_opt opt)
86428642
nasm_newn(use_loaded, use_package_count);
86438643
}
86448644

8645+
static Include *pop_include_stack(void)
8646+
{
8647+
Include *i = istk;
8648+
8649+
if (i->fp)
8650+
fclose(i->fp);
8651+
if (i->conds) {
8652+
/*
8653+
* This should never happen for a builtin macro package,
8654+
* but if it does, at least get an error message out...
8655+
*/
8656+
nasm_fatal("expected `%%endif' before end of %s",
8657+
i->fp ? "file" : "macro package");
8658+
}
8659+
8660+
istk = i->next;
8661+
8662+
if (!i->nolist)
8663+
lfmt->downlevel(LIST_INCLUDE);
8664+
if (!i->noline) {
8665+
struct src_location whereto
8666+
= istk ? istk->where : src_nowhere();
8667+
if (ppdbg & PDBG_INCLUDE)
8668+
dfmt->debug_include(false, whereto, i->where);
8669+
src_update(whereto);
8670+
}
8671+
8672+
return i;
8673+
}
8674+
86458675
/*
86468676
* Get a line of tokens. If we popped the macro expansion/include stack,
86478677
* we return a pointer to the dummy token tok_pop; at that point if
@@ -8809,25 +8839,8 @@ static Token *pp_tokline(void)
88098839
/*
88108840
* The current file/input has ended; work down the istk
88118841
*/
8812-
Include *i = istk;
8813-
8814-
if (i->fp)
8815-
fclose(i->fp);
8816-
if (i->conds)
8817-
nasm_fatal("expected `%%endif' before end of file");
8818-
8819-
istk = i->next;
8842+
Include *i = pop_include_stack();
88208843

8821-
if (!i->nolist)
8822-
lfmt->downlevel(LIST_INCLUDE);
8823-
if (!i->noline) {
8824-
struct src_location whereto
8825-
= istk ? istk->where : src_nowhere();
8826-
if (ppdbg & PDBG_INCLUDE)
8827-
dfmt->debug_include(false, whereto, i->where);
8828-
if (istk)
8829-
src_update(istk->where);
8830-
}
88318844

88328845
put_mmacro(&i->mstk.mstk);
88338846
put_mmacro(&i->mstk.mmac);
@@ -8942,10 +8955,10 @@ void pp_cleanup_pass(void)
89428955
{
89438956
if (defining) {
89448957
if (defining->name) {
8945-
nasm_nonfatal("end of file while still defining macro `%s'",
8958+
nasm_nonfatal("end of input while still defining macro `%s'",
89468959
defining->name);
89478960
} else {
8948-
nasm_nonfatal("end of file while still in %%rep");
8961+
nasm_nonfatal("end of input while still in %%rep");
89498962
}
89508963

89518964
free_mmacro(defining);
@@ -8955,19 +8968,8 @@ void pp_cleanup_pass(void)
89558968
while (cstk)
89568969
ctx_pop();
89578970
free_macros();
8958-
while (istk) {
8959-
Include *i = istk;
8960-
istk = istk->next;
8961-
fclose(i->fp);
8962-
if (!istk && (ppdbg & PDBG_INCLUDE)) {
8963-
/* Signal closing the top-level input file */
8964-
dfmt->debug_include(false, src_nowhere(), i->where);
8965-
}
8966-
nasm_free(i);
8967-
}
8968-
while (cstk)
8969-
ctx_pop();
8970-
src_set_fname(NULL);
8971+
while (istk)
8972+
nasm_free(pop_include_stack());
89718973

89728974
if (ppdbg & PDBG_MMACROS)
89738975
debug_macro_output();

0 commit comments

Comments
 (0)