Skip to content

Commit

Permalink
Check for memory errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmsc committed Sep 25, 2023
1 parent 21c8d0b commit 2e82c66
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/atr.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ struct atr_image *load_atr_image(const char *file_name)
num_sectors * ssz - pad_size);
}
// Allocate new storage
uint8_t *data = calloc(ssz, num_sectors);
uint8_t *data = check_calloc(ssz, num_sectors);
// Read 3 first sectors
for( unsigned i = 0; i < num_sectors; i++ )
{
Expand Down Expand Up @@ -132,7 +132,7 @@ struct atr_image *load_atr_image(const char *file_name)
}
fclose(f);
// Ok, copy to image
struct atr_image *atr = malloc(sizeof(struct atr_image));
struct atr_image *atr = check_malloc(sizeof(struct atr_image));
atr->data = data;
atr->sec_size = ssz;
atr->sec_count = num_sectors;
Expand Down
14 changes: 5 additions & 9 deletions src/darray.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,22 @@
*/

#include "darray.h"
#include "msg.h"
#include <stdio.h>

static void memory_error(void)
{
fprintf(stderr, "INTERNAL ERROR: memory allocation failure.\n");
abort();
}

void darray_fill_ptr(void *arr, size_t sz, size_t init)
{
darray(char) *ret = arr;
if( !ret || !(ret->data = malloc(sz * init)) )
if( !ret )
memory_error();
ret->data = check_malloc(sz * init);
ret->len = 0;
ret->size = init;
}

