PR 23966
	* libbfd.c (SSIZE_MAX): Define.
	(bfd_malloc, bfd_realloc): Don't cast size to long to check for
	"negative" values, compare against SSIZE_MAX instead.
+2018-12-28  Alan Modra  <amodra@gmail.com>
+
+       PR 23966
+       * libbfd.c (SSIZE_MAX): Define.
+       (bfd_malloc, bfd_realloc): Don't cast size to long to check for
+       "negative" values, compare against SSIZE_MAX instead.
+
 2018-12-23  H.J. Lu  <hongjiu.lu@intel.com>
 
        * elf32-i386.c (elf_i386_rtype_to_howto): Remove the unused bfd
 
 \f
 /* Allocate memory using malloc.  */
 
+#ifndef SSIZE_MAX
+#define SSIZE_MAX ((size_t) -1 >> 1)
+#endif
+
 void *
 bfd_malloc (bfd_size_type size)
 {
 
   if (size != sz
       /* This is to pacify memory checkers like valgrind.  */
-      || ((signed long) sz) < 0)
+      || sz > SSIZE_MAX)
     {
       bfd_set_error (bfd_error_no_memory);
       return NULL;
 
   if (size != sz
       /* This is to pacify memory checkers like valgrind.  */
-      || ((signed long) sz) < 0)
+      || sz > SSIZE_MAX)
     {
       bfd_set_error (bfd_error_no_memory);
       return NULL;