Question about destroy_toolcontext()
Hi Peter,
I read the source of lib/commands/toolcontext.c
and have a question about the following lines:
----------------------------
void destroy_toolcontext(struct cmd_context *cmd){
[snip]
#ifndef VALGRIND_POOL
if (cmd->linebuffer) {
/* Reset stream buffering to defaults */
if (is_valid_fd(STDIN_FILENO) &&
((flags = fcntl(STDIN_FILENO, F_GETFL)) > 0) &&
(flags & O_ACCMODE) != O_WRONLY) {
if (_reopen_stream(stdin, STDIN_FILENO, "r", "stdin", &new_stream)) {
stdin = new_stream;
setlinebuf(stdin);
} else
cmd->linebuffer = NULL; /* Leave buffer in place (deliberate leak) */
} if (is_valid_fd(STDOUT_FILENO) &&
((flags = fcntl(STDOUT_FILENO, F_GETFL)) > 0) &&
(flags & O_ACCMODE) != O_RDONLY) {
if (_reopen_stream(stdout, STDOUT_FILENO, "w", "stdout", &new_stream)) {
stdout = new_stream;
setlinebuf(stdout);
} else
cmd->linebuffer = NULL; /* Leave buffer in place (deliberate leak) */
}
dm_free(cmd->linebuffer);
} #endif
-------------------------------
when cmd->linebuffer is NULL and then dm_free(cmd->linebuffer), won't it make it segfault?
Thanks!
Liuhua
Dne 17.8.2016 v 11:04 Liuhua Wang napsal(a):
Quoted 35 lines
Hi Peter,
I read the source of
lib/commands/toolcontext.c
and have a question about the following lines:----------------------------
voiddestroy_toolcontext(struct cmd_context *cmd){[snip]
#ifndef VALGRIND_POOL if (cmd->linebuffer) { /* Reset stream buffering to defaults */ if (is_valid_fd(STDIN_FILENO) && ((flags = fcntl(STDIN_FILENO, F_GETFL)) > 0) && (flags & O_ACCMODE) != O_WRONLY) { if (_reopen_stream(stdin, STDIN_FILENO, "r", "stdin", &new_stream)) { stdin = new_stream; setlinebuf(stdin); } else cmd->linebuffer = NULL; /* Leave buffer in place (deliberate leak) */ }if (is_valid_fd(STDOUT_FILENO) && ((flags = fcntl(STDOUT_FILENO, F_GETFL)) > 0) && (flags & O_ACCMODE) != O_RDONLY) { if (_reopen_stream(stdout, STDOUT_FILENO, "w", "stdout", &new_stream)) { stdout = new_stream; setlinebuf(stdout); } else cmd->linebuffer = NULL; /* Leave buffer in place (deliberate leak) */ } dm_free(cmd->linebuffer); }
Quoted 4 lines
#endif
-------------------------------
whencmd->linebufferis NULL and thendm_free(cmd->linebuffer), won't it make it segfault?
dm_free() accepts NULL (just like 'free()' - for details see: man 3 free.
Regards
Zdenek
Hi Zdenek,
Quoted 1 line
dm_free()accepts NULL (just like 'free()' - for details see: man 3 free.
I see. Thank you very much and sorry for the noise.
Best,
Liuhua.
Quoted 10 lines
Regards
Zdenek
--
lvm-devel mailing list
lvm-devel at redhat.com
https://www.redhat.com/mailman/listinfo/lvm-devel
Quoted 35 lines · Liuhua Wang
Hi Peter,
I read the source of
lib/commands/toolcontext.c
and have a question about the following lines:----------------------------
voiddestroy_toolcontext(struct cmd_context *cmd){[snip]
#ifndef VALGRIND_POOL if (cmd->linebuffer) { /* Reset stream buffering to defaults */ if (is_valid_fd(STDIN_FILENO) && ((flags = fcntl(STDIN_FILENO, F_GETFL)) > 0) && (flags & O_ACCMODE) != O_WRONLY) { if (_reopen_stream(stdin, STDIN_FILENO, "r", "stdin", &new_stream)) { stdin = new_stream; setlinebuf(stdin); } else cmd->linebuffer = NULL; /* Leave buffer in place (deliberate leak) */ }if (is_valid_fd(STDOUT_FILENO) && ((flags = fcntl(STDOUT_FILENO, F_GETFL)) > 0) && (flags & O_ACCMODE) != O_RDONLY) { if (_reopen_stream(stdout, STDOUT_FILENO, "w", "stdout", &new_stream)) { stdout = new_stream; setlinebuf(stdout); } else cmd->linebuffer = NULL; /* Leave buffer in place (deliberate leak) */ } dm_free(cmd->linebuffer); }
Quoted 4 lines
#endif
-------------------------------
whencmd->linebufferis NULL and thendm_free(cmd->linebuffer), won't it make it segfault?
The "dm_free" is just a wrapper over "free" call which currently ends up as either "dm_free_aux" (libdm/mm/dbg_malloc.c) call with some extra code added around "free" if memory debugging is enabled or directly as pure "free" call. In both cases, when NULL value is passed, there's no operation done. So it's safe to call dm_free with NULL as it may happen in the code you pasted above...
Signature · 1 line
Peter