Skip to content

Commit

Permalink
Check array bounds when doing matrix lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
SjoerdLangkemper-sfy committed Oct 18, 2021
1 parent 8e497d8 commit 2e730a9
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions mpck/checkframe.c
Expand Up @@ -38,14 +38,17 @@
#include <errno.h>
#endif

/* return matrix[x][y], or 0 if x or y are out of bounds */
#define matrix_lookup(matrix, x, y) (x < sizeof(matrix) / sizeof(matrix[0]) && y < sizeof(matrix[0]) / sizeof(matrix[0][0]) ? matrix[x][y] : 0)

/* samplerate in Hz of frame fi with headervalue h */
#define samplerate(h, fi) samplerate_matrix[h][fi->version]
#define samplerate(h, fi) matrix_lookup(samplerate_matrix, h, fi->version)

/* the duration of this frame in ms */
#define frametime(fi) (1000*fi->samples/fi->samplerate)

/* the number of samples in frame fi */
#define framesamples(fi) samples_matrix[fi->layer][fi->version]
#define framesamples(fi) matrix_lookup(samples_matrix, fi->layer, fi->version)

/* the layer for headervalue h */
#define layer(h) (4-(h))
Expand Down Expand Up @@ -108,7 +111,7 @@ setconsistent(file_info * file, const frame_info * frame)
/* returns bitrate in bps */
static int bitrate(int headervalue, frame_info * fi)
{
return 1000*bitrate_matrix[3*fi->version+fi->layer-1][headervalue];
return 1000*matrix_lookup(bitrate_matrix, 3*fi->version+fi->layer-1, headervalue);
}

static int
Expand Down

0 comments on commit 2e730a9

Please sign in to comment.