From patchwork Tue May 19 10:44:34 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 39306 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id F18433948A69; Tue, 19 May 2020 10:44:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F18433948A69 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1589885082; bh=KwK1GvxiDoLUrmkvUchIxMiCedGPXk0IfquX/Qrv3dc=; h=To:Subject:In-Reply-To:References:Date:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=n89pxF86SJ50LKNZ5zI47Emiu88Bp/Iwk3Pit97BJ1vAZkVak8eQYWzZ92A3UccFW 402WqcOUAcZW+D/ogsAlVYlpIWOBwL4gtrCypgxGMG+rnlSeujKbaXjVu99tR3l0H5 xMHdodTt003Cc8IUZHDUZI6JIuHfFhTml7Nw9Vls= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-1.mimecast.com (us-smtp-2.mimecast.com [205.139.110.61]) by sourceware.org (Postfix) with ESMTP id 3BEAE3948829 for ; Tue, 19 May 2020 10:44:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 3BEAE3948829 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-108-2qs9Dtj7MVODtPRhudsNnA-1; Tue, 19 May 2020 06:44:37 -0400 X-MC-Unique: 2qs9Dtj7MVODtPRhudsNnA-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5CD2C460 for ; Tue, 19 May 2020 10:44:36 +0000 (UTC) Received: from oldenburg2.str.redhat.com (ovpn-113-18.ams2.redhat.com [10.36.113.18]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D40DB8206B for ; Tue, 19 May 2020 10:44:35 +0000 (UTC) To: libc-alpha@sourceware.org Subject: [PATCH 09/19] nptl: Use __pthread_attr_copy in pthread_setattr_default_np In-Reply-To: References: Message-Id: <74f2d4c0a6cd892c70d693fff4bd75a65c4c2e27.1589884403.git.fweimer@redhat.com> Date: Tue, 19 May 2020 12:44:34 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Florian Weimer via Libc-alpha From: Florian Weimer Reply-To: Florian Weimer Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" --- nptl/pthread_setattr_default_np.c | 53 +++++++++---------------------- 1 file changed, 15 insertions(+), 38 deletions(-) Reviewed-by: Carlos O'Donell Tested-by: Carlos O'Donell 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; }