diff options
| author | Paul Eggert | 2011-07-18 20:34:13 -0700 |
|---|---|---|
| committer | Paul Eggert | 2011-07-18 20:34:13 -0700 |
| commit | e097a6fa863b26952a476e71a786fa7b2460277b (patch) | |
| tree | 759c9146fe747732a9243b6070ed0a31317dc713 /src/charset.c | |
| parent | 5637687fead7d57f73ea9a7677d25b93fb785dc7 (diff) | |
| download | emacs-e097a6fa863b26952a476e71a786fa7b2460277b.tar.gz emacs-e097a6fa863b26952a476e71a786fa7b2460277b.zip | |
* charset.c (Fdefine_charset_internal): Check for integer overflow.
Add a FIXME comment about memory leaks.
(syms_of_charset): Don't assume xmalloc returns.
Diffstat (limited to 'src/charset.c')
| -rw-r--r-- | src/charset.c | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/src/charset.c b/src/charset.c index e2bfcd08671..852aeb19bcb 100644 --- a/src/charset.c +++ b/src/charset.c | |||
| @@ -1150,13 +1150,28 @@ usage: (define-charset-internal ...) */) | |||
| 1150 | hash_code); | 1150 | hash_code); |
| 1151 | if (charset_table_used == charset_table_size) | 1151 | if (charset_table_used == charset_table_size) |
| 1152 | { | 1152 | { |
| 1153 | struct charset *new_table | 1153 | struct charset *new_table; |
| 1154 | /* Ensure that charset IDs fit into 'int' as well as into the | ||
| 1155 | restriction imposed by fixnums, ptrdiff_t, and size_t. | ||
| 1156 | Although the 'int' restriction could be removed, too much other | ||
| 1157 | code would need altering; for example, the IDs are stuffed into | ||
| 1158 | struct coding_system.charbuf[i] entries, which are 'int'. */ | ||
| 1159 | int charset_table_size_max = | ||
| 1160 | min (min (INT_MAX, MOST_POSITIVE_FIXNUM), | ||
| 1161 | min (PTRDIFF_MAX, SIZE_MAX) / sizeof (struct charset)); | ||
| 1162 | if (charset_table_size_max - 16 < charset_table_size) | ||
| 1163 | memory_full (SIZE_MAX); | ||
| 1164 | new_table | ||
| 1154 | = (struct charset *) xmalloc (sizeof (struct charset) | 1165 | = (struct charset *) xmalloc (sizeof (struct charset) |
| 1155 | * (charset_table_size + 16)); | 1166 | * (charset_table_size + 16)); |
| 1156 | memcpy (new_table, charset_table, | 1167 | memcpy (new_table, charset_table, |
| 1157 | sizeof (struct charset) * charset_table_size); | 1168 | sizeof (struct charset) * charset_table_size); |
| 1158 | charset_table_size += 16; | 1169 | charset_table_size += 16; |
| 1159 | charset_table = new_table; | 1170 | charset_table = new_table; |
| 1171 | /* FIXME: Doesn't this leak memory? The old charset_table | ||
| 1172 | becomes unreachable. If the memory leak is intentional, | ||
| 1173 | a comment should be added to explain this. If not, the | ||
| 1174 | old charset_table should be freed, using xfree. */ | ||
| 1160 | } | 1175 | } |
| 1161 | id = charset_table_used++; | 1176 | id = charset_table_used++; |
| 1162 | new_definition_p = 1; | 1177 | new_definition_p = 1; |
| @@ -2347,9 +2362,8 @@ syms_of_charset (void) | |||
| 2347 | Vcharset_hash_table = Fmake_hash_table (2, args); | 2362 | Vcharset_hash_table = Fmake_hash_table (2, args); |
| 2348 | } | 2363 | } |
| 2349 | 2364 | ||
| 2365 | charset_table = (struct charset *) xmalloc (sizeof (struct charset) * 128); | ||
| 2350 | charset_table_size = 128; | 2366 | charset_table_size = 128; |
| 2351 | charset_table = ((struct charset *) | ||
| 2352 | xmalloc (sizeof (struct charset) * charset_table_size)); | ||
| 2353 | charset_table_used = 0; | 2367 | charset_table_used = 0; |
| 2354 | 2368 | ||
| 2355 | defsubr (&Scharsetp); | 2369 | defsubr (&Scharsetp); |