Skip to content

Commit

Permalink
SWORD25: Add error checks after allocating memory with malloc
Browse files Browse the repository at this point in the history
  • Loading branch information
Templier committed Jun 23, 2011
1 parent 53c4a19 commit e972c6c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 0 deletions.
11 changes: 11 additions & 0 deletions engines/sword25/fmv/theora_decoder.cpp
Expand Up @@ -424,6 +424,12 @@ bool TheoraDecoder::queueAudio() {
if (!_audStream)
return false;

// An audio buffer should have been allocated (either in the constructor or after queuing the current buffer)
if (!_audiobuf) {
warning("[TheoraDecoder::queueAudio] Invalid audio buffer");
return false;
}

bool queuedAudio = false;

for (;;) {
Expand Down Expand Up @@ -454,6 +460,11 @@ bool TheoraDecoder::queueAudio() {
// The audio mixer is now responsible for the old audio buffer.
// We need to create a new one.
_audiobuf = (ogg_int16_t *)malloc(AUDIOFD_FRAGSIZE * sizeof(ogg_int16_t));
if (!_audiobuf) {
warning("[TheoraDecoder::queueAudio] Cannot allocate memory for audio buffer");
return false;
}

_audiobufFill = 0;
queuedAudio = true;
}
Expand Down
27 changes: 27 additions & 0 deletions engines/sword25/gfx/image/art.cpp
Expand Up @@ -151,6 +151,8 @@ ArtSVP *art_svp_from_vpath(ArtVpath *vpath) {
n_segs_max = 16;
svp = (ArtSVP *)malloc(sizeof(ArtSVP) +
(n_segs_max - 1) * sizeof(ArtSVPSeg));
if (!svp)
error("[art_svp_from_vpath] Cannot allocate memory");

dir = 0;
n_points = 0;
Expand Down Expand Up @@ -1041,6 +1043,8 @@ struct _ArtPriPoint {

static ArtPriQ *art_pri_new(void) {
ArtPriQ *result = art_new(ArtPriQ, 1);
if (!result)
error("[art_pri_new] Cannot allocate memory");

result->n_items = 0;
result->n_items_max = 16;
Expand Down Expand Up @@ -1189,6 +1193,9 @@ static int art_svp_writer_rewind_add_segment(ArtSvpWriter *self, int wind_left,
seg->bbox.x1 = x;
seg->bbox.y1 = y;
seg->points = art_new(ArtPoint, init_n_points_max);
if (!seg->points)
error("[art_svp_writer_rewind_add_segment] Cannot allocate memory");

seg->points[0].x = x;
seg->points[0].y = y;
return seg_num;
Expand Down Expand Up @@ -1233,6 +1240,8 @@ ArtSVP *art_svp_writer_rewind_reap(ArtSvpWriter *self) {

ArtSvpWriter *art_svp_writer_rewind_new(ArtWindRule rule) {
ArtSvpWriterRewind *result = art_new(ArtSvpWriterRewind, 1);
if (!result)
error("[art_svp_writer_rewind_new] Cannot allocate memory");

result->super.add_segment = art_svp_writer_rewind_add_segment;
result->super.add_point = art_svp_writer_rewind_add_point;
Expand All @@ -1242,6 +1251,9 @@ ArtSvpWriter *art_svp_writer_rewind_new(ArtWindRule rule) {
result->n_segs_max = 16;
result->svp = (ArtSVP *)malloc(sizeof(ArtSVP) +
(result->n_segs_max - 1) * sizeof(ArtSVPSeg));
if (!result->svp)
error("[art_svp_writer_rewind_new] Cannot allocate memory");

result->svp->n_segs = 0;
result->n_points_max = art_new(int, result->n_segs_max);

Expand Down Expand Up @@ -1412,6 +1424,9 @@ static void art_svp_intersect_push_pt(ArtIntersectCtx *ctx, ArtActiveSeg *seg,
seg->y1 = y;

pri_pt = art_new(ArtPriPoint, 1);
if (!pri_pt)
error("[art_svp_intersect_push_pt] Cannot allocate memory");

pri_pt->x = x;
pri_pt->y = y;
pri_pt->user_data = seg;
Expand Down Expand Up @@ -1875,6 +1890,8 @@ static void art_svp_intersect_horiz(ArtIntersectCtx *ctx, ArtActiveSeg *seg,
return;

hs = art_new(ArtActiveSeg, 1);
if (!hs)
error("[art_svp_intersect_horiz] Cannot allocate memory");

hs->flags = ART_ACTIVE_FLAGS_DEL | (seg->flags & ART_ACTIVE_FLAGS_OUT);
if (seg->flags & ART_ACTIVE_FLAGS_OUT) {
Expand Down Expand Up @@ -2017,6 +2034,8 @@ static void art_svp_intersect_add_seg(ArtIntersectCtx *ctx, const ArtSVPSeg *in_
ArtActiveSeg *last = NULL;
ArtActiveSeg *left, *right;
ArtPriPoint *pri_pt = art_new(ArtPriPoint, 1);
if (!pri_pt)
error("[art_svp_intersect_add_seg] Cannot allocate memory");

seg->flags = 0;
seg->in_seg = in_seg;
Expand Down Expand Up @@ -2186,6 +2205,9 @@ void art_svp_intersector(const ArtSVP *in, ArtSvpWriter *out) {
return;

ctx = art_new(ArtIntersectCtx, 1);
if (!ctx)
error("[art_svp_intersector] Cannot allocate memory");

ctx->in = in;
ctx->out = out;
pq = art_pri_new();
Expand All @@ -2198,6 +2220,9 @@ void art_svp_intersector(const ArtSVP *in, ArtSvpWriter *out) {

ctx->in_curs = 0;
first_point = art_new(ArtPriPoint, 1);
if (!first_point)
error("[art_svp_intersector] Cannot allocate memory");

first_point->x = in->segs[0].points[0].x;
first_point->y = in->segs[0].points[0].y;
first_point->user_data = NULL;
Expand Down Expand Up @@ -2339,6 +2364,8 @@ static void art_svp_render_delete_active(int *active_segs, int j, int n_active_s
ArtSVPRenderAAIter *art_svp_render_aa_iter(const ArtSVP *svp,
int x0, int y0, int x1, int y1) {
ArtSVPRenderAAIter *iter = art_new(ArtSVPRenderAAIter, 1);
if (!iter)
error("[art_svp_render_aa_iter] Cannot allocate memory");

iter->svp = svp;
iter->y = y0;
Expand Down
1 change: 1 addition & 0 deletions engines/sword25/gfx/image/art.h
Expand Up @@ -57,6 +57,7 @@ namespace Sword25 {
} else { \
max = 1; \
p = art_new(type, 1); \
if (!p) error("Cannot allocate memory for art data"); \
} \
} while (0)

Expand Down
2 changes: 2 additions & 0 deletions engines/sword25/gfx/image/vectorimage.cpp
Expand Up @@ -321,6 +321,8 @@ ArtBpath *VectorImage::storeBez(ArtBpath *bez, int lineStyle, int fillStyle0, in
bez[*bezNodes].code = ART_END;

ArtBpath *bez1 = art_new(ArtBpath, *bezNodes + 1);
if (!bez1)
error("[VectorImage::storeBez] Cannot allocate memory");

for (int i = 0; i <= *bezNodes; i++)
bez1[i] = bez[i];
Expand Down
7 changes: 7 additions & 0 deletions engines/sword25/gfx/image/vectorimagerenderer.cpp
Expand Up @@ -270,6 +270,9 @@ ArtVpath *art_vpath_cat(ArtVpath *a, ArtVpath *b) {
len_a = art_vpath_len(a);
len_b = art_vpath_len(b);
dest = art_new(ArtVpath, len_a + len_b + 1);
if (!dest)
error("[art_vpath_cat] Cannot allocate memory");

p = dest;

for (int i = 0; i < len_a; i++)
Expand Down Expand Up @@ -299,6 +302,8 @@ ArtVpath *art_vpath_reverse(ArtVpath *a) {

len = art_vpath_len(a);
dest = art_new(ArtVpath, len + 1);
if (!dest)
error("[art_vpath_reverse] Cannot allocate memory");

for (i = 0; i < len; i++) {
it = a[len - i - 1];
Expand Down Expand Up @@ -371,6 +376,8 @@ void drawBez(ArtBpath *bez1, ArtBpath *bez2, byte *buffer, int width, int height

int size = art_vpath_len(vec);
ArtVpath *vect = art_new(ArtVpath, size + 1);
if (!vect)
error("[drawBez] Cannot allocate memory");

int k;
for (k = 0; k < size; k++) {
Expand Down
3 changes: 3 additions & 0 deletions engines/sword25/package/packagemanager.h
Expand Up @@ -141,6 +141,9 @@ class PackageManager : public Service {
uint fileSize;
char *data = (char *)getFile(fileName, &fileSize);
char *result = (char *)malloc(fileSize + strlen(versionStr) + 1);
if (!result)
error("[PackageManager::getXmlFile] Cannot allocate memory");

strcpy(result, versionStr);
Common::copy(data, data + fileSize, result + strlen(versionStr));
result[fileSize + strlen(versionStr)] = '\0';
Expand Down

0 comments on commit e972c6c

Please sign in to comment.