From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Kenichi Handa Newsgroups: gmane.emacs.devel Subject: dangerous alloca calls Date: Thu, 20 Nov 2003 22:01:20 +0900 (JST) Sender: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Message-ID: <200311201301.WAA17488@etlken.m17n.org> NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 (generated by SEMI 1.14.3 - "Ushinoya") Content-Type: text/plain; charset=US-ASCII X-Trace: sea.gmane.org 1069335278 18322 80.91.224.253 (20 Nov 2003 13:34:38 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Thu, 20 Nov 2003 13:34:38 +0000 (UTC) Original-X-From: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Thu Nov 20 14:34:35 2003 Return-path: Original-Received: from quimby.gnus.org ([80.91.224.244]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1AMox9-0007WH-00 for ; Thu, 20 Nov 2003 14:34:35 +0100 Original-Received: from monty-python.gnu.org ([199.232.76.173]) by quimby.gnus.org with esmtp (Exim 3.35 #1 (Debian)) id 1AMox9-0005Fn-00 for ; Thu, 20 Nov 2003 14:34:35 +0100 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.24) id 1AMppa-0007YX-7f for emacs-devel@quimby.gnus.org; Thu, 20 Nov 2003 09:30:50 -0500 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.24) id 1AMpoj-0007Xl-KH for emacs-devel@gnu.org; Thu, 20 Nov 2003 09:29:57 -0500 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.24) id 1AMpoB-0007SU-Vv for emacs-devel@gnu.org; Thu, 20 Nov 2003 09:29:56 -0500 Original-Received: from [192.47.44.130] (helo=tsukuba.m17n.org) by monty-python.gnu.org with esmtp (Exim 4.24) id 1AMpOS-0001Kb-Vk for emacs-devel@gnu.org; Thu, 20 Nov 2003 09:02:49 -0500 Original-Received: from fs.m17n.org (fs.m17n.org [192.47.44.2]) by tsukuba.m17n.org (8.11.6p2/3.7W-20010518204228) with ESMTP id hAKD1Lh23366 for ; Thu, 20 Nov 2003 22:01:21 +0900 (JST) (envelope-from handa@m17n.org) Original-Received: from etlken.m17n.org (etlken.m17n.org [192.47.44.125]) by fs.m17n.org (8.11.6/3.7W-20010823150639) with ESMTP id hAKD1Ks07502 for ; Thu, 20 Nov 2003 22:01:20 +0900 (JST) Original-Received: (from handa@localhost) by etlken.m17n.org (8.8.8+Sun/3.7W-2001040620) id WAA17488; Thu, 20 Nov 2003 22:01:20 +0900 (JST) Original-To: emacs-devel@gnu.org User-Agent: SEMI/1.14.3 (Ushinoya) FLIM/1.14.2 (Yagi-Nishiguchi) APEL/10.2 Emacs/21.3 (sparc-sun-solaris2.6) MULE/5.0 (SAKAKI) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.2 Precedence: list List-Id: Emacs development discussions. List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Xref: main.gmane.org gmane.emacs.devel:17961 X-Report-Spam: http://spam.gmane.org/gmane.emacs.devel:17961 With CVS HEAD emacs on GNU/Linux, (mapcar 'car (make-list #x400000 nil)) causes segfault. The attached patch fixes it. But, I found such dangerous alloca calls in several other places. E.g. (format (make-string #x1000000 32)) (apply 'car (make-list #x100000 nil)) It seems that it is better that we use a macro something like this consitently. What do you think? #define SAFE_ALLOCA(address, size, unwind_function) \ do { \ if (size < MAX_ALLOCA) \ address = alloca (size); \ else \ { \ address = xmalloc (size); \ if (unwind_function) \ record_unwind_protect (unwind_function, Qnil); \ } \ } while (0) --- Ken'ichi HANDA handa@m17n.org *** fns.c.~1.346.~ Wed Nov 19 20:06:32 2003 --- fns.c Thu Nov 20 21:26:50 2003 *************** *** 79,84 **** --- 79,88 ---- #ifndef HAVE_UNISTD_H extern long time (); #endif + + /* Don't use alloca for bytes larger than this, lest we overflow + their stack. */ + #define MAX_ALLOCA 16*1024 DEFUN ("identity", Fidentity, Sidentity, 1, 1, 0, doc: /* Return the argument unchanged. */) *************** *** 2937,2942 **** --- 2941,2956 ---- UNGCPRO; } + static Lisp_Object *mapcar_args; + + static Lisp_Object + mapcar_unwind (arg) + Lisp_Object arg; + { + xfree (mapcar_args); + return Qnil; + } + DEFUN ("mapconcat", Fmapconcat, Smapconcat, 3, 3, 0, doc: /* Apply FUNCTION to each element of SEQUENCE, and concat the results as strings. In between each pair of results, stick in SEPARATOR. Thus, " " as *************** *** 2951,2963 **** register Lisp_Object *args; register int i; struct gcpro gcpro1; len = Flength (sequence); leni = XINT (len); nargs = leni + leni - 1; if (nargs < 0) return build_string (""); ! args = (Lisp_Object *) alloca (nargs * sizeof (Lisp_Object)); GCPRO1 (separator); mapcar1 (leni, args, function, sequence); --- 2965,2986 ---- register Lisp_Object *args; register int i; struct gcpro gcpro1; + int nbytes; + int count = SPECPDL_INDEX (); len = Flength (sequence); leni = XINT (len); nargs = leni + leni - 1; if (nargs < 0) return build_string (""); ! nbytes = nargs * sizeof (Lisp_Object); ! if (nbytes < MAX_ALLOCA) ! args = (Lisp_Object *) alloca (nbytes); ! else ! { ! args = mapcar_args = (Lisp_Object *) xmalloc (nbytes); ! record_unwind_protect (mapcar_unwind, Qnil); ! } GCPRO1 (separator); mapcar1 (leni, args, function, sequence); *************** *** 2969,2975 **** for (i = 1; i < nargs; i += 2) args[i] = separator; ! return Fconcat (nargs, args); } DEFUN ("mapcar", Fmapcar, Smapcar, 2, 2, 0, --- 2992,3001 ---- for (i = 1; i < nargs; i += 2) args[i] = separator; ! if (nbytes < MAX_ALLOCA) ! return Fconcat (nargs, args); ! else ! return unbind_to (count, Fconcat (nargs, args)); } DEFUN ("mapcar", Fmapcar, Smapcar, 2, 2, 0, *************** *** 2982,2995 **** register Lisp_Object len; register int leni; register Lisp_Object *args; len = Flength (sequence); leni = XFASTINT (len); ! args = (Lisp_Object *) alloca (leni * sizeof (Lisp_Object)); mapcar1 (leni, args, function, sequence); ! return Flist (leni, args); } DEFUN ("mapc", Fmapc, Smapc, 2, 2, 0, --- 3008,3035 ---- register Lisp_Object len; register int leni; register Lisp_Object *args; + int nbytes; + int count = SPECPDL_INDEX (); len = Flength (sequence); leni = XFASTINT (len); ! if (leni < 0) return Qnil; ! ! nbytes = leni * sizeof (Lisp_Object); ! if (nbytes < MAX_ALLOCA) ! args = (Lisp_Object *) alloca (nbytes); ! else ! { ! args = mapcar_args = (Lisp_Object *) xmalloc (nbytes); ! record_unwind_protect (mapcar_unwind, Qnil); ! } mapcar1 (leni, args, function, sequence); ! if (nbytes < MAX_ALLOCA) ! return Flist (leni, args); ! else ! return unbind_to (count, Flist (leni, args)); } DEFUN ("mapc", Fmapc, Smapc, 2, 2, 0, *************** *** 3603,3612 **** c = from[i++]; \ } \ while (IS_BASE64_IGNORABLE (c)) - - /* Don't use alloca for regions larger than this, lest we overflow - their stack. */ - #define MAX_ALLOCA 16*1024 /* Table of characters coding the 64 values. */ static char base64_value_to_char[64] = --- 3643,3648 ----