From 7bf1094e8253e19753ae467ab5b86a18d9abd9f3 Mon Sep 17 00:00:00 2001 From: Florian Weimer Date: Mon, 18 May 2020 18:11:48 +0200 Subject: nptl: Use __pthread_attr_copy in pthread_setattr_default_np Reviewed-by: Carlos O'Donell Tested-by: Carlos O'Donell --- nptl/pthread_setattr_default_np.c | 53 +++++++++++---------------------------- 1 file changed, 15 insertions(+), 38 deletions(-) (limited to 'nptl') diff --git a/nptl/pthread_setattr_default_np.c b/nptl/pthread_setattr_default_np.c index d6a46624b5..eb5d24d3bd 100644 --- a/nptl/pthread_setattr_default_np.c +++ b/nptl/pthread_setattr_default_np.c @@ -26,7 +26,6 @@ int pthread_setattr_default_np (const pthread_attr_t *in) { const struct pthread_attr *real_in; - struct pthread_attr attrs; int ret; real_in = (struct pthread_attr *) in; @@ -58,49 +57,27 @@ pthread_setattr_default_np (const pthread_attr_t *in) if (real_in->flags & ATTR_FLAG_STACKADDR) return EINVAL; - attrs = *real_in; + union pthread_attr_transparent temp; + ret = __pthread_attr_copy (&temp.external, in); + if (ret != 0) + return ret; - /* Now take the lock because we start writing into + /* Now take the lock because we start accessing __default_pthread_attr. */ lll_lock (__default_pthread_attr_lock, LLL_PRIVATE); - /* Free the cpuset if the input is 0. Otherwise copy in the cpuset - contents. */ - size_t cpusetsize = attrs.cpusetsize; - if (cpusetsize == 0) - { - free (__default_pthread_attr.cpuset); - __default_pthread_attr.cpuset = NULL; - } - else if (cpusetsize == __default_pthread_attr.cpusetsize) - { - attrs.cpuset = __default_pthread_attr.cpuset; - memcpy (attrs.cpuset, real_in->cpuset, cpusetsize); - } - else - { - /* This may look wrong at first sight, but it isn't. We're freeing - __default_pthread_attr.cpuset and allocating to attrs.cpuset because - we'll copy over all of attr to __default_pthread_attr later. */ - cpu_set_t *newp = realloc (__default_pthread_attr.cpuset, - cpusetsize); - - if (newp == NULL) - { - ret = ENOMEM; - goto out; - } - - attrs.cpuset = newp; - memcpy (attrs.cpuset, real_in->cpuset, cpusetsize); - } + /* Preserve the previous stack size (see above). */ + if (temp.internal.stacksize == 0) + temp.internal.stacksize = __default_pthread_attr.stacksize; + + /* Destroy the old attribute structure because it will be + overwritten. */ + __pthread_attr_destroy ((pthread_attr_t *) &__default_pthread_attr); - /* We don't want to accidentally set the default stacksize to zero. */ - if (attrs.stacksize == 0) - attrs.stacksize = __default_pthread_attr.stacksize; - __default_pthread_attr = attrs; + /* __default_pthread_attr takes ownership, so do not free + attrs.internal after this point. */ + __default_pthread_attr = temp.internal; - out: lll_unlock (__default_pthread_attr_lock, LLL_PRIVATE); return ret; } -- cgit v1.2.3