void *darray_alloc(size_t sz, size_t init)
{
darray(char) *ret = malloc(sizeof(darray(char)));
darray(char) *ret = check_malloc(sizeof(darray(char)));
darray_fill_ptr(ret, sz, init);
return ret;
}
Expand All @@ -46,7 +42,7 @@ void darray_grow(void *arr, size_t sz, size_t newsize)
while( newsize > p->size )
{
p->size *= 2;
if( !p->size || !(p->data = realloc(p->data, sz * p->size)) )
if( !p->size || !(p->data = check_realloc(p->data, sz * p->size)) )
memory_error();
}
}
Expand Down
14 changes: 8 additions & 6 deletions src/flist.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static char *read_file(const char *fname, size_t size)
FILE *f = fopen(fname, "rb");
if( !f )
show_error("can't open file '%s': %s", fname, strerror(errno));
data = malloc(size);
data = check_malloc(size);
if( size != fread(data, 1, size, f) )
show_error("error reading file '%s': %s", fname, strerror(errno));
fclose(f);
Expand All @@ -55,6 +55,8 @@ static char *atari_name(const char *fname)
{
// Convert to 8+3 filename
char *out = strdup(" ");
if( !out )
memory_error();

// Search last part of filename (similar to "basename")
const char *in, *p;
Expand Down Expand Up @@ -102,7 +104,7 @@ static char *atari_name(const char *fname)
static char *path_name(const char *dir, const char *name)
{
size_t n = strlen(dir);
char *ret = malloc(n + 14);
char *ret = check_malloc(n + 14);
strcpy(ret, dir);
ret[n++] = '>';
int i;
Expand All @@ -122,7 +124,7 @@ static char *path_name(const char *dir, const char *name)
void flist_add_main_dir(file_list *flist)
{
// Creates MAIN directory
struct afile *dir = malloc(sizeof(struct afile));
struct afile *dir = check_malloc(sizeof(struct afile));
// Convert time to broken time
time_t ttim = time(0);
struct tm *tim = localtime(&ttim);
Expand All @@ -140,7 +142,7 @@ void flist_add_main_dir(file_list *flist)
dir->size = 23;
dir->is_dir = 1;
dir->boot_file = 0;
dir->data = malloc(SFS_MAX_DIR_SIZE);
dir->data = check_malloc(SFS_MAX_DIR_SIZE);
dir->level = 0;

darray_add(flist, dir);
Expand All @@ -156,7 +158,7 @@ void flist_add_file(file_list *flist, const char *fname, int boot_file,

if( S_ISREG(st.st_mode) || S_ISDIR(st.st_mode) )
{
struct afile *f = malloc(sizeof(struct afile));
struct afile *f = check_malloc(sizeof(struct afile));

// Search in the file list if the path is inside an added directory
struct afile *dir = 0, **ptr;
Expand Down Expand Up @@ -203,7 +205,7 @@ void flist_add_file(file_list *flist, const char *fname, int boot_file,
f->size = 23;
f->is_dir = 1;
f->boot_file = 0;
f->data = malloc(SFS_MAX_DIR_SIZE);
f->data = check_malloc(SFS_MAX_DIR_SIZE);

show_msg("added dir '%-20s', from '%s'.", f->pname, f->fname);
}
Expand Down
20 changes: 10 additions & 10 deletions src/lsdos.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ static unsigned read_file(struct atr_image *atr, unsigned sect, unsigned size,
uint8_t *data, int dos2, int mdos)
{
// To avoid circular references, keep a bitmap with all the sectors already used
uint8_t *visited = calloc(65536, 1);
uint8_t *visited = check_calloc(65536, 1);
unsigned lst = atr->sec_size - 3;
unsigned pos = 0;
while( sect )
Expand All @@ -99,8 +99,8 @@ static unsigned read_file(struct atr_image *atr, unsigned sect, unsigned size,
show_msg("invalid sector link");
break;
}
int len = m[lst + 2];
int link = (m[lst] << 8) | m[lst + 1];
unsigned len = m[lst + 2];
unsigned link = (m[lst] << 8) | m[lst + 1];

// DOS 1.0, only last sector has a size field
if( !dos2 && !mdos )
Expand Down Expand Up @@ -152,7 +152,7 @@ static void read_dir(struct lsdos *ls, unsigned dir, const char *name)
if( ls->atari_list )
printf("Directory of %s\n\n", *name ? name : "/");

for( unsigned fn = 0; fn < ls->dir_size; fn++ )
for( int fn = 0; fn < ls->dir_size; fn++ )
{
const uint8_t *data = dir_data(ls, dir, fn);
if( !data )
Expand Down Expand Up @@ -213,7 +213,7 @@ static void read_dir(struct lsdos *ls, unsigned dir, const char *name)
int mdos = flags & 0x04;
int dos2 = flags & 0x02;
int max_size = size * ssize;
uint8_t *fdata = malloc(max_size);
uint8_t *fdata = check_malloc(max_size);
unsigned fsize = 0;
// Skip files of size 0
if( max_size > 0 )
Expand Down Expand Up @@ -255,7 +255,7 @@ static void read_dir(struct lsdos *ls, unsigned dir, const char *name)
if( ls->atari_list )
{
printf("\n");
for( unsigned fn = 0; fn < ls->dir_size; fn++ )
for( int fn = 0; fn < ls->dir_size; fn++ )
{
const uint8_t *data = dir_data(ls, dir, fn);
if( !data )
Expand Down Expand Up @@ -283,13 +283,13 @@ int dos_read(struct atr_image *atr, const char *atr_name, int atari_list, int lo
int extract_files)
{
// Check DOS filesystem
if( atr->sec_count < 361 )
// Read VTOC
const uint8_t *vtoc = atr_data(atr, 360);
if( !vtoc )
{
show_msg("%s: ATR image with too few sectors.", atr_name);
return 1;
}
// Read VTOC
const uint8_t *vtoc = atr_data(atr, 360);
unsigned signature = vtoc[0];
unsigned alloc_sect = read16(vtoc + 1);
unsigned free_sect = read16(vtoc + 3);
Expand Down Expand Up @@ -377,7 +377,7 @@ int dos_read(struct atr_image *atr, const char *atr_name, int atari_list, int lo
printf("%s: %u sectors of %u bytes, %s, %d sectors free of %d total.\n", atr_name,
atr->sec_count, atr->sec_size, dosver, free_sect, alloc_sect);

struct lsdos *ls = malloc(sizeof(struct lsdos));
struct lsdos *ls = check_malloc(sizeof(struct lsdos));
ls->atr = atr;
ls->atari_list = atari_list;
ls->lower_case = lower_case;
Expand Down
6 changes: 3 additions & 3 deletions src/lssfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ static void read_dir(struct lssfs *ls, unsigned map, const char *name)
if( ls->atari_list )
printf("Directory of %s\n\n", *name ? name : "/");

uint8_t *data = malloc(65536); // max directory size (2848 entries)
uint8_t *data = check_malloc(65536); // max directory size (2848 entries)
unsigned len = read_file(ls->atr, map, 65536, data);
if( !len )
{
Expand Down Expand Up @@ -248,7 +248,7 @@ static void read_dir(struct lssfs *ls, unsigned map, const char *name)
}
else
{
uint8_t *fdata = malloc(fsize);
uint8_t *fdata = check_malloc(fsize);
unsigned r = read_file(ls->atr, fmap, fsize, fdata);
if( r != fsize )
show_msg("%s: short file in disk", new_name);
Expand Down Expand Up @@ -362,7 +362,7 @@ int sfs_read(struct atr_image *atr, const char *atr_name, int atari_list, int lo
printf("%s: %u sectors of %u bytes, volume name '%s'.\n", atr_name,
atr->sec_count, atr->sec_size, vol_name);

struct lssfs *ls = malloc(sizeof(struct lssfs));
struct lssfs *ls = check_malloc(sizeof(struct lssfs));
ls->atr = atr;
ls->atari_list = atari_list;
ls->lower_case = lower_case;
Expand Down
29 changes: 29 additions & 0 deletions src/msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,32 @@ void show_version(void)
prog_version);
exit(EXIT_SUCCESS);
}

void memory_error(void)
{
show_error("memory error!\n");
}

void *check_malloc(size_t size)
{
void *ret = malloc(size);
if( !ret )
memory_error();
return ret;
}

void *check_calloc(size_t nmemb, size_t size)
{
void *ret = calloc(nmemb, size);
if( !ret )
memory_error();
return ret;
}

void *check_realloc(void *ptr, size_t size)
{
void *ret = realloc(ptr, size);
if( !ret )
memory_error();
return ret;
}
5 changes: 5 additions & 0 deletions src/msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* Shows error messages.
*/
#pragma once
#include <stddef.h>

extern const char *prog_name;

Expand All @@ -26,3 +27,7 @@ void show_opt_error(const char *format, ...)
__attribute__((noreturn, format(printf, 1, 2)));
void show_msg(const char *format, ...) __attribute__((format(printf, 1, 2)));
void show_version(void);
void memory_error(void) __attribute__((noreturn));
void *check_malloc(size_t size);
void *check_calloc(size_t nmemb, size_t size);
void *check_realloc(void *ptr, size_t size);
15 changes: 7 additions & 8 deletions src/spartafs.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,13 @@ static int compare_level(const void *a, const void *b)
struct sfs *build_spartafs(int sector_size, int num_sectors, unsigned boot_addr,
file_list *flist)
{
struct sfs *sfs = malloc(sizeof(struct sfs));

sfs->data = calloc(sector_size, num_sectors);
sfs->nsec = num_sectors;
sfs->bmap = 4;
sfs->nbmp = ((num_sectors + 8) / 8 + sector_size - 1) / sector_size;
sfs->csec = 4 + sfs->nbmp;
sfs->sec_size = sector_size;
struct sfs *sfs = check_malloc(sizeof(struct sfs));
sfs->data = check_calloc(sector_size, num_sectors);
sfs->nsec = num_sectors;
sfs->bmap = 4;
sfs->nbmp = ((num_sectors + 8) / 8 + sector_size - 1) / sector_size;
sfs->csec = 4 + sfs->nbmp;
sfs->sec_size = sector_size;

write_boot(sfs, boot_addr);

Expand Down
2 changes: 1 addition & 1 deletion src/spartafs.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
* Sparta FS
*/
#pragma once
#include <stdint.h>
#include "flist.h"
#include <stdint.h>

struct sfs;

Expand Down

0 comments on commit 2e82c66

Please sign in to comment.