50
50
/* We need this for `regex.h', and perhaps for the Emacs include files. */
51
51
# include <sys/types.h>
52
52
#endif
53
+ #ifdef HAVE_STDLIB_H
54
+ # include <stdlib.h>
55
+ #endif
53
56
54
57
#if !defined(__STDC__ ) && !defined(_MSC_VER )
55
58
# define volatile
63
66
64
67
#ifdef RUBY_PLATFORM
65
68
#include "defines.h"
69
+ #undef xmalloc
70
+ #undef xrealloc
71
+ #undef xcalloc
72
+ #undef xfree
66
73
67
74
# define RUBY
68
75
extern int rb_prohibit_interrupt ;
@@ -104,6 +111,11 @@ void *alloca ();
104
111
# include <strings.h>
105
112
#endif
106
113
114
+ #define xmalloc malloc
115
+ #define xrealloc realloc
116
+ #define xcalloc calloc
117
+ #define xfree free
118
+
107
119
#ifdef C_ALLOCA
108
120
#define FREE_VARIABLES () alloca(0)
109
121
#else
@@ -127,10 +139,12 @@ void *alloca ();
127
139
unsigned int xlen = stacke - stackb; \
128
140
if (stackb == stacka) { \
129
141
stackx = (type*)xmalloc(2 * xlen * sizeof(type)); \
142
+ if (!stackx) goto memory_exhausted; \
130
143
memcpy(stackx, stackb, xlen * sizeof (type)); \
131
144
} \
132
145
else { \
133
146
stackx = (type*)xrealloc(stackb, 2 * xlen * sizeof(type)); \
147
+ if (!stackx) goto memory_exhausted; \
134
148
} \
135
149
/* Rearrange the pointers. */ \
136
150
stackp = stackx + (stackp - stackb ); \
@@ -2775,8 +2789,8 @@ bm_search(little, llen, big, blen, skip, translate)
2775
2789
The caller must supply the address of a (1 << BYTEWIDTH)-byte data
2776
2790
area as bufp->fastmap.
2777
2791
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 )
2780
2794
struct re_pattern_buffer * bufp ;
2781
2795
{
2782
2796
unsigned char * pattern = (unsigned char * )bufp -> buffer ;
@@ -2944,7 +2958,7 @@ re_compile_fastmap(bufp)
2944
2958
fastmap [j ] = 1 ;
2945
2959
}
2946
2960
if (bufp -> can_be_null ) {
2947
- FREE_AND_RETURN_VOID (stackb );
2961
+ FREE_AND_RETURN (stackb , 0 );
2948
2962
}
2949
2963
/* Don't return; check the alternative paths
2950
2964
so we can set can_be_null if appropriate. */
@@ -3110,7 +3124,16 @@ re_compile_fastmap(bufp)
3110
3124
else
3111
3125
break ;
3112
3126
}
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 );
3114
3137
}
3115
3138
3116
3139
/* adjust startpos value to the position between characters. */
@@ -3144,7 +3167,8 @@ re_adjust_startpos(bufp, string, size, startpos, range)
3144
3167
{
3145
3168
/* Update the fastmap now if not correct already. */
3146
3169
if (!bufp -> fastmap_accurate ) {
3147
- re_compile_fastmap (bufp );
3170
+ int ret = re_compile_fastmap0 (bufp );
3171
+ if (ret ) return ret ;
3148
3172
}
3149
3173
3150
3174
/* Adjust startpos for mbc string */
@@ -3190,7 +3214,8 @@ re_search(bufp, string, size, startpos, range, regs)
3190
3214
3191
3215
/* Update the fastmap now if not correct already. */
3192
3216
if (fastmap && !bufp -> fastmap_accurate ) {
3193
- re_compile_fastmap (bufp );
3217
+ int ret = re_compile_fastmap0 (bufp );
3218
+ if (ret ) return ret ;
3194
3219
}
3195
3220
3196
3221
@@ -3580,7 +3605,7 @@ re_match_exec(bufp, string_arg, size, pos, beg, regs)
3580
3605
``dummy''; if a failure happens and the failure point is a dummy, it
3581
3606
gets discarded and the next next one is tried. */
3582
3607
3583
- unsigned char * * stacka ;
3608
+ unsigned char * * const stacka = 0 ;
3584
3609
unsigned char * * stackb ;
3585
3610
unsigned char * * stackp ;
3586
3611
unsigned char * * stacke ;
@@ -3629,8 +3654,7 @@ re_match_exec(bufp, string_arg, size, pos, beg, regs)
3629
3654
}
3630
3655
3631
3656
/* 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 * );
3634
3658
stackp = stackb ;
3635
3659
stacke = & stackb [MAX_NUM_FAILURE_ITEMS * NFAILURES ];
3636
3660
@@ -4400,6 +4424,8 @@ re_match_exec(bufp, string_arg, size, pos, beg, regs)
4400
4424
goto restore_best_regs ;
4401
4425
4402
4426
FREE_AND_RETURN (stackb ,(-1 )); /* Failure to match. */
4427
+ memory_exhausted :
4428
+ FREE_AND_RETURN (stackb ,(-2 ));
4403
4429
}
4404
4430
4405
4431
@@ -4663,5 +4689,5 @@ utf8_startpos(string, pos)
4663
4689
mode : C
4664
4690
c-file-style : "gnu"
4665
4691
tab-width : 8
4666
- End :
4692
+ End
4667
4693
*/
0 commit comments