Skip to content

Commit 4fdbddb

Browse files
committed
c++: refactor ReusableBuffer to be 2D specific
This allows to conveniently protect against size overflows inside the ensure_size method.
1 parent 1797eb5 commit 4fdbddb

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/SubtitleOctopus.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@
2020

2121
int log_level = 3;
2222

23-
class ReusableBuffer {
23+
class ReusableBuffer2D {
2424
private:
2525
void *buffer;
2626
size_t size;
2727
int lessen_counter;
2828

2929
public:
30-
ReusableBuffer(): buffer(NULL), size(0), lessen_counter(0) {}
30+
ReusableBuffer2D(): buffer(NULL), size(0), lessen_counter(0) {}
3131

32-
~ReusableBuffer() {
32+
~ReusableBuffer2D() {
3333
free(buffer);
3434
}
3535

@@ -42,12 +42,16 @@ class ReusableBuffer {
4242

4343
/*
4444
* Request a raw pointer to a buffer being able to hold at least
45-
* the requested amount of data.
45+
* x times y values of size member_size.
4646
* On failure NULL is returned.
4747
* The pointer is valid during the lifetime of the ReusableBuffer
4848
* object until the next call to get_rawbuf or clear.
4949
*/
50-
void *get_rawbuf(size_t new_size) {
50+
void *get_rawbuf(size_t x, size_t y, size_t member_size) {
51+
if (x > SIZE_MAX / member_size / y)
52+
return NULL;
53+
54+
size_t new_size = x * y * member_size;
5155
if (!new_size) new_size = 1;
5256
if (size >= new_size) {
5357
if (size >= 1.3 * new_size) {
@@ -277,7 +281,7 @@ class SubtitleOctopus {
277281
}
278282

279283
// make float buffer for blending
280-
float* buf = (float*)m_blend.get_rawbuf(sizeof(float) * width * height * 4);
284+
float* buf = (float*)m_blend.get_rawbuf(width, height, sizeof(float) * 4);
281285
if (buf == NULL) {
282286
fprintf(stderr, "jso: cannot allocate buffer for blending\n");
283287
return &m_blendResult;
@@ -357,7 +361,7 @@ class SubtitleOctopus {
357361
}
358362

359363
private:
360-
ReusableBuffer m_blend;
364+
ReusableBuffer2D m_blend;
361365
RenderBlendResult m_blendResult;
362366
};
363367

0 commit comments

Comments
 (0)