delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2005/05/03/14:55:30

X-Authentication-Warning: delorie.com: mail set sender to djgpp-workers-bounces using -f
From: <ams AT ludd DOT ltu DOT se>
Message-Id: <200505030858.j438wvU7006744@speedy.ludd.ltu.se>
Subject: Bug 000366 (mbstowcs causes SIGSEGV when passed a NULL pointer)
To: DJGPP-WORKERS <djgpp-workers AT delorie DOT com>
Date: Tue, 3 May 2005 10:58:57 +0200 (CEST)
X-Mailer: ELM [version 2.4ME+ PL78 (25)]
MIME-Version: 1.0
X-ltu-MailScanner-Information: Please contact the ISP for more information
X-ltu-MailScanner: Found to be clean
X-MailScanner-From: ams AT ludd DOT ltu DOT se
Reply-To: djgpp-workers AT delorie DOT com

Patch including test programs for this bug (pasted):
Index: djgpp/src/libc/ansi/locale/mbstowcs.c
===================================================================
RCS file: /cvs/djgpp/djgpp/src/libc/ansi/locale/mbstowcs.c,v
retrieving revision 1.3
diff -p -u -r1.3 mbstowcs.c
--- djgpp/src/libc/ansi/locale/mbstowcs.c       17 Oct 2002 23:00:24 -0000      
1.3
+++ djgpp/src/libc/ansi/locale/mbstowcs.c       3 May 2005 18:51:07 -0000
@@ -1,3 +1,4 @@
+/* Copyright (C) 2005 DJ Delorie, see COPYING.DJ for details */
 /* Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details */
 /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
 #include <stdlib.h>
@@ -5,9 +6,14 @@
 size_t
 mbstowcs(wchar_t *wcs, const char *s, size_t n)
 {
-  size_t i;
-  for (i=0; s[i] && (i+1<n); i++)
+  size_t i = 0;
+  while (i < n)
+  {
     wcs[i] = s[i];
-  wcs[i] = 0;
+    i++;
+  }
+  if (i < n)
+    wcs[i] = 0;
+
   return i;
 }
Index: djgpp/src/libc/ansi/locale/wcstombs.c
===================================================================
RCS file: /cvs/djgpp/djgpp/src/libc/ansi/locale/wcstombs.c,v
retrieving revision 1.3
diff -p -u -r1.3 wcstombs.c
--- djgpp/src/libc/ansi/locale/wcstombs.c       17 Oct 2002 23:00:24 -0000      
1.3
+++ djgpp/src/libc/ansi/locale/wcstombs.c       3 May 2005 18:51:07 -0000
@@ -1,3 +1,4 @@
+/* Copyright (C) 2005 DJ Delorie, see COPYING.DJ for details */
 /* Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details */
 /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
 #include <stdlib.h>
@@ -5,9 +6,14 @@
 size_t
 wcstombs(char *s, const wchar_t *wcs, size_t n)
 {
-  size_t i;
-  for (i=0; wcs[i] && (i+1<n); i++)
+  size_t i = 0;
+  while (i < n)
+  {
     s[i] = wcs[i];
-  s[i] = 0;
+    i++;
+  }
+  if (i < n)
+    s[i] = 0;
+
   return i;
 }
Index: djgpp/tests/libc/ansi/stdlib/makefile
===================================================================
RCS file: /cvs/djgpp/djgpp/tests/libc/ansi/stdlib/makefile,v
retrieving revision 1.3
diff -p -u -r1.3 makefile
--- djgpp/tests/libc/ansi/stdlib/makefile       8 Jun 2001 10:03:33 -0000       
1.3
+++ djgpp/tests/libc/ansi/stdlib/makefile       3 May 2005 18:51:08 -0000
@@ -2,10 +2,12 @@ TOP=../..
 
 SRC += env.c
 SRC += getenv.c
+SRC += mbstowcs.c
 SRC += shell.c
 SRC += strtod.c
 SRC += system.c
 SRC += system2.c
 SRC += tmalloc.c
+SRC += wcstombs.c
 
 include $(TOP)/../makefile.inc
Index: djgpp/tests/libc/ansi/stdlib/mbstowcs.c
===================================================================
RCS file: djgpp/tests/libc/ansi/stdlib/mbstowcs.c
diff -N djgpp/tests/libc/ansi/stdlib/mbstowcs.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ djgpp/tests/libc/ansi/stdlib/mbstowcs.c     3 May 2005 18:51:08 -0000
@@ -0,0 +1,12 @@
+#include <stdlib.h>
+
+int main(void)
+{
+  size_t len;
+
+  char s[] = "A";
+
+  len = mbstowcs(NULL, s, 0);
+
+  return 0;
+}
Index: djgpp/tests/libc/ansi/stdlib/wcstombs.c
===================================================================
RCS file: djgpp/tests/libc/ansi/stdlib/wcstombs.c
diff -N djgpp/tests/libc/ansi/stdlib/wcstombs.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ djgpp/tests/libc/ansi/stdlib/wcstombs.c     3 May 2005 18:51:08 -0000
@@ -0,0 +1,12 @@
+#include <stdlib.h>
+
+int main(void)
+{
+  size_t len;
+
+  wchar_t ws[] = L"A";
+
+  len = wcstombs(NULL, ws, 0);
+
+  return 0;
+}


Right,

						MartinS

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019