diff --git a/src/ChangeLog b/src/ChangeLog
index 43784484..4ff93595 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,9 @@
+2005-08-27  Hrvoje Niksic  <hniksic@xemacs.org>
+
+	* cmpt.c (strtoll): Correctly handle strtoll("0x", ptr, 0) and
+	strtoll("0x<nonhexchar>", ptr, 0) -- in both cases *ptr must be
+	set to the position of 'x', not after it.
+
 2005-08-27  Hrvoje Niksic  <hniksic@xemacs.org>
 
 	* hash.c (hash_table_map): Rename to hash_table_for_each and
diff --git a/src/cmpt.c b/src/cmpt.c
index a4142951..03ff3ceb 100644
--- a/src/cmpt.c
+++ b/src/cmpt.c
@@ -1348,6 +1348,13 @@ strtoll (const char *nptr, char **endptr, int base)
 	{
 	  base = 16;
 	  nptr += 2;
+	  /* "0x" must be followed by at least one hex char.  If not,
+	     return 0 and place ENDPTR on 'x'. */
+	  if (!ISXDIGIT (*nptr))
+	    {
+	      --nptr;
+	      goto out;
+	    }
 	}
       else if (base == 0)
 	base = 8;
@@ -1387,6 +1394,7 @@ strtoll (const char *nptr, char **endptr, int base)
 	  result = newresult;
 	}
     }
+ out:
   if (endptr)
     *endptr = (char *) nptr;
   return result;