Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kernel: close streams after internal exception #5896

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 89 additions & 42 deletions src/streams.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,17 @@
if (!OpenInputStream(&input, stream, echo == True)) {
return result;
}
ExecStatus status = ReadEvalCommand(0, &input, &evalResult, 0);
ExecStatus status;

GAP_TRY
{
status = ReadEvalCommand(0, &input, &evalResult, 0);
}
GAP_CATCH

Check warning on line 254 in src/streams.c

View check run for this annotation

Codecov / codecov/patch

src/streams.c#L254

Added line #L254 was not covered by tests
{
CloseInput(&input);
GAP_THROW();

Check warning on line 257 in src/streams.c

View check run for this annotation

Codecov / codecov/patch

src/streams.c#L256-L257

Added lines #L256 - L257 were not covered by tests
}
CloseInput(&input);

if (status == STATUS_EOF || status == STATUS_QQUIT)
Expand Down Expand Up @@ -404,7 +414,11 @@
}

TypInputFile input;
if (OpenInput(&input, path)) {
if (!OpenInput(&input, path))
return 0;

Check warning on line 418 in src/streams.c

View check run for this annotation

Codecov / codecov/patch

src/streams.c#L418

Added line #L418 was not covered by tests

GAP_TRY
{
while (1) {
ExecStatus status = ReadEvalCommand(0, &input, 0, 0);
if (STATE(UserHasQuit) || STATE(UserHasQUIT))
Expand All @@ -416,11 +430,14 @@
break;
}
}
}
GAP_CATCH

Check warning on line 434 in src/streams.c

View check run for this annotation

Codecov / codecov/patch

src/streams.c#L434

Added line #L434 was not covered by tests
{
CloseInput(&input);
return 1;
GAP_THROW();

Check warning on line 437 in src/streams.c

View check run for this annotation

Codecov / codecov/patch

src/streams.c#L437

Added line #L437 was not covered by tests
}

return 0;
CloseInput(&input);
return 1;
}


Expand Down Expand Up @@ -806,8 +823,17 @@
if (!OpenInputFileOrStream(SELF_NAME, &input, inputObj))
return False;

// read the file
READ_INNER(&input);
GAP_TRY
{
// read the file
READ_INNER(&input);
}
GAP_CATCH

Check warning on line 831 in src/streams.c

View check run for this annotation

Codecov / codecov/patch

src/streams.c#L831

Added line #L831 was not covered by tests
{
CloseInput(&input);
GAP_THROW();

Check warning on line 834 in src/streams.c

View check run for this annotation

Codecov / codecov/patch

src/streams.c#L833-L834

Added lines #L833 - L834 were not covered by tests
}

if (!CloseInput(&input)) {
ErrorQuit("Panic: READ cannot close input", 0, 0);
}
Expand All @@ -826,9 +852,10 @@
static Obj FuncREAD_STREAM_LOOP(Obj self,
Obj instream,
Obj outstream,
Obj context)
Obj ctx)
{
Int res;
volatile Obj context = ctx;

RequireInputStream(SELF_NAME, instream);
RequireOutputStream(SELF_NAME, outstream);
Expand All @@ -853,52 +880,61 @@

LockCurrentOutput(TRUE);

// get the starting time
UInt oldPrintObjState = SetPrintObjState(0);
// save the old print state
volatile UInt oldPrintObjState = SetPrintObjState(0);

// now do the reading
while (1) {
Obj evalResult;
BOOL dualSemicolon;
UInt oldtime = SyTime();

// read and evaluate the command
SetPrintObjState(0);
ExecStatus status =
ReadEvalCommand(context, &input, &evalResult, &dualSemicolon);

// stop the stopwatch
UpdateTime(oldtime);

// handle ordinary command
if (status == STATUS_END && evalResult != 0) {
UpdateLast(evalResult);
if (!dualSemicolon) {
ViewObjHandler(evalResult);
BOOL rethrow = FALSE;

GAP_TRY
{
// now do the reading
while (1) {
Obj evalResult;
BOOL dualSemicolon;
UInt oldtime = SyTime();

// read and evaluate the command
SetPrintObjState(0);
ExecStatus status =
ReadEvalCommand(context, &input, &evalResult, &dualSemicolon);

// stop the stopwatch
UpdateTime(oldtime);

// handle ordinary command
if (status == STATUS_END && evalResult != 0) {
UpdateLast(evalResult);
if (!dualSemicolon) {
ViewObjHandler(evalResult);
}
}
}

// handle return-value or return-void command
else if (status == STATUS_RETURN) {
Pr("'return' must not be used in file read-eval loop\n", 0, 0);
}
// handle return-value or return-void command
else if (status == STATUS_RETURN) {
Pr("'return' must not be used in file read-eval loop\n", 0, 0);
}

// handle quit command or <end-of-file>
else if (status == STATUS_EOF || status == STATUS_QUIT ||
status == STATUS_QQUIT) {
break;
// handle quit command or <end-of-file>
else if (status == STATUS_EOF || status == STATUS_QUIT ||
status == STATUS_QQUIT) {
break;
}
}
}
GAP_CATCH

Check warning on line 924 in src/streams.c

View check run for this annotation

Codecov / codecov/patch

src/streams.c#L924

Added line #L924 was not covered by tests
{
rethrow = TRUE;

Check warning on line 926 in src/streams.c

View check run for this annotation

Codecov / codecov/patch

src/streams.c#L926

Added line #L926 was not covered by tests
}

SetPrintObjState(oldPrintObjState);

LockCurrentOutput(FALSE);

res = CloseInput(&input);
GAP_ASSERT(res);

res &= CloseOutput(&output);
GAP_ASSERT(res);

if (rethrow)
GAP_THROW();

Check warning on line 937 in src/streams.c

View check run for this annotation

Codecov / codecov/patch

src/streams.c#L937

Added line #L937 was not covered by tests

return res ? True : False;
}
Expand All @@ -914,7 +950,18 @@
if (!OpenInputFileOrStream(SELF_NAME, &input, inputObj))
return False;

Obj func = READ_AS_FUNC(&input);
Obj func;

GAP_TRY
{
func = READ_AS_FUNC(&input);
}
GAP_CATCH

Check warning on line 959 in src/streams.c

View check run for this annotation

Codecov / codecov/patch

src/streams.c#L959

Added line #L959 was not covered by tests
{
CloseInput(&input);
GAP_THROW();

Check warning on line 962 in src/streams.c

View check run for this annotation

Codecov / codecov/patch

src/streams.c#L961-L962

Added lines #L961 - L962 were not covered by tests
}

if (!CloseInput(&input)) {
ErrorQuit("Panic: READ_AS_FUNC cannot close input", 0, 0);
}
Expand Down
Loading