Blame SOURCES/db-5.3.21-memp_stat-upstream-fix.patch

b30d9d
diff -r -u db-5.3.21_orig/src/mp/mp_stat.c db-5.3.21/src/mp/mp_stat.c
b30d9d
--- db-5.3.21_orig/src/mp/mp_stat.c	2012-05-12 01:57:53.000000000 +0800
b30d9d
+++ db-5.3.21/src/mp/mp_stat.c	2015-05-19 15:07:09.000000000 +0800
b30d9d
@@ -87,6 +87,13 @@
b30d9d
 	u_int32_t i;
b30d9d
 	uintmax_t tmp_wait, tmp_nowait;
b30d9d
 
b30d9d
+	/*
b30d9d
+	 * The array holding the lengths related to the buffer allocated for *fspp.
b30d9d
+	 * The first element of the array holds the number of entries allocated.
b30d9d
+	 * The second element of the array holds the total number of bytes allocated.
b30d9d
+	 */
b30d9d
+	u_int32_t fsp_len[2];
b30d9d
+
b30d9d
 	dbmp = env->mp_handle;
b30d9d
 	mp = dbmp->reginfo[0].primary;
b30d9d
 
b30d9d
@@ -193,32 +200,53 @@
b30d9d
 	if (fspp != NULL) {
b30d9d
 		*fspp = NULL;
b30d9d
 
b30d9d
-		/* Count the MPOOLFILE structures. */
b30d9d
-		i = 0;
b30d9d
-		len = 0;
b30d9d
-		if ((ret = __memp_walk_files(env,
b30d9d
-		     mp, __memp_count_files, &len, &i, flags)) != 0)
b30d9d
-			return (ret);
b30d9d
+		while (*fspp == NULL) {
b30d9d
+			/* Count the MPOOLFILE structures. */
b30d9d
+			i = 0;
b30d9d
+			/*
b30d9d
+			 * Allow space for the first __memp_get_files() to align the
b30d9d
+			 * structure array to uintmax_t, DB_MPOOL_STAT's most
b30d9d
+			 * restrictive field.  [#23150]
b30d9d
+			 */
b30d9d
+			len = sizeof(uintmax_t);
b30d9d
+			if ((ret = __memp_walk_files(env,
b30d9d
+			     mp, __memp_count_files, &len, &i, flags)) != 0)
b30d9d
+				return (ret);
b30d9d
+
b30d9d
+			if (i == 0)
b30d9d
+				return (0);
b30d9d
+
b30d9d
+			/* 
b30d9d
+			 * Copy the number of DB_MPOOL_FSTAT entries and the number of
b30d9d
+			 * bytes allocated for them into fsp_len. Do not count the space
b30d9d
+			 * reserved for allignment.
b30d9d
+			 */
b30d9d
+			fsp_len[0] = i;
b30d9d
+			fsp_len[1] = len - sizeof(uintmax_t);
b30d9d
 
b30d9d
-		if (i == 0)
b30d9d
-			return (0);
b30d9d
-		len += sizeof(DB_MPOOL_FSTAT *);	/* Trailing NULL */
b30d9d
+			len += sizeof(DB_MPOOL_FSTAT *);	/* Trailing NULL */
b30d9d
 
b30d9d
-		/* Allocate space */
b30d9d
-		if ((ret = __os_umalloc(env, len, fspp)) != 0)
b30d9d
-			return (ret);
b30d9d
+			/* Allocate space */
b30d9d
+			if ((ret = __os_umalloc(env, len, fspp)) != 0)
b30d9d
+				return (ret);
b30d9d
 
b30d9d
-		tfsp = *fspp;
b30d9d
-		*tfsp = NULL;
b30d9d
-
b30d9d
-		/*
b30d9d
-		 * Files may have been opened since we counted, don't walk
b30d9d
-		 * off the end of the allocated space.
b30d9d
-		 */
b30d9d
-		if ((ret = __memp_walk_files(env,
b30d9d
-		    mp, __memp_get_files, &tfsp, &i, flags)) != 0)
b30d9d
-			return (ret);
b30d9d
+			tfsp = *fspp;
b30d9d
+			*tfsp = NULL;
b30d9d
 
b30d9d
+			/*
b30d9d
+			 * Files may have been opened since we counted, if we walk off
b30d9d
+			 * the end of the allocated space specified in fsp_len, retry.
b30d9d
+			 */
b30d9d
+			if ((ret = __memp_walk_files(env,
b30d9d
+			    mp, __memp_get_files, &tfsp, fsp_len, flags)) != 0) {
b30d9d
+				if (ret == DB_BUFFER_SMALL) {
b30d9d
+					__os_ufree(env, *fspp);
b30d9d
+					*fspp = NULL;
b30d9d
+					tfsp = NULL;
b30d9d
+				} else
b30d9d
+					return (ret);
b30d9d
+			}
b30d9d
+		}
b30d9d
 		*++tfsp = NULL;
b30d9d
 	}
b30d9d
 
b30d9d
@@ -286,28 +314,35 @@
b30d9d
  * for the text file names.
b30d9d
  */
b30d9d
 static int
b30d9d
-__memp_get_files(env, mfp, argp, countp, flags)
b30d9d
+__memp_get_files(env, mfp, argp, fsp_len, flags)
b30d9d
 	ENV *env;
