Skip to content

Commit 9cb20d6

Browse files
committed
merge revision(s) 18212:
* regex.c (xmalloc, xrealloc, xfree): not to use ruby managed memory. * regex.c (DOUBLE_STACK, re_compile_fastmap0, re_adjust_startpos), (re_search, re_match_exec): check if failed to allocate memory. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8_7@18345 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 47bb180 commit 9cb20d6

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

ChangeLog

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
Mon Aug 4 14:13:15 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
2+
3+
* regex.c (xmalloc, xrealloc, xfree): not to use ruby managed memory.
4+
5+
* regex.c (DOUBLE_STACK, re_compile_fastmap0, re_adjust_startpos),
6+
(re_search, re_match_exec): check if failed to allocate memory.
7+
18
Mon Aug 4 13:53:42 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
29

310
* bignum.c (rb_big2str0, bigsqr): made interruptible. [ruby-Bugs-20622]

regex.c

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@
5050
/* We need this for `regex.h', and perhaps for the Emacs include files. */
5151
# include <sys/types.h>
5252
#endif
53+
#ifdef HAVE_STDLIB_H
54+
# include <stdlib.h>
55+
#endif
5356

5457
#if !defined(__STDC__) && !defined(_MSC_VER)
5558
# define volatile
@@ -63,6 +66,10 @@
6366

6467
#ifdef RUBY_PLATFORM
6568
#include "defines.h"
69+
#undef xmalloc
70+
#undef xrealloc
71+
#undef xcalloc
72+
#undef xfree
6673

6774
# define RUBY
6875
extern int rb_prohibit_interrupt;
@@ -104,6 +111,11 @@ void *alloca ();
104111
# include <strings.h>
105112
#endif
106113

114+
#define xmalloc malloc
115+
#define xrealloc realloc
116+
#define xcalloc calloc
117+
#define xfree free
118+
107119
#ifdef C_ALLOCA
108120
#define FREE_VARIABLES() alloca(0)
109121
#else
@@ -127,10 +139,12 @@ void *alloca ();
127139
unsigned int xlen = stacke - stackb; \
128140
if (stackb == stacka) { \
129141
stackx = (type*)xmalloc(2 * xlen * sizeof(type)); \
142+
if (!stackx) goto memory_exhausted; \
130143
memcpy(stackx, stackb, xlen * sizeof (type)); \
131144
} \
132145
else { \
133146
stackx = (type*)xrealloc(stackb, 2 * xlen * sizeof(type)); \
147+
if (!stackx) goto memory_exhausted; \
134148
} \
135149
/* Rearrange the pointers. */ \
136150
stackp = stackx + (stackp - stackb); \
@@ -2775,8 +2789,8 @@ bm_search(little, llen, big, blen, skip, translate)
27752789
The caller must supply the address of a (1 << BYTEWIDTH)-byte data
27762790
area as bufp->fastmap.
27772791
The other components of bufp describe the pattern to be used. */
2778-
void
2779-
re_compile_fastmap(bufp)
2792+
static int
2793+
re_compile_fastmap0(bufp)
27802794
struct re_pattern_buffer *bufp;
27812795
{
27822796
unsigned char *pattern = (unsigned char*)bufp->buffer;
@@ -2944,7 +2958,7 @@ re_compile_fastmap(bufp)
29442958
fastmap[j] = 1;
29452959
}
29462960
if (bufp->can_be_null) {
2947-
FREE_AND_RETURN_VOID(stackb);
2961+
FREE_AND_RETURN(stackb, 0);
29482962
}
29492963
/* Don't return; check the alternative paths
29502964
so we can set can_be_null if appropriate. */
@@ -3110,7 +3124,16 @@ re_compile_fastmap(bufp)
31103124
else
31113125
break;
31123126
}
3113-
FREE_AND_RETURN_VOID(stackb);
3127+
FREE_AND_RETURN(stackb, 0);
3128+
memory_exhausted:
3129+
FREE_AND_RETURN(stackb, -2);
3130+
}
3131+
3132+
void
3133+
re_compile_fastmap(bufp)
3134+
struct re_pattern_buffer *bufp;
3135+
{
3136+
(void)re_compile_fastmap0(bufp);
31143137
}
31153138

31163139
/* adjust startpos value to the position between characters. */
@@ -3144,7 +3167,8 @@ re_adjust_startpos(bufp, string, size, startpos, range)
31443167
{
31453168
/* Update the fastmap now if not correct already. */
31463169
if (!bufp->fastmap_accurate) {
3147-
re_compile_fastmap(bufp);
3170+
int ret = re_compile_fastmap0(bufp);
3171+
if (ret) return ret;
31483172
}
31493173

31503174
/* Adjust startpos for mbc string */
@@ -3190,7 +3214,8 @@ re_search(bufp, string, size, startpos, range, regs)
31903214

31913215
/* Update the fastmap now if not correct already. */
31923216
if (fastmap && !bufp->fastmap_accurate) {
3193-
re_compile_fastmap(bufp);
3217+
int ret = re_compile_fastmap0(bufp);
3218+
if (ret) return ret;
31943219
}
31953220

31963221

@@ -3580,7 +3605,7 @@ re_match_exec(bufp, string_arg, size, pos, beg, regs)
35803605
``dummy''; if a failure happens and the failure point is a dummy, it
35813606
gets discarded and the next next one is tried. */
35823607

3583-
unsigned char **stacka;
3608+
unsigned char **const stacka = 0;
35843609
unsigned char **stackb;
35853610
unsigned char **stackp;
35863611
unsigned char **stacke;
@@ -3629,8 +3654,7 @@ re_match_exec(bufp, string_arg, size, pos, beg, regs)
36293654
}
36303655

36313656
/* Initialize the stack. */
3632-
stacka = RE_TALLOC(MAX_NUM_FAILURE_ITEMS * NFAILURES, unsigned char*);
3633-
stackb = stacka;
3657+
stackb = TMALLOC(MAX_NUM_FAILURE_ITEMS * NFAILURES, unsigned char*);
36343658
stackp = stackb;
36353659
stacke = &stackb[MAX_NUM_FAILURE_ITEMS * NFAILURES];
36363660

@@ -4400,6 +4424,8 @@ re_match_exec(bufp, string_arg, size, pos, beg, regs)
44004424
goto restore_best_regs;
44014425

44024426
FREE_AND_RETURN(stackb,(-1)); /* Failure to match. */
4427+
memory_exhausted:
4428+
FREE_AND_RETURN(stackb,(-2));
44034429
}
44044430

44054431

@@ -4663,5 +4689,5 @@ utf8_startpos(string, pos)
46634689
mode : C
46644690
c-file-style : "gnu"
46654691
tab-width : 8
4666-
End :
4692+
End
46674693
*/

version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define RUBY_RELEASE_DATE "2008-08-04"
33
#define RUBY_VERSION_CODE 187
44
#define RUBY_RELEASE_CODE 20080804
5-
#define RUBY_PATCHLEVEL 67
5+
#define RUBY_PATCHLEVEL 68
66

77
#define RUBY_VERSION_MAJOR 1
88
#define RUBY_VERSION_MINOR 8

0 commit comments

Comments
 (0)