Skip to content

Commit 4bde88a

Browse files
committed
FIX: write file fails were silently ignored; closing port on read errors before throwing
1 parent 3c43f44 commit 4bde88a

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/core/p-file.c

+17-8
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ REBINT Mode_Syms[] = {
267267

268268
/***********************************************************************
269269
**
270-
*/ static void Read_File_Port(REBSER *port, REBREQ *file, REBVAL *path, REBCNT args, REBCNT len)
270+
*/ static void Read_File_Port(REBREQ *file, REBVAL *path, REBCNT args, REBCNT len)
271271
/*
272272
** Read from a file port.
273273
**
@@ -283,7 +283,11 @@ REBINT Mode_Syms[] = {
283283
// Do the read, check for errors:
284284
file->data = BIN_HEAD(ser);
285285
file->length = len;
286-
if (OS_DO_DEVICE(file, RDC_READ) < 0) Trap_Port(RE_READ_ERROR, port, file->error);
286+
if (OS_DO_DEVICE(file, RDC_READ) < 0) return; // Trap_Port(RE_READ_ERROR, port, file->error);
287+
// Not throwing the error from here!
288+
// We may want to close the port before error reporting.
289+
// It is passed above in the file->error
290+
287291
SERIES_TAIL(ser) = file->actual;
288292
STR_TERM(ser);
289293

@@ -351,7 +355,9 @@ REBINT Mode_Syms[] = {
351355
//Trap1(PE_BAD_ARGUMENT, data);
352356
}
353357
file->length = len;
354-
OS_DO_DEVICE(file, RDC_WRITE);
358+
OS_DO_DEVICE(file, RDC_WRITE); // don't report error here!
359+
// We may want to close the port before error reporting.
360+
// It is passed above in the file->error
355361

356362
if(n > 0) {
357363
// remove the temporary added newline from the series
@@ -427,7 +433,7 @@ REBINT Mode_Syms[] = {
427433
REBREQ *file = 0;
428434
REBCNT args = 0;
429435
REBCNT len;
430-
REBINT result;
436+
REBINT result, error;
431437
REBOOL opened = FALSE; // had to be opened (shortcut case)
432438

433439
//Print("FILE ACTION: %r", Get_Action_Word(action));
@@ -464,14 +470,15 @@ REBINT Mode_Syms[] = {
464470

465471
if (args & AM_READ_SEEK) Set_Seek(file, D_ARG(ARG_READ_INDEX));
466472
len = Set_Length(ds, file, ARG_READ_PART);
467-
Read_File_Port(port, file, path, args, len);
473+
Read_File_Port(file, path, args, len);
468474

475+
error = (REBINT)file->error; // store error value, before closing the file!
469476
if (opened) {
470477
OS_DO_DEVICE(file, RDC_CLOSE);
471478
Cleanup_File(file);
472479
}
473480

474-
if (file->error) Trap_Port(RE_READ_ERROR, port, file->error);
481+
if (error) Trap_Port(RE_READ_ERROR, port, error);
475482
break;
476483

477484
case A_APPEND:
@@ -522,12 +529,14 @@ REBINT Mode_Syms[] = {
522529
Write_File_Port(file, spec, len, args);
523530

524531
file->file.index += file->actual;
532+
533+
error = (REBINT)file->error; // store error value, before closing the file!
525534
if (opened) {
526535
OS_DO_DEVICE(file, RDC_CLOSE);
527536
Cleanup_File(file);
528537
}
529538

530-
if (file->error) Trap1(RE_WRITE_ERROR, path);
539+
if (error) Trap_Port(RE_WRITE_ERROR, port, error);
531540
*D_RET = *path;
532541
break;
533542

@@ -542,7 +551,7 @@ REBINT Mode_Syms[] = {
542551
case A_COPY:
543552
if (!IS_OPEN(file)) Trap1(RE_NOT_OPEN, path); //!!!! wrong msg
544553
len = Set_Length(ds, file, 2);
545-
Read_File_Port(port, file, path, args, len);
554+
Read_File_Port(file, path, args, len);
546555
break;
547556

548557
case A_OPENQ:

0 commit comments

Comments
 (0)