Skip to content

Commit

Permalink
util: plug a file descriptor leak
Browse files Browse the repository at this point in the history
It's possible that a file descriptor would leak if fdopen() failed for
any reason. Found by Coverity.
  • Loading branch information
daniloegea committed Jul 19, 2023
1 parent 3e2d7ed commit 6517885
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,14 @@ netplan_util_dump_yaml_subtree(const char* prefix, int input_fd, int output_fd,
cleanup:
if (input)
fclose(input);
else
close(in_dup);

if (output)
fclose(output);
else
close(out_dup);

if (yaml_path)
g_strfreev(yaml_path);
return ret;
Expand Down
14 changes: 13 additions & 1 deletion tests/test_libnetplan.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,13 +605,25 @@ def test_create_yaml_patch_bad_syntax(self):
patchfile.seek(0, io.SEEK_END)
self.assertEqual(patchfile.tell(), 0)

def test_dump_yaml_subtree_bad_file_perms(self):
def test_dump_yaml_subtree_bad_input_file_perms(self):
input_file = os.path.join(self.workdir.name, 'input.yaml')
with open(input_file, "w") as f, tempfile.TemporaryFile() as output:
with self.assertRaises(libnetplan.NetplanFileException) as context:
libnetplan.dump_yaml_subtree('network', f, output)
self.assertIn('Invalid argument', str(context.exception))

def test_dump_yaml_subtree_bad_output_file_perms(self):
input_file = os.path.join(self.workdir.name, 'input.yaml')
output_file = os.path.join(self.workdir.name, 'output.yaml')
with open(input_file, 'w') as input, open(output_file, 'w') as output:
input.write('network: {}')
output.write('')

with open(input_file, "r") as f, open(output_file, 'r') as output:
with self.assertRaises(libnetplan.NetplanFileException) as context:
libnetplan.dump_yaml_subtree('network', f, output)
self.assertIn('Invalid argument', str(context.exception))

def test_dump_yaml_subtree_bad_yaml_outside(self):
input_file = os.path.join(self.workdir.name, 'input.yaml')
with open(input_file, "w+") as f, tempfile.TemporaryFile() as output:
Expand Down

0 comments on commit 6517885

Please sign in to comment.