ce426f
commit 88ed594f5d431d855256edbe7e886c8cf4b575dc
ce426f
Author: Roland McGrath <roland@hack.frob.com>
ce426f
Date:   Tue May 19 15:04:41 2015 -0700
ce426f
ce426f
    BZ#18434: Fix sem_post EOVERFLOW check for [!__HAVE_64B_ATOMICS].
ce426f
ce426f
Index: glibc-2.17-c758a686/nptl/Makefile
ce426f
===================================================================
ce426f
--- glibc-2.17-c758a686.orig/nptl/Makefile
ce426f
+++ glibc-2.17-c758a686/nptl/Makefile
ce426f
@@ -224,6 +224,7 @@ tests = tst-typesizes \
ce426f
 	tst-key1 tst-key2 tst-key3 tst-key4 \
ce426f
 	tst-sem1 tst-sem2 tst-sem3 tst-sem4 tst-sem5 tst-sem6 tst-sem7 \
ce426f
 	tst-sem8 tst-sem9 tst-sem10 tst-sem11 tst-sem12 tst-sem13 tst-sem14 \
ce426f
+	tst-sem15 \
ce426f
 	tst-barrier1 tst-barrier2 tst-barrier3 tst-barrier4 \
ce426f
 	tst-align tst-align2 tst-align3 \
ce426f
 	tst-basic1 tst-basic2 tst-basic3 tst-basic4 tst-basic5 tst-basic6 \
ce426f
Index: glibc-2.17-c758a686/nptl/tst-sem15.c
ce426f
===================================================================
ce426f
--- /dev/null
ce426f
+++ glibc-2.17-c758a686/nptl/tst-sem15.c
ce426f
@@ -0,0 +1,99 @@
ce426f
+/* Test for SEM_VALUE_MAX overflow detection: BZ #18434.
ce426f
+   Copyright (C) 2015 Free Software Foundation, Inc.
ce426f
+   This file is part of the GNU C Library.
ce426f
+
ce426f
+   The GNU C Library is free software; you can redistribute it and/or
ce426f
+   modify it under the terms of the GNU Lesser General Public
ce426f
+   License as published by the Free Software Foundation; either
ce426f
+   version 2.1 of the License, or (at your option) any later version.
ce426f
+
ce426f
+   The GNU C Library is distributed in the hope that it will be useful,
ce426f
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
ce426f
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
ce426f
+   Lesser General Public License for more details.
ce426f
+
ce426f
+   You should have received a copy of the GNU Lesser General Public
ce426f
+   License along with the GNU C Library; if not, see
ce426f
+   <http://www.gnu.org/licenses/>.  */
ce426f
+
ce426f
+#include <errno.h>
ce426f
+#include <limits.h>
ce426f
+#include <semaphore.h>
ce426f
+#include <stdio.h>
ce426f
+#include <string.h>
ce426f
+#include <unistd.h>
ce426f
+
ce426f
+
ce426f
+static int
ce426f
+do_test (void)
ce426f
+{
ce426f
+  sem_t s;
ce426f
+
ce426f
+  if (sem_init (&s, 0, SEM_VALUE_MAX))
ce426f
+    {
ce426f
+      printf ("sem_init: %m\n");
ce426f
+      return 1;
ce426f
+    }
ce426f
+
ce426f
+  int result = 0;
ce426f
+
ce426f
+  int value = 0xdeadbeef;
ce426f
+  if (sem_getvalue (&s, &value))
ce426f
+    {
ce426f
+      printf ("sem_getvalue: %m\n");
ce426f
+      result = 1;
ce426f
+    }
ce426f
+  else
ce426f
+    {
ce426f
+      printf ("sem_getvalue after init: %d\n", value);
ce426f
+      if (value != SEM_VALUE_MAX)
ce426f
+	{
ce426f
+	  printf ("\tshould be %d\n", SEM_VALUE_MAX);
ce426f
+	  result = 1;
ce426f
+	}
ce426f
+    }
ce426f
+
ce426f
+  errno = 0;
ce426f
+  if (sem_post(&s) == 0)
ce426f
+    {
ce426f
+      puts ("sem_post at SEM_VALUE_MAX succeeded!");
ce426f
+      result = 1;
ce426f
+    }
ce426f
+  else
ce426f
+    {
ce426f
+      printf ("sem_post at SEM_VALUE_MAX: %m (%d)\n", errno);
ce426f
+      if (errno != EOVERFLOW)
ce426f
+	{
ce426f
+	  printf ("\tshould be %s (EOVERFLOW = %d)\n",
ce426f
+		  strerror (EOVERFLOW), EOVERFLOW);
ce426f
+	  result = 1;
ce426f
+	}
ce426f
+    }
ce426f
+
ce426f
+  value = 0xbad1d00d;
ce426f
+  if (sem_getvalue (&s, &value))
ce426f
+    {
ce426f
+      printf ("sem_getvalue: %m\n");
ce426f
+      result = 1;
ce426f
+    }
ce426f
+  else
ce426f
+    {
ce426f
+      printf ("sem_getvalue after post: %d\n", value);
ce426f
+      if (value != SEM_VALUE_MAX)
ce426f
+	{
ce426f
+	  printf ("\tshould be %d\n", SEM_VALUE_MAX);
ce426f
+	  result = 1;
ce426f
+	}
ce426f
+    }
ce426f
+
ce426f
+  if (sem_destroy (&s))
ce426f
+    {
ce426f
+      printf ("sem_destroy: %m\n");
ce426f
+      result = 1;
ce426f
+    }
ce426f
+
ce426f
+  return result;
ce426f
+}
ce426f
+
ce426f
+#define TEST_FUNCTION do_test ()
ce426f
+#include "../test-skeleton.c"
ce426f
Index: glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/sem_post.c
ce426f
===================================================================
ce426f
--- glibc-2.17-c758a686.orig/nptl/sysdeps/unix/sysv/linux/sem_post.c
ce426f
+++ glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/sem_post.c
ce426f
@@ -83,14 +83,14 @@ __new_sem_post (sem_t *sem)
ce426f
   unsigned int v = atomic_load_relaxed (&isem->value);
ce426f
   do
ce426f
     {
ce426f
-      if ((v << SEM_VALUE_SHIFT) == SEM_VALUE_MAX)
ce426f
+      if ((v >> SEM_VALUE_SHIFT) == SEM_VALUE_MAX)
ce426f
 	{
ce426f
 	  __set_errno (EOVERFLOW);
ce426f
 	  return -1;
ce426f
 	}
ce426f
     }
ce426f
-  while (!atomic_compare_exchange_weak_release (&isem->value,
ce426f
-      &v, v + (1 << SEM_VALUE_SHIFT)));
ce426f
+  while (!atomic_compare_exchange_weak_release
ce426f
+	 (&isem->value, &v, v + (1 << SEM_VALUE_SHIFT)));
ce426f
 
ce426f
   /* If there is any potentially blocked waiter, wake one of them.  */
ce426f
   if ((v & SEM_NWAITERS_MASK) != 0)