diff --git a/lib/libc/gen/swapcontext.c b/lib/libc/gen/swapcontext.c index d58ae6dc6d30..eeb2f782a3f5 100644 --- a/lib/libc/gen/swapcontext.c +++ b/lib/libc/gen/swapcontext.c @@ -27,29 +27,32 @@ #include __FBSDID("$FreeBSD$"); +#include +#include +#include + #include -#include #include -#include __weak_reference(__swapcontext, swapcontext); int __swapcontext(ucontext_t *oucp, const ucontext_t *ucp) { - volatile int swapping; int ret; - if (oucp == NULL || ucp == NULL) { + if ((oucp == NULL) || + (oucp->uc_mcontext.mc_len != sizeof(mcontext_t)) || + (ucp == NULL) || + (ucp->uc_mcontext.mc_len != sizeof(mcontext_t))) { errno = EINVAL; - ret = -1; - } else { - swapping = 0; - ret = getcontext(oucp); - if (ret == 0 && swapping == 0) { - swapping = 1; - ret = setcontext(ucp); - } + return (-1); + } + oucp->uc_flags &= ~UCF_SWAPPED; + ret = getcontext(oucp); + if ((ret == 0) && !(oucp->uc_flags & UCF_SWAPPED)) { + oucp->uc_flags |= UCF_SWAPPED; + ret = setcontext(ucp); } return (ret); }