Skip to content

Commit

Permalink
Close stream first, close fd only if not stream
Browse files Browse the repository at this point in the history
  • Loading branch information
ddeclerck committed Jan 28, 2025
1 parent 7995a57 commit 3b40cf1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
6 changes: 6 additions & 0 deletions libcob/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@

2025-01-28 David Declerck <[email protected]>

* fileio.c (cob_file_close): try to close the file stream
first, close the file descriptor only if the stream is NULL
(fixes assertions under MSVC debug)

2024-11-06 David Declerck <[email protected]>

Reverted change 2022-02-21 to integrate change
Expand Down
27 changes: 6 additions & 21 deletions libcob/fileio.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2002-2012, 2014-2023 Free Software Foundation, Inc.
Copyright (C) 2002-2012, 2014-2025 Free Software Foundation, Inc.
Written by Keisuke Nishida, Roger While, Simon Sobisch, Ron Norman
This file is part of GnuCOBOL.
Expand Down Expand Up @@ -4764,32 +4764,17 @@ cob_file_close (cob_file_api *a, cob_file *f, const int opt)
f->file_pid = 0;
return COB_STATUS_00_SUCCESS;
}
#ifdef _MSC_VER /* explicit only stream or fd close with this compiler */
if (f->file != NULL) {
fclose ((FILE *)f->file);
f->file = NULL;
#ifdef _WIN32
/* at least on MSVC that closes the underlying file descriptor, too */
f->fd = -1;
#endif
}
#endif
} else {
if (f->fd >= 0) {
close (f->fd);
f->fd = -1;
#ifdef _MSC_VER
f->file = NULL;
#endif
}
}
#ifndef _MSC_VER /* explicit disallowed there, is that the same for other compilers? */
if (f->file != NULL) {
/* This should close the underlying fd */
fclose ((FILE *)f->file);
f->file = NULL;
f->fd = -1;
} else
if (f->fd >= 0) {
close (f->fd);
f->fd = -1;
}
#endif
if (opt == COB_CLOSE_NO_REWIND) {
f->open_mode = COB_OPEN_CLOSED;
return COB_STATUS_07_SUCCESS_NO_UNIT;
Expand Down

0 comments on commit 3b40cf1

Please sign in to comment.