Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error checks for malloc, calloc and write calls #17

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions iec16022.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
#include "iec16022ecc200.h"
#include "config.h"

// simple checked response malloc
static void *safemalloc(int n)
// simple checked response malloc
static void *safemalloc(size_t n)
{
void *p = malloc(n);
if (!p) {
fprintf(stderr, "Malloc(%d) failed\n", n);
exit(1);
perror("Malloc failed");
exit(EXIT_FAILURE);
}
return p;
}
Expand Down
18 changes: 9 additions & 9 deletions iec16022ecc200.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ static const struct ecc200matrix_s {
{0} // terminate
};

// simple checked response malloc
static void *safemalloc(int n, int s)
// simple checked response malloc
static void *safemalloc(size_t n)
{
void *p = calloc(n, s);
void *p = malloc(n);
if (!p) {
fprintf(stderr, "Malloc(%d) failed\n", n);
exit(1);
perror("Malloc failed");
exit(EXIT_FAILURE);
}
return p;
}
Expand Down Expand Up @@ -520,7 +520,7 @@ static char *encmake(int l, const unsigned char *s, int *lenp, char exact)
if (lenp)
*lenp = 0;
if (!l) {
encoding = safemalloc(1, 1);
encoding = safemalloc(1);
*encoding = 0;
return encoding; // no length
}
Expand Down Expand Up @@ -814,7 +814,7 @@ static char *encmake(int l, const unsigned char *s, int *lenp, char exact)
* fprintf (stderr, "\n");
*/
}
encoding = safemalloc(1, l + 1);
encoding = safemalloc(l + 1);
p = 0;
{
int cur = E_ASCII; // starts ASCII
Expand Down Expand Up @@ -958,9 +958,9 @@ unsigned char *iec16022ecc200f(int *Wptr, int *Hptr, char **encodingptr,
int x, y, NC, NR, *places;
NC = W - 2 * (W / matrix->FW);
NR = H - 2 * (H / matrix->FH);
places = safemalloc(NC, NR * sizeof(int));
places = safemalloc(NC * NR * sizeof(int));
ecc200placement(places, NR, NC);
grid = safemalloc(H, W + 16); // extra padding to simplify some operations
grid = safemalloc(H * (W + 16)); // extra padding to simplify some operations
for (y = 0; y < H; y += matrix->FH) {
memset(grid + y * W, 1, W);
for (x = 0; x < W; x += 2)
Expand Down
46 changes: 34 additions & 12 deletions image.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ static inline unsigned htonl(unsigned i)
#include <zlib.h>
#endif

// simple checked response write
static ssize_t safewrite(int f, const void *b, size_t c)
{
ssize_t n = write(f, b, c);
if (n == -1) {
perror("Write failed");
exit(EXIT_FAILURE);
}
return n;
}

// simple checked response malloc
static void *safemalloc(size_t n)
{
void *p = malloc(n);
if (!p) {
perror("Malloc failed");
exit(EXIT_FAILURE);
}
return p;
}

Image *ImageNew(int w, int h, int c)
{ // create a new blank image
Image *i = NULL;
Expand Down Expand Up @@ -101,7 +123,7 @@ static void make_crc_table(void)

static unsigned int writecrc(int fh, const char *ptr, int len, unsigned int c)
{
write(fh, ptr, len);
safewrite(fh, ptr, len);
while (len--)
c = crc_table[(c ^ *ptr++) & 0xff] ^ (c >> 8);
return c;
Expand All @@ -110,12 +132,12 @@ static unsigned int writecrc(int fh, const char *ptr, int len, unsigned int c)
static void writechunk(int fh, const char *typ, const void *ptr, int len)
{
unsigned int v = htonl(len), crc;
write(fh, &v, 4);
safewrite(fh, &v, 4);
crc = writecrc(fh, typ, 4, ~0);
if (len)
crc = writecrc(fh, ptr, len, crc);
v = htonl(~crc);
write(fh, &v, 4);
safewrite(fh, &v, 4);
}

#ifndef USEZLIB
Expand Down Expand Up @@ -155,7 +177,7 @@ static unsigned char *repack1bpp(const Image *i, int *packed_len)
{
int len = (i->L + 8 + 6) / 8 * i->H;
unsigned char *p = i->Image;
unsigned char *out = calloc(len, 1);
unsigned char *out = safemalloc(len);
unsigned char *tmp = out;
unsigned int n = i->H;
unsigned char mask = i->Colour[0] == 0 ? 0 : 0xff;
Expand Down Expand Up @@ -187,7 +209,7 @@ void ImageWritePNG(Image * i, int fh, int back, int trans, const char *comment)
is_1bpp &= (i->L + 8 + 6) / 8 * i->H <= 0xffff;
#endif
make_crc_table();
write(fh, "\211PNG\r\n\032\n", 8); // PNG header
safewrite(fh, "\211PNG\r\n\032\n", 8); // PNG header
{ // IHDR
struct {
unsigned int width;
Expand All @@ -205,14 +227,14 @@ void ImageWritePNG(Image * i, int fh, int back, int trans, const char *comment)
}
if (!is_1bpp) { // PLTE
unsigned int v = htonl(i->C * 3), crc, n;
write(fh, &v, 4);
safewrite(fh, &v, 4);
crc = writecrc(fh, "PLTE", 4, ~0);
for (n = 0; n < i->C; n++) {
v = htonl(i->Colour[n] << 8);
crc = writecrc(fh, (void *)&v, 3, crc);
}
v = htonl(~crc);
write(fh, &v, 4);
safewrite(fh, &v, 4);
}
if (back >= 0) { // bKGD
unsigned char b = back;
Expand All @@ -221,12 +243,12 @@ void ImageWritePNG(Image * i, int fh, int back, int trans, const char *comment)
if (*comment) { // tEXt
static const char c[] = "Comment";
unsigned int v = htonl(strlen(c) + strlen(comment) + 1), crc;
write(fh, &v, 4);
safewrite(fh, &v, 4);
crc = writecrc(fh, "tEXt", 4, ~0);
crc = writecrc(fh, c, strlen(c) + 1, crc);
crc = writecrc(fh, comment, strlen(comment), crc);
v = htonl(~crc);
write(fh, &v, 4);
safewrite(fh, &v, 4);
}
{ // tRNS
int has_alpha = 0;
Expand All @@ -250,7 +272,7 @@ void ImageWritePNG(Image * i, int fh, int back, int trans, const char *comment)
crc, adler = 1;
unsigned char *p = i->Image;
if (is_1bpp) v = htonl(5 + (i->L + 8 + 6) / 8 * i->H + 6);
write(fh, &v, 4);
safewrite(fh, &v, 4);
crc = writecrc(fh, "IDAT", 4, ~0);
crc = writecrc(fh, "\170\001", 2, crc); // zlib header for deflate
if (is_1bpp) {
Expand All @@ -269,7 +291,7 @@ void ImageWritePNG(Image * i, int fh, int back, int trans, const char *comment)
v = htonl(adler);
crc = writecrc(fh, (void *)&v, 4, crc);
v = htonl(~crc);
write(fh, &v, 4);
safewrite(fh, &v, 4);
}
#else
{ // IDAT
Expand All @@ -281,7 +303,7 @@ void ImageWritePNG(Image * i, int fh, int back, int trans, const char *comment)
i->Image[n * i->L] = 0; // filter 0
if (is_1bpp) data = repack1bpp(i, &len);
n = len * 1001 / 1000 + 12;
temp = malloc(n);
temp = safemalloc(n);
if (compress2(temp, &n, data, len, 9) != Z_OK)
fprintf(stderr, "Deflate error\n");
else
Expand Down
17 changes: 14 additions & 3 deletions reedsol.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ static int rlen;

static int *log = NULL, *alog = NULL, *rspoly = NULL;

// simple checked response calloc
static void *safecalloc(size_t n, size_t s)
{
void *p = calloc(n, s);
if (!p) {
perror("Calloc failed");
exit(EXIT_FAILURE);
}
return p;
}

// rs_init_gf(poly) initialises the parameters for the Galois Field.
// The symbol size is determined from the highest bit set in poly
// This implementation will support sizes up to 30 bits (though that
Expand Down Expand Up @@ -80,8 +91,8 @@ void rs_init_gf(int poly)

// Calculate the log/alog tables
logmod = (1 << m) - 1;
log = (int *)calloc(logmod + 1, sizeof(*log));
alog = (int *)calloc(logmod, sizeof(*alog));
log = (int *)safecalloc(logmod + 1, sizeof(*log));
alog = (int *)safecalloc(logmod, sizeof(*alog));

for (p = 1, v = 0; v < logmod; v++) {
alog[v] = p;
Expand All @@ -105,7 +116,7 @@ void rs_init_code(int nsym, int index)

if (rspoly)
free(rspoly);
rspoly = (int *)malloc(sizeof(int) * (nsym + 1));
rspoly = (int *)safecalloc(nsym + 1, sizeof(int));

rlen = nsym;

Expand Down