From owner-svn-src-all@FreeBSD.ORG Wed Oct 27 09:59:43 2010 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 64087106564A; Wed, 27 Oct 2010 09:59:43 +0000 (UTC) (envelope-from davidxu@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 52B398FC18; Wed, 27 Oct 2010 09:59:43 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id o9R9xh8t098987; Wed, 27 Oct 2010 09:59:43 GMT (envelope-from davidxu@svn.freebsd.org) Received: (from davidxu@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id o9R9xh6g098985; Wed, 27 Oct 2010 09:59:43 GMT (envelope-from davidxu@svn.freebsd.org) Message-Id: <201010270959.o9R9xh6g098985@svn.freebsd.org> From: David Xu Date: Wed, 27 Oct 2010 09:59:43 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r214413 - head/lib/libthr/thread X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 27 Oct 2010 09:59:43 -0000 Author: davidxu Date: Wed Oct 27 09:59:43 2010 New Revision: 214413 URL: http://svn.freebsd.org/changeset/base/214413 Log: Check small set and reject it, this is how kernel did. Always use the size kernel is using. Modified: head/lib/libthr/thread/thr_attr.c Modified: head/lib/libthr/thread/thr_attr.c ============================================================================== --- head/lib/libthr/thread/thr_attr.c Wed Oct 27 09:29:03 2010 (r214412) +++ head/lib/libthr/thread/thr_attr.c Wed Oct 27 09:59:43 2010 (r214413) @@ -141,19 +141,14 @@ _pthread_attr_get_np(pthread_t pthread, struct pthread *curthread; struct pthread_attr attr, *dst; int ret; - size_t cpusetsize; + size_t kern_size; if (pthread == NULL || dstattr == NULL || (dst = *dstattr) == NULL) return (EINVAL); - cpusetsize = _get_kern_cpuset_size(); - if (dst->cpusetsize < cpusetsize) { - char *newset = realloc(dst->cpuset, cpusetsize); - if (newset == NULL) - return (errno); - memset(newset + dst->cpusetsize, 0, cpusetsize - - dst->cpusetsize); - dst->cpuset = (cpuset_t *)newset; - dst->cpusetsize = cpusetsize; + kern_size = _get_kern_cpuset_size(); + if (dst->cpuset == NULL) { + dst->cpuset = calloc(1, kern_size); + dst->cpusetsize = kern_size; } curthread = _get_curthread(); if ((ret = _thr_find_thread(curthread, pthread, /*include dead*/0)) != 0) @@ -606,27 +601,25 @@ _pthread_attr_setaffinity_np(pthread_att } return (0); } - - if (cpusetsize > attr->cpusetsize) { - size_t kern_size = _get_kern_cpuset_size(); - if (cpusetsize > kern_size) { - size_t i; - for (i = kern_size; i < cpusetsize; ++i) { - if (((char *)cpusetp)[i]) - return (EINVAL); - } + size_t kern_size = _get_kern_cpuset_size(); + /* Kernel rejects small set, we check it here too. */ + if (cpusetsize < kern_size) + return (ERANGE); + if (cpusetsize > kern_size) { + /* Kernel checks invalid bits, we check it here too. */ + size_t i; + for (i = kern_size; i < cpusetsize; ++i) { + if (((char *)cpusetp)[i]) + return (EINVAL); } - void *newset = realloc(attr->cpuset, cpusetsize); - if (newset == NULL) - return (ENOMEM); - attr->cpuset = newset; - attr->cpusetsize = cpusetsize; - } else { - memset(((char *)attr->cpuset) + cpusetsize, 0, - attr->cpusetsize - cpusetsize); - attr->cpusetsize = cpusetsize; } - memcpy(attr->cpuset, cpusetp, cpusetsize); + if (attr->cpuset == NULL) { + attr->cpuset = calloc(1, kern_size); + if (attr->cpuset == NULL) + return (errno); + attr->cpusetsize = kern_size; + } + memcpy(attr->cpuset, cpusetp, kern_size); ret = 0; } return (ret); @@ -642,16 +635,18 @@ _pthread_attr_getaffinity_np(const pthre if (pattr == NULL || (attr = (*pattr)) == NULL) ret = EINVAL; - else if (attr->cpuset != NULL) { - memcpy(cpusetp, attr->cpuset, MIN(cpusetsize, attr->cpusetsize)); - if (cpusetsize > attr->cpusetsize) - memset(((char *)cpusetp) + attr->cpusetsize, 0, - cpusetsize - attr->cpusetsize); - } else { + else { + /* Kernel rejects small set, we check it here too. */ size_t kern_size = _get_kern_cpuset_size(); - memset(cpusetp, -1, MIN(cpusetsize, kern_size)); + if (cpusetsize < kern_size) + return (ERANGE); + if (attr->cpuset != NULL) + memcpy(cpusetp, attr->cpuset, MIN(cpusetsize, + attr->cpusetsize)); + else + memset(cpusetp, -1, kern_size); if (cpusetsize > kern_size) - memset(((char *)cpusetp) + kern_size, 0, + memset(((char *)cpusetp) + kern_size, 0, cpusetsize - kern_size); } return (ret);