-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix a crash when running 'unalias r' #133
Conversation
After commit ddaa145, the following set of commands now causes ksh to crash: $ unalias history; unalias r Memory fault When ksh is compiled with -D_std_malloc, the crash always occurs when the 'r' alias is removed with 'unalias r', although with vmalloc 'unalias history' must be run first for the crash to occur. With the native malloc, the crash message is also different: $ unalias history; unalias r free(): invalid pointer Abort This crash happens because when an alias is unset, _nv_unset removes the NV_NOFREE flag which results in an invalid use of free(3) as nv_isattr no longer detects NV_NOFREE afterward. The history and r aliases shouldn't be freed from memory by nv_delete because those aliases are given the NV_NOFREE attribute. src/cmd/ksh93/bltins/typeset.c: - Save the state of NV_NOFREE for aliases to fix the crash caused by 'unalias r'. Also of note, using the return value from nv_isattr is incorrect since it's just a boolean value. The actual flag passed must be NV_NOFREE to prevent bugs (like the crash). src/cmd/ksh93/tests/alias.sh: - Use unalias on both history and r to check for the crash. 'unalias -a' can't be used to replicate the crash.
Looks like I done goofed again. Thank you for this fix! |
I'm still going to give this some more thought before applying it, though. Maybe the real bug is that I'm going to be busy with other things in the next few days, so this will have to wait for a while. Meanwhile if you have any thoughts on that I'd appreciate them. |
Well, this diff diff --git a/src/cmd/ksh93/sh/name.c b/src/cmd/ksh93/sh/name.c
index 2a0c216..d5d5276 100644
--- a/src/cmd/ksh93/sh/name.c
+++ b/src/cmd/ksh93/sh/name.c
@@ -2536,7 +2536,7 @@ done:
env_delete(shp->env,nv_name(np));
if(!(flags&NV_EXPORT) || nv_isattr(np,NV_EXPORT))
np->nvenv = 0;
- nv_setattr(np,0);
+ nv_setattr(np,nv_isattr(np,NV_NOFREE));
}
else
{ fixes the crash, but also causes multiple regressions:
So, I'll merge your fix then. |
Since aliases don't have virtual subshell tables, no need to _nv_unset() them first.
Actually, I think your fix can be made more concise. As of ec88886, aliases don't have virtual subshell tables. So there's no need to mark an alias node as unset first with I've pushed a commit with that change to your fork. Since this is your pull request, please test it and let me know if you give it the OK. |
Actually, never mind. My version introduces a memory leak. Apparently, we must |
Actually, never mind. My version introduces a memory leak. Apparently, we must _nv_unset() before calling nv_delete(). Which makes sense because nv_delete() only frees the node itself, _nv_unset() is what frees the value. This reverts commit f6ddacf.
You're definitely not correct about one thing, though: ksh/src/cmd/ksh93/include/nval.h Line 205 in 7e5fd3e
& ) and not a logical AND (&& ). So you could even use this to test for multiple flags at the same time, as in nv_isattr(np,FLAG1|FLAG2|FLAG3) and it will return the flags that are set.
|
After commit ddaa145, the following set of commands now causes ksh to crash:
When ksh is compiled with
-D_std_malloc
, the crash always occurs when ther
alias is removed withunalias r
, although with vmallocunalias history
must be run first for the crash to occur. The crash message is also different when the native malloc is used instead of vmalloc:This crash happens because the
NV_NOFREE
flag disappears after_nv_unset
, which results innv_isattr
becoming a no-op (as it always returns zero).nv_delete
then attempts an invalid free, which causes the crash:ksh/src/cmd/ksh93/bltins/typeset.c
Lines 1295 to 1296 in 05683ec
ksh/src/cmd/ksh93/bltins/typeset.c
Line 1314 in 05683ec
The
history
andr
aliases shouldn't be freed from memory bynv_delete
because those aliases are given theNV_NOFREE
flag:ksh/src/cmd/ksh93/data/aliases.c
Lines 32 to 33 in 05683ec
This pull request fixes the crash by saving the
NV_NOFREE
flag for aliases to prevent an invalid free from occurring.