aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPaul Eggert2011-07-28 13:29:44 -0700
committerPaul Eggert2011-07-28 13:29:44 -0700
commit69e8622f7f852f63b8f71c5b1aa4c2fd406e383d (patch)
tree85d121dd4d89d798cb83f3034e0399e34c61d47f /src
parent860887db5c3c55a502795d89d43176783e0e313d (diff)
downloademacs-69e8622f7f852f63b8f71c5b1aa4c2fd406e383d.tar.gz
emacs-69e8622f7f852f63b8f71c5b1aa4c2fd406e383d.zip
* ccl.c: Integer and memory overflow fixes.
(Fccl_execute_on_string): Check for memory overflow. Use ptrdiff_t rather than EMACS_INT where ptrdiff_t will do. Redo buffer-overflow calculations to avoid integer overflow.
Diffstat (limited to 'src')
-rw-r--r--src/ChangeLog5
-rw-r--r--src/ccl.c35
2 files changed, 31 insertions, 9 deletions
diff --git a/src/ChangeLog b/src/ChangeLog
index 9b9a9686259..b35f5607619 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,5 +1,10 @@
12011-07-28 Paul Eggert <eggert@cs.ucla.edu> 12011-07-28 Paul Eggert <eggert@cs.ucla.edu>
2 2
3 * ccl.c: Integer and memory overflow fixes.
4 (Fccl_execute_on_string): Check for memory overflow.
5 Use ptrdiff_t rather than EMACS_INT where ptrdiff_t will do.
6 Redo buffer-overflow calculations to avoid integer overflow.
7
3 * callproc.c (child_setup): Don't assume strlen fits in int. 8 * callproc.c (child_setup): Don't assume strlen fits in int.
4 9
5 * buffer.c: Memory overflow fixes. 10 * buffer.c: Memory overflow fixes.
diff --git a/src/ccl.c b/src/ccl.c
index 087c0feb4ab..0a9b3d90708 100644
--- a/src/ccl.c
+++ b/src/ccl.c
@@ -2061,12 +2061,12 @@ usage: (ccl-execute-on-string CCL-PROGRAM STATUS STRING &optional CONTINUE UNIBY
2061 Lisp_Object val; 2061 Lisp_Object val;
2062 struct ccl_program ccl; 2062 struct ccl_program ccl;
2063 int i; 2063 int i;
2064 EMACS_INT outbufsize; 2064 ptrdiff_t outbufsize;
2065 unsigned char *outbuf, *outp; 2065 unsigned char *outbuf, *outp;
2066 EMACS_INT str_chars, str_bytes; 2066 ptrdiff_t str_chars, str_bytes;
2067#define CCL_EXECUTE_BUF_SIZE 1024 2067#define CCL_EXECUTE_BUF_SIZE 1024
2068 int source[CCL_EXECUTE_BUF_SIZE], destination[CCL_EXECUTE_BUF_SIZE]; 2068 int source[CCL_EXECUTE_BUF_SIZE], destination[CCL_EXECUTE_BUF_SIZE];
2069 EMACS_INT consumed_chars, consumed_bytes, produced_chars; 2069 ptrdiff_t consumed_chars, consumed_bytes, produced_chars;
2070 2070
2071 if (setup_ccl_program (&ccl, ccl_prog) < 0) 2071 if (setup_ccl_program (&ccl, ccl_prog) < 0)
2072 error ("Invalid CCL program"); 2072 error ("Invalid CCL program");
@@ -2093,6 +2093,10 @@ usage: (ccl-execute-on-string CCL-PROGRAM STATUS STRING &optional CONTINUE UNIBY
2093 ccl.ic = i; 2093 ccl.ic = i;
2094 } 2094 }
2095 2095
2096 if (((min (PTRDIFF_MAX, SIZE_MAX) - 256)
2097 / (ccl.buf_magnification ? ccl.buf_magnification : 1))
2098 < str_bytes)
2099 memory_full (SIZE_MAX);
2096 outbufsize = (ccl.buf_magnification 2100 outbufsize = (ccl.buf_magnification
2097 ? str_bytes * ccl.buf_magnification + 256 2101 ? str_bytes * ccl.buf_magnification + 256
2098 : str_bytes + 256); 2102 : str_bytes + 256);
@@ -2127,11 +2131,19 @@ usage: (ccl-execute-on-string CCL-PROGRAM STATUS STRING &optional CONTINUE UNIBY
2127 produced_chars += ccl.produced; 2131 produced_chars += ccl.produced;
2128 if (NILP (unibyte_p)) 2132 if (NILP (unibyte_p))
2129 { 2133 {
2130 if (outp - outbuf + MAX_MULTIBYTE_LENGTH * ccl.produced 2134 ptrdiff_t offset = outp - outbuf;
2131 > outbufsize) 2135 if ((outbufsize - offset) / MAX_MULTIBYTE_LENGTH < ccl.produced)
2132 { 2136 {
2133 EMACS_INT offset = outp - outbuf; 2137 ptrdiff_t produced;
2134 outbufsize += MAX_MULTIBYTE_LENGTH * ccl.produced; 2138 if (((min (PTRDIFF_MAX, SIZE_MAX) - outbufsize)
2139 / MAX_MULTIBYTE_LENGTH)
2140 < ccl.produced)
2141 {
2142 xfree (outbuf);
2143 memory_full (SIZE_MAX);
2144 }
2145 produced = ccl.produced;
2146 outbufsize += MAX_MULTIBYTE_LENGTH * produced;
2135 outbuf = (unsigned char *) xrealloc (outbuf, outbufsize); 2147 outbuf = (unsigned char *) xrealloc (outbuf, outbufsize);
2136 outp = outbuf + offset; 2148 outp = outbuf + offset;
2137 } 2149 }
@@ -2140,9 +2152,14 @@ usage: (ccl-execute-on-string CCL-PROGRAM STATUS STRING &optional CONTINUE UNIBY
2140 } 2152 }
2141 else 2153 else
2142 { 2154 {
2143 if (outp - outbuf + ccl.produced > outbufsize) 2155 ptrdiff_t offset = outp - outbuf;
2156 if (outbufsize - offset < ccl.produced)
2144 { 2157 {
2145 EMACS_INT offset = outp - outbuf; 2158 if (min (PTRDIFF_MAX, SIZE_MAX) - outbufsize < ccl.produced)
2159 {
2160 xfree (outbuf);
2161 memory_full (SIZE_MAX);
2162 }
2146 outbufsize += ccl.produced; 2163 outbufsize += ccl.produced;
2147 outbuf = (unsigned char *) xrealloc (outbuf, outbufsize); 2164 outbuf = (unsigned char *) xrealloc (outbuf, outbufsize);
2148 outp = outbuf + offset; 2165 outp = outbuf + offset;