From 425d4ab4b75ab81c9dcb586781a808b5b84989fc Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Tue, 7 Mar 2023 12:37:39 -0800 Subject: [PATCH] CLOSE FILE HANDLE: Fix bugs and add test. In addition to the particular bug that was reported, there was a memory leak. This commit fixes both. Reported by knassen@chartermi.net. --- src/data/file-handle-def.c | 2 + src/language/commands/file-handle.c | 8 +++- tests/language/commands/file-handle.at | 73 +++++++++++++++++++++++++++++++++- 3 files changed, 81 insertions(+), 2 deletions(-) diff --git a/src/data/file-handle-def.c b/src/data/file-handle-def.c index 04f773452..e6bedcca8 100644 --- a/src/data/file-handle-def.c +++ b/src/data/file-handle-def.c @@ -191,6 +191,8 @@ void fh_unname (struct file_handle *handle) { assert (handle->ref_cnt > 1); + if (fh_get_default_handle () == handle) + fh_set_default_handle (NULL); if (handle != fh_inline_file () && handle->id != NULL) unname_handle (handle); } diff --git a/src/language/commands/file-handle.c b/src/language/commands/file-handle.c index de797b3c9..3c2494ca3 100644 --- a/src/language/commands/file-handle.c +++ b/src/language/commands/file-handle.c @@ -289,9 +289,15 @@ cmd_close_file_handle (struct lexer *lexer, struct dataset *ds UNUSED) return CMD_CASCADING_FAILURE; handle = fh_from_id (lex_tokcstr (lexer)); if (handle == NULL) - return CMD_CASCADING_FAILURE; + { + lex_next_error (lexer, 0, 0, _("No file handle named %s."), + lex_tokcstr (lexer)); + return CMD_CASCADING_FAILURE; + } + lex_get (lexer); fh_unname (handle); + fh_unref (handle); return CMD_SUCCESS; } diff --git a/tests/language/commands/file-handle.at b/tests/language/commands/file-handle.at index 74989ba49..5088a3fb7 100644 --- a/tests/language/commands/file-handle.at +++ b/tests/language/commands/file-handle.at @@ -145,4 +145,75 @@ AT_CHECK([pspp --testing-mode -O format=csv insert.sps], [1], [dnl 20 | FILE HANDLE y/NAME='x.txt'/MODE=FIXED. | ^~~~~" ]) -AT_CLEANUP \ No newline at end of file +AT_CLEANUP + +AT_SETUP([CLOSE FILE HANDLE]) +AT_DATA([wiggle.txt], [dnl +1 +2 +5 +109 +]) +AT_DATA([wobble.txt], [dnl +7 +8 +9 +]) +AT_DATA([file-handle.sps], [dnl +FILE HANDLE myhandle /NAME='wiggle.txt'. +DATA LIST LIST FILE=myhandle /x *. +LIST. + +CLOSE FILE HANDLE myhandle. + +FILE HANDLE myhandle /NAME='wobble.txt'. +DATA LIST LIST FILE=myhandle /x *. +LIST. +]) +AT_CHECK([pspp -O format=csv file-handle.sps], [0], [dnl +Table: Reading free-form data from myhandle. +Variable,Format +x,F8.0 + +Table: Data List +x +1.00 +2.00 +5.00 +109.00 + +Table: Reading free-form data from myhandle. +Variable,Format +x,F8.0 + +Table: Data List +x +7.00 +8.00 +9.00 +]) +AT_CLEANUP + +AT_SETUP([CLOSE FILE HANDLE syntax errors]) +AT_DATA([wiggle.txt], [dnl +1 +2 +5 +109 +]) +AT_DATA([file-handle.sps], [dnl +FILE HANDLE myhandle /NAME='wiggle.txt'. +CLOSE FILE HANDLE myhandle **. +CLOSE FILE HANDLE myhandle. +CLOSE FILE HANDLE myhandle. +CLOSE FILE HANDLE **. +]) +AT_DATA([insert.sps], [dnl +INSERT FILE='file-handle.sps' ERROR=IGNORE. +]) +AT_CHECK([pspp -O format=csv insert.sps], [1], [dnl +"insert.sps:1.37-1.42: error: INSERT: Syntax error expecting CONTINUE or STOP. + 1 | INSERT FILE='file-handle.sps' ERROR=IGNORE. + | ^~~~~~" +]) +AT_CLEANUP -- 2.11.4.GIT