b30d9d
 	MPOOLFILE *mfp;
b30d9d
 	void *argp;
b30d9d
-	u_int32_t *countp;
b30d9d
+	u_int32_t fsp_len[];
b30d9d
 	u_int32_t flags;
b30d9d
 {
b30d9d
 	DB_MPOOL *dbmp;
b30d9d
 	DB_MPOOL_FSTAT **tfsp, *tstruct;
b30d9d
 	char *name, *tname;
b30d9d
-	size_t nlen;
b30d9d
+	size_t nlen, tlen;
b30d9d
 
b30d9d
-	if (*countp == 0)
b30d9d
-		return (0);
b30d9d
+	/* We walked through more files than argp was allocated for. */
b30d9d
+	if (fsp_len[0] == 0)
b30d9d
+		return DB_BUFFER_SMALL;
b30d9d
 
b30d9d
 	dbmp = env->mp_handle;
b30d9d
 	tfsp = *(DB_MPOOL_FSTAT ***)argp;
b30d9d
 
b30d9d
 	if (*tfsp == NULL) {
b30d9d
-		/* Add 1 to count because we need to skip over the NULL. */
b30d9d
-		tstruct = (DB_MPOOL_FSTAT *)(tfsp + *countp + 1);
b30d9d
-		tname = (char *)(tstruct + *countp);
b30d9d
+		/*
b30d9d
+		 * Add 1 to count because to skip over the NULL end marker.
b30d9d
+		 * Align it further for DB_MPOOL_STAT's most restrictive field
b30d9d
+		 * because uintmax_t might require stricter alignment than
b30d9d
+		 * pointers; e.g., IP32 LL64 SPARC. [#23150]
b30d9d
+		 */
b30d9d
+		tstruct = (DB_MPOOL_FSTAT *)&tfsp[fsp_len[0] + 1];
b30d9d
+		tstruct = ALIGNP_INC(tstruct, sizeof(uintmax_t));
b30d9d
+		tname = (char *)&tstruct[fsp_len[0]];
b30d9d
 		*tfsp = tstruct;
b30d9d
 	} else {
b30d9d
 		tstruct = *tfsp + 1;
b30d9d
@@ -317,6 +352,15 @@
b30d9d
 
b30d9d
 	name = __memp_fns(dbmp, mfp);
b30d9d
 	nlen = strlen(name) + 1;
b30d9d
+
b30d9d
+	/* The space required for file names is larger than argp was allocated for. */
b30d9d
+	tlen = sizeof(DB_MPOOL_FSTAT *) + sizeof(DB_MPOOL_FSTAT) + nlen;
b30d9d
+	if (fsp_len[1] < tlen)
b30d9d
+		return DB_BUFFER_SMALL;
b30d9d
+	else
b30d9d
+		/* Count down the number of bytes left in argp. */
b30d9d
+		fsp_len[1] -= tlen;
b30d9d
+
b30d9d
 	memcpy(tname, name, nlen);
b30d9d
 	memcpy(tstruct, &mfp->stat, sizeof(mfp->stat));
b30d9d
 	tstruct->file_name = tname;
b30d9d
@@ -325,7 +369,9 @@
b30d9d
 	tstruct->st_pagesize = mfp->pagesize;
b30d9d
 
b30d9d
 	*(DB_MPOOL_FSTAT ***)argp = tfsp;
b30d9d
-	(*countp)--;
b30d9d
+
b30d9d
+	/* Count down the number of entries left in argp. */
b30d9d
+	fsp_len[0]--;
b30d9d
 
b30d9d
 	if (LF_ISSET(DB_STAT_CLEAR))
b30d9d
 		memset(&mfp->stat, 0, sizeof(mfp->stat));
b30d9d
diff -r -u db-5.3.21_orig/src/mp/mp_sync.c db-5.3.21/src/mp/mp_sync.c
b30d9d
--- db-5.3.21_orig/src/mp/mp_sync.c	2012-05-12 01:57:53.000000000 +0800
b30d9d
+++ db-5.3.21/src/mp/mp_sync.c	2015-05-19 15:08:05.000000000 +0800
b30d9d
@@ -57,11 +57,13 @@
b30d9d
 			if ((t_ret = func(env,
b30d9d
 			    mfp, arg, countp, flags)) != 0 && ret == 0)
b30d9d
 				ret = t_ret;
b30d9d
-			if (ret != 0 && !LF_ISSET(DB_STAT_MEMP_NOERROR))
b30d9d
+			if (ret != 0 &&
b30d9d
+			    (!LF_ISSET(DB_STAT_MEMP_NOERROR) || ret == DB_BUFFER_SMALL))
b30d9d
 				break;
b30d9d
 		}
b30d9d
 		MUTEX_UNLOCK(env, hp->mtx_hash);
b30d9d
-		if (ret != 0 && !LF_ISSET(DB_STAT_MEMP_NOERROR))
b30d9d
+		if (ret != 0 &&
b30d9d
+		    (!LF_ISSET(DB_STAT_MEMP_NOERROR) || ret == DB_BUFFER_SMALL))
b30d9d
 			break;
b30d9d
 	}
b30d9d
 	return (ret);