Skip to content

Commit 4199180

Browse files
committed
Merge remote-tracking branch 'upstream/master' into gc4
2 parents 7995a57 + 4f6f53e commit 4199180

File tree

2 files changed

+45
-54
lines changed

2 files changed

+45
-54
lines changed

libcob/ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11

2+
2025-01-28 David Declerck <david.declerck@ocamlpro.com>
3+
4+
* fileio.c (cob_file_close): try to close the file stream
5+
first, close the file descriptor only if the stream is NULL
6+
(fixes assertions under MSVC debug)
7+
28
2024-11-06 David Declerck <david.declerck@ocamlpro.com>
39

410
Reverted change 2022-02-21 to integrate change

libcob/fileio.c

Lines changed: 39 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (C) 2002-2012, 2014-2023 Free Software Foundation, Inc.
2+
Copyright (C) 2002-2012, 2014-2025 Free Software Foundation, Inc.
33
Written by Keisuke Nishida, Roger While, Simon Sobisch, Ron Norman
44
55
This file is part of GnuCOBOL.
@@ -4725,71 +4725,56 @@ cob_file_close (cob_file_api *a, cob_file *f, const int opt)
47254725
#endif
47264726
}
47274727
/* Close the file */
4728-
if (f->organization == COB_ORG_LINE_SEQUENTIAL) {
4729-
if (f->flag_is_pipe) {
4730-
if (f->file_pid) {
4731-
if (f->file)
4732-
fclose (f->file);
4733-
if (f->fileout
4734-
&& f->fileout != f->file)
4735-
fclose (f->fileout);
4728+
if (f->flag_is_pipe) {
4729+
if (f->file_pid) {
4730+
if (f->file) {
4731+
fclose (f->file);
4732+
}
4733+
if (f->fileout
4734+
&& f->fileout != f->file) {
4735+
fclose (f->fileout);
4736+
}
47364737
#if defined (HAVE_UNISTD_H) && !(defined (_WIN32))
4737-
{
4738-
int sts;
4739-
errno = 0;
4740-
kill (f->file_pid, 0);
4741-
if (errno == ESRCH) {
4742-
waitpid (f->file_pid, &sts, WNOHANG);
4743-
}
4744-
else {
4745-
cob_sleep_msec (50);
4746-
kill (f->file_pid, SIGKILL);
4747-
cob_sleep_msec (50);
4748-
waitpid (f->file_pid, &sts, 0);
4749-
}
4738+
{
4739+
int sts;
4740+
errno = 0;
4741+
kill (f->file_pid, 0);
4742+
if (errno == ESRCH) {
4743+
waitpid (f->file_pid, &sts, WNOHANG);
4744+
}
4745+
else {
4746+
cob_sleep_msec (50);
4747+
kill (f->file_pid, SIGKILL);
4748+
cob_sleep_msec (50);
4749+
waitpid (f->file_pid, &sts, 0);
4750+
}
47504751
}
47514752
#elif defined (_WIN32)
4752-
{
4753-
char buff[COB_MINI_BUFF];
4754-
snprintf((char *)&buff, COB_MINI_MAX, "TASKKILL /PID %d", f->file_pid);
4755-
system(buff);
4756-
}
4757-
#endif
4758-
} else {
4759-
pclose ((FILE *)f->file);
4753+
{
4754+
char buff[COB_MINI_BUFF];
4755+
snprintf ((char *)&buff, COB_MINI_MAX, "TASKKILL /PID %d", f->file_pid);
4756+
system (buff);
47604757
}
4761-
f->flag_is_pipe = 0;
4762-
f->file = f->fileout = NULL;
4763-
f->fd = f->fdout = -1;
4764-
f->file_pid = 0;
4765-
return COB_STATUS_00_SUCCESS;
4766-
}
4767-
#ifdef _MSC_VER /* explicit only stream or fd close with this compiler */
4768-
if (f->file != NULL) {
4769-
fclose ((FILE *)f->file);
4770-
f->file = NULL;
4771-
#ifdef _WIN32
4772-
/* at least on MSVC that closes the underlying file descriptor, too */
4773-
f->fd = -1;
4774-
#endif
4775-
}
4776-
#endif
4777-
} else {
4778-
if (f->fd >= 0) {
4779-
close (f->fd);
4780-
f->fd = -1;
4781-
#ifdef _MSC_VER
4782-
f->file = NULL;
47834758
#endif
4759+
} else {
4760+
pclose ((FILE *)f->file);
47844761
}
4762+
f->flag_is_pipe = 0;
4763+
f->file = f->fileout = NULL;
4764+
f->fd = f->fdout = -1;
4765+
f->file_pid = 0;
4766+
return COB_STATUS_00_SUCCESS;
47854767
}
4786-
#ifndef _MSC_VER /* explicit disallowed there, is that the same for other compilers? */
47874768
if (f->file != NULL) {
4769+
/* This also closes the underlying fd */
47884770
fclose ((FILE *)f->file);
47894771
f->file = NULL;
47904772
f->fd = -1;
4773+
} else
4774+
if (f->fd >= 0) {
4775+
close (f->fd);
4776+
f->fd = -1;
47914777
}
4792-
#endif
47934778
if (opt == COB_CLOSE_NO_REWIND) {
47944779
f->open_mode = COB_OPEN_CLOSED;
47954780
return COB_STATUS_07_SUCCESS_NO_UNIT;

0 commit comments

Comments
 (0)