Skip to content

Commit 07f6fa1

Browse files
committed
Better handling of temp files during tests.
1 parent d47be66 commit 07f6fa1

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

test/src/lingot-test-io-config.c

+22-8
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,23 @@ void lingot_test_io_config(void) {
155155

156156
// we check that we can save and load it again
157157

158-
filename = tmpnam(NULL);
159-
lingot_io_config_save(config, filename);
160-
ok = lingot_io_config_load(config, filename);
158+
// although the use of tmpnam() is flagged as 'dangerous', it is the only
159+
// standard way to get the temp file name with full path, and it is used
160+
// only during the tests anyway.
161+
char temp_filename_buffer[L_tmpnam];
162+
char* temp_filename;
163+
temp_filename = tmpnam(temp_filename_buffer);
164+
if (!temp_filename) {
165+
fprintf(stderr, "%s\n", "warning: temporary file cannot be created");
166+
return; // we allow to continue with the distcheck.
167+
}
168+
169+
lingot_io_config_save(config, temp_filename);
170+
ok = lingot_io_config_load(config, temp_filename);
161171
CU_ASSERT(ok);
162172
check_last_configs(config);
163173

164-
ok = !remove(filename);
174+
ok = !remove(temp_filename);
165175
CU_ASSERT(ok);
166176

167177
// -----------
@@ -174,13 +184,17 @@ void lingot_test_io_config(void) {
174184

175185
// we check that we can save and load it again
176186

177-
filename = tmpnam(NULL);
178-
lingot_io_config_save(config, filename);
179-
ok = lingot_io_config_load(config, filename);
187+
temp_filename = tmpnam(temp_filename_buffer);
188+
if (!temp_filename) {
189+
fprintf(stderr, "%s\n", "warning: temporary file cannot be created");
190+
return; // we allow to continue with the distcheck.
191+
}
192+
lingot_io_config_save(config, temp_filename);
193+
ok = lingot_io_config_load(config, temp_filename);
180194
CU_ASSERT(ok);
181195
check_last_configs(config);
182196

183-
ok = !remove(filename);
197+
ok = !remove(temp_filename);
184198
CU_ASSERT(ok);
185199

186200
// -----------

0 commit comments

Comments
 (0)