@@ -34,8 +34,9 @@ class VM_BufferWriter {
34
34
U_8 *_buffer;
35
35
U_8 *_cursor;
36
36
UDATA _size;
37
-
37
+ U_8 *_bufferEnd;
38
38
U_8 *_maxCursor;
39
+ bool _overflow;
39
40
40
41
#if defined(J9VM_ENV_LITTLE_ENDIAN)
41
42
static const bool _isLE = true ;
@@ -94,8 +95,26 @@ class VM_BufferWriter {
94
95
: _buffer(buffer)
95
96
, _cursor(buffer)
96
97
, _size(size)
98
+ , _bufferEnd(buffer + size)
97
99
, _maxCursor(NULL )
100
+ , _overflow(false )
101
+ {
102
+ }
103
+
104
+ bool
105
+ checkBounds (UDATA size)
106
+ {
107
+ if ((_cursor + size) >= _bufferEnd) {
108
+ _overflow = true ;
109
+ }
110
+
111
+ return !_overflow;
112
+ }
113
+
114
+ bool
115
+ overflowOccurred ()
98
116
{
117
+ return _overflow;
99
118
}
100
119
101
120
U_64
@@ -123,50 +142,66 @@ class VM_BufferWriter {
123
142
}
124
143
125
144
void
126
- writeU8 (U_8 val)
145
+ writeU8NoCheck (U_8 val)
127
146
{
128
147
*_cursor = val;
129
148
_cursor += sizeof (U_8);
130
149
}
131
150
151
+ void
152
+ writeU8 (U_8 val)
153
+ {
154
+ if (checkBounds (sizeof (U_8))) {
155
+ writeU8NoCheck (val);
156
+ }
157
+ }
158
+
132
159
void
133
160
writeU16 (U_16 val)
134
161
{
135
- U_16 newVal = val;
136
- if (_isLE) {
137
- newVal = byteSwap (val);
162
+ if (checkBounds (sizeof (U_16))) {
163
+ U_16 newVal = val;
164
+ if (_isLE) {
165
+ newVal = byteSwap (val);
166
+ }
167
+ *(U_16 *)_cursor = newVal;
168
+ _cursor += sizeof (U_16);
138
169
}
139
- *(U_16 *)_cursor = newVal;
140
- _cursor += sizeof (U_16);
141
170
}
142
171
143
172
void
144
173
writeU32 (U_32 val)
145
174
{
146
- U_32 newVal = val;
147
- if (_isLE) {
148
- newVal = byteSwap (val);
175
+ if (checkBounds (sizeof (U_32))) {
176
+ U_32 newVal = val;
177
+ if (_isLE) {
178
+ newVal = byteSwap (val);
179
+ }
180
+ *(U_32 *)_cursor = newVal;
181
+ _cursor += sizeof (U_32);
149
182
}
150
- *(U_32 *)_cursor = newVal;
151
- _cursor += sizeof (U_32);
152
183
}
153
184
154
185
void
155
186
writeU64 (U_64 val)
156
187
{
157
- U_64 newVal = val;
158
- if (_isLE) {
159
- newVal = byteSwap (val);
188
+ if (checkBounds (sizeof (U_64))) {
189
+ U_64 newVal = val;
190
+ if (_isLE) {
191
+ newVal = byteSwap (val);
192
+ }
193
+ *(U_64 *)_cursor = newVal;
194
+ _cursor += sizeof (U_64);
160
195
}
161
- *(U_64 *)_cursor = newVal;
162
- _cursor += sizeof (U_64);
163
196
}
164
197
165
198
void
166
199
writeData (U_8 *data, UDATA size)
167
200
{
168
- memcpy (_cursor, data, size);
169
- _cursor += size;
201
+ if (checkBounds (size)) {
202
+ memcpy (_cursor, data, size);
203
+ _cursor += size;
204
+ }
170
205
}
171
206
172
207
U_8 *
@@ -203,19 +238,21 @@ class VM_BufferWriter {
203
238
void
204
239
writeLEB128 (U_64 val)
205
240
{
206
- U_64 newVal = val;
207
- if (!_isLE) {
208
- newVal = byteSwap (val);
209
- }
210
- do {
211
- U_8 byte = newVal & 0x7F ;
212
- newVal >>= 7 ;
213
-
214
- if (newVal > 0 ) {
215
- byte |= 0x80 ;
241
+ if (checkBounds (9 )) {
242
+ U_64 newVal = val;
243
+ if (!_isLE) {
244
+ newVal = byteSwap (val);
216
245
}
217
- writeU8 (byte);
218
- } while (newVal > 0 );
246
+ do {
247
+ U_8 byte = newVal & 0x7F ;
248
+ newVal >>= 7 ;
249
+
250
+ if (newVal > 0 ) {
251
+ byte |= 0x80 ;
252
+ }
253
+ writeU8NoCheck (byte);
254
+ } while (newVal > 0 );
255
+ }
219
256
}
220
257
221
258
void
@@ -230,19 +267,21 @@ class VM_BufferWriter {
230
267
void
231
268
writeLEB128PaddedU72 (U_64 val)
232
269
{
233
- U_64 newVal = val;
234
- if (!_isLE) {
235
- newVal = byteSwap (val);
270
+ if (checkBounds (9 )) {
271
+ U_64 newVal = val;
272
+ if (!_isLE) {
273
+ newVal = byteSwap (val);
274
+ }
275
+ writeU8NoCheck ((newVal & 0x7F ) | 0x80 );
276
+ writeU8NoCheck (((newVal >> 7 ) & 0x7F ) | 0x80 );
277
+ writeU8NoCheck (((newVal >> 14 ) & 0x7F ) | 0x80 );
278
+ writeU8NoCheck (((newVal >> 21 ) & 0x7F ) | 0x80 );
279
+ writeU8NoCheck (((newVal >> 28 ) & 0x7F ) | 0x80 );
280
+ writeU8NoCheck (((newVal >> 35 ) & 0x7F ) | 0x80 );
281
+ writeU8NoCheck (((newVal >> 42 ) & 0x7F ) | 0x80 );
282
+ writeU8NoCheck (((newVal >> 49 ) & 0x7F ) | 0x80 );
283
+ writeU8NoCheck (((newVal >> 56 ) & 0x7F ));
236
284
}
237
- writeU8 ((newVal & 0x7F ) | 0x80 );
238
- writeU8 (((newVal >> 7 ) & 0x7F ) | 0x80 );
239
- writeU8 (((newVal >> 14 ) & 0x7F ) | 0x80 );
240
- writeU8 (((newVal >> 21 ) & 0x7F ) | 0x80 );
241
- writeU8 (((newVal >> 28 ) & 0x7F ) | 0x80 );
242
- writeU8 (((newVal >> 35 ) & 0x7F ) | 0x80 );
243
- writeU8 (((newVal >> 42 ) & 0x7F ) | 0x80 );
244
- writeU8 (((newVal >> 49 ) & 0x7F ) | 0x80 );
245
- writeU8 (((newVal >> 56 ) & 0x7F ));
246
285
}
247
286
248
287
void
@@ -257,18 +296,20 @@ class VM_BufferWriter {
257
296
void
258
297
writeLEB128PaddedU64 (U_64 val)
259
298
{
260
- U_64 newVal = val;
261
- if (!_isLE) {
262
- newVal = byteSwap (val);
299
+ if (checkBounds (sizeof (U_64))) {
300
+ U_64 newVal = val;
301
+ if (!_isLE) {
302
+ newVal = byteSwap (val);
303
+ }
304
+ writeU8NoCheck ((newVal & 0x7F ) | 0x80 );
305
+ writeU8NoCheck (((newVal >> 7 ) & 0x7F ) | 0x80 );
306
+ writeU8NoCheck (((newVal >> 14 ) & 0x7F ) | 0x80 );
307
+ writeU8NoCheck (((newVal >> 21 ) & 0x7F ) | 0x80 );
308
+ writeU8NoCheck (((newVal >> 28 ) & 0x7F ) | 0x80 );
309
+ writeU8NoCheck (((newVal >> 35 ) & 0x7F ) | 0x80 );
310
+ writeU8NoCheck (((newVal >> 42 ) & 0x7F ) | 0x80 );
311
+ writeU8NoCheck (((newVal >> 49 ) & 0x7F ));
263
312
}
264
- writeU8 ((newVal & 0x7F ) | 0x80 );
265
- writeU8 (((newVal >> 7 ) & 0x7F ) | 0x80 );
266
- writeU8 (((newVal >> 14 ) & 0x7F ) | 0x80 );
267
- writeU8 (((newVal >> 21 ) & 0x7F ) | 0x80 );
268
- writeU8 (((newVal >> 28 ) & 0x7F ) | 0x80 );
269
- writeU8 (((newVal >> 35 ) & 0x7F ) | 0x80 );
270
- writeU8 (((newVal >> 42 ) & 0x7F ) | 0x80 );
271
- writeU8 (((newVal >> 49 ) & 0x7F ));
272
313
}
273
314
274
315
void
@@ -283,14 +324,16 @@ class VM_BufferWriter {
283
324
void
284
325
writeLEB128PaddedU32 (U_32 val)
285
326
{
286
- U_64 newVal = val;
287
- if (!_isLE) {
288
- newVal = byteSwap (val);
327
+ if (checkBounds (sizeof (U_32))) {
328
+ U_64 newVal = val;
329
+ if (!_isLE) {
330
+ newVal = byteSwap (val);
331
+ }
332
+ writeU8NoCheck ((newVal & 0x7F ) | 0x80 );
333
+ writeU8NoCheck (((newVal >> 7 ) & 0x7F ) | 0x80 );
334
+ writeU8NoCheck (((newVal >> 14 ) & 0x7F ) | 0x80 );
335
+ writeU8NoCheck (((newVal >> 21 ) & 0x7F ));
289
336
}
290
- writeU8 ((newVal & 0x7F ) | 0x80 );
291
- writeU8 (((newVal >> 7 ) & 0x7F ) | 0x80 );
292
- writeU8 (((newVal >> 14 ) & 0x7F ) | 0x80 );
293
- writeU8 (((newVal >> 21 ) & 0x7F ));
294
337
}
295
338
296
339
static U_32
0 commit comments