Prefix safe allocation functions with 'e' instead of 's_'

This commit is contained in:
Bert Münnich 2015-10-28 22:29:01 +01:00
parent b096cbd536
commit 851e4288c1
6 changed files with 34 additions and 35 deletions

12
image.c
View File

@ -138,7 +138,7 @@ bool img_load_gif(img_t *img, const fileinfo_t *file)
if (img->multi.cap == 0) { if (img->multi.cap == 0) {
img->multi.cap = 8; img->multi.cap = 8;
img->multi.frames = (img_frame_t*) img->multi.frames = (img_frame_t*)
s_malloc(sizeof(img_frame_t) * img->multi.cap); emalloc(sizeof(img_frame_t) * img->multi.cap);
} }
img->multi.cnt = img->multi.sel = 0; img->multi.cnt = img->multi.sel = 0;
img->multi.length = 0; img->multi.length = 0;
@ -190,9 +190,9 @@ bool img_load_gif(img_t *img, const fileinfo_t *file)
w = gif->Image.Width; w = gif->Image.Width;
h = gif->Image.Height; h = gif->Image.Height;
rows = (GifRowType*) s_malloc(h * sizeof(GifRowType)); rows = (GifRowType*) emalloc(h * sizeof(GifRowType));
for (i = 0; i < h; i++) for (i = 0; i < h; i++)
rows[i] = (GifRowType) s_malloc(w * sizeof(GifPixelType)); rows[i] = (GifRowType) emalloc(w * sizeof(GifPixelType));
if (gif->Image.Interlace) { if (gif->Image.Interlace) {
for (i = 0; i < 4; i++) { for (i = 0; i < 4; i++) {
for (j = intoffset[i]; j < h; j += intjump[i]) for (j = intoffset[i]; j < h; j += intjump[i])
@ -203,7 +203,7 @@ bool img_load_gif(img_t *img, const fileinfo_t *file)
DGifGetLine(gif, rows[i], w); DGifGetLine(gif, rows[i], w);
} }
ptr = data = (DATA32*) s_malloc(sizeof(DATA32) * sw * sh); ptr = data = (DATA32*) emalloc(sizeof(DATA32) * sw * sh);
cmap = gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap; cmap = gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap;
r = cmap->Colors[bg].Red; r = cmap->Colors[bg].Red;
g = cmap->Colors[bg].Green; g = cmap->Colors[bg].Green;
@ -257,8 +257,8 @@ bool img_load_gif(img_t *img, const fileinfo_t *file)
if (img->multi.cnt == img->multi.cap) { if (img->multi.cnt == img->multi.cap) {
img->multi.cap *= 2; img->multi.cap *= 2;
img->multi.frames = (img_frame_t*) img->multi.frames = (img_frame_t*)
s_realloc(img->multi.frames, erealloc(img->multi.frames,
img->multi.cap * sizeof(img_frame_t)); img->multi.cap * sizeof(img_frame_t));
} }
img->multi.frames[img->multi.cnt].im = im; img->multi.frames[img->multi.cnt].im = im;
img->multi.frames[img->multi.cnt].delay = delay > 0 ? delay : DEF_GIF_DELAY; img->multi.frames[img->multi.cnt].delay = delay > 0 ? delay : DEF_GIF_DELAY;

10
main.c
View File

@ -122,7 +122,7 @@ void check_add_file(char *filename, bool given)
if (fileidx == filecnt) { if (fileidx == filecnt) {
filecnt *= 2; filecnt *= 2;
files = s_realloc(files, filecnt * sizeof(*files)); files = erealloc(files, filecnt * sizeof(*files));
memset(&files[filecnt/2], 0, filecnt/2 * sizeof(*files)); memset(&files[filecnt/2], 0, filecnt/2 * sizeof(*files));
} }
@ -131,7 +131,7 @@ void check_add_file(char *filename, bool given)
return; return;
} }
files[fileidx].name = s_strdup(filename); files[fileidx].name = estrdup(filename);
if ((bn = strrchr(files[fileidx].name , '/')) != NULL && bn[1] != '\0') if ((bn = strrchr(files[fileidx].name , '/')) != NULL && bn[1] != '\0')
files[fileidx].base = ++bn; files[fileidx].base = ++bn;
else else
@ -493,7 +493,7 @@ void run_key_handler(const char *key, unsigned int mask)
warn("could not open pipe for key handler"); warn("could not open pipe for key handler");
return; return;
} }
oldst = s_malloc(fcnt * sizeof(*oldst)); oldst = emalloc(fcnt * sizeof(*oldst));
strncpy(win.bar.l.buf, "Running key handler...", win.bar.l.size); strncpy(win.bar.l.buf, "Running key handler...", win.bar.l.size);
win_draw(&win); win_draw(&win);
@ -800,7 +800,7 @@ int main(int argc, char **argv)
else else
filecnt = options->filecnt; filecnt = options->filecnt;
files = s_malloc(filecnt * sizeof(*files)); files = emalloc(filecnt * sizeof(*files));
memset(files, 0, filecnt * sizeof(*files)); memset(files, 0, filecnt * sizeof(*files));
fileidx = 0; fileidx = 0;
@ -865,7 +865,7 @@ int main(int argc, char **argv)
for (i = 0; i < ARRLEN(cmd); i++) { for (i = 0; i < ARRLEN(cmd); i++) {
n = strlen(homedir) + strlen(dsuffix) + strlen(name[i]) + 12; n = strlen(homedir) + strlen(dsuffix) + strlen(name[i]) + 12;
*cmd[i] = (char*) s_malloc(n); *cmd[i] = (char*) emalloc(n);
snprintf(*cmd[i], n, "%s%s/sxiv/exec/%s", homedir, dsuffix, name[i]); snprintf(*cmd[i], n, "%s%s/sxiv/exec/%s", homedir, dsuffix, name[i]);
if (access(*cmd[i], X_OK) != 0) { if (access(*cmd[i], X_OK) != 0) {
free(*cmd[i]); free(*cmd[i]);

View File

@ -48,7 +48,7 @@ char* tns_cache_filepath(const char *filepath)
if (strncmp(filepath, cache_dir, strlen(cache_dir)) != 0) { if (strncmp(filepath, cache_dir, strlen(cache_dir)) != 0) {
/* don't cache images inside the cache directory! */ /* don't cache images inside the cache directory! */
len = strlen(cache_dir) + strlen(filepath) + 2; len = strlen(cache_dir) + strlen(filepath) + 2;
cfile = (char*) s_malloc(len); cfile = (char*) emalloc(len);
snprintf(cfile, len, "%s/%s", cache_dir, filepath + 1); snprintf(cfile, len, "%s/%s", cache_dir, filepath + 1);
} }
return cfile; return cfile;
@ -155,7 +155,7 @@ void tns_init(tns_t *tns, fileinfo_t *files, const int *cnt, int *sel,
const char *homedir, *dsuffix = ""; const char *homedir, *dsuffix = "";
if (cnt != NULL && *cnt > 0) { if (cnt != NULL && *cnt > 0) {
tns->thumbs = (thumb_t*) s_malloc(*cnt * sizeof(thumb_t)); tns->thumbs = (thumb_t*) emalloc(*cnt * sizeof(thumb_t));
memset(tns->thumbs, 0, *cnt * sizeof(thumb_t)); memset(tns->thumbs, 0, *cnt * sizeof(thumb_t));
} else { } else {
tns->thumbs = NULL; tns->thumbs = NULL;
@ -178,7 +178,7 @@ void tns_init(tns_t *tns, fileinfo_t *files, const int *cnt, int *sel,
if (homedir != NULL) { if (homedir != NULL) {
free(cache_dir); free(cache_dir);
len = strlen(homedir) + strlen(dsuffix) + 6; len = strlen(homedir) + strlen(dsuffix) + 6;
cache_dir = (char*) s_malloc(len); cache_dir = (char*) emalloc(len);
snprintf(cache_dir, len, "%s%s/sxiv", homedir, dsuffix); snprintf(cache_dir, len, "%s%s/sxiv", homedir, dsuffix);
} else { } else {
warn("could not locate thumbnail cache directory"); warn("could not locate thumbnail cache directory");

29
util.c
View File

@ -28,7 +28,7 @@
void cleanup(void); void cleanup(void);
void* s_malloc(size_t size) void* emalloc(size_t size)
{ {
void *ptr; void *ptr;
@ -38,7 +38,7 @@ void* s_malloc(size_t size)
return ptr; return ptr;
} }
void* s_realloc(void *ptr, size_t size) void* erealloc(void *ptr, size_t size)
{ {
ptr = realloc(ptr, size); ptr = realloc(ptr, size);
if (ptr == NULL) if (ptr == NULL)
@ -46,16 +46,15 @@ void* s_realloc(void *ptr, size_t size)
return ptr; return ptr;
} }
char* s_strdup(const char *s) char* estrdup(const char *s)
{ {
char *d = NULL; char *d;
size_t n = strlen(s) + 1;
if (s != NULL) { d = malloc(n);
d = malloc(strlen(s) + 1); if (d == NULL)
if (d == NULL) die("could not allocate memory");
die("could not allocate memory"); memcpy(d, s, n);
strcpy(d, s);
}
return d; return d;
} }
@ -112,7 +111,7 @@ int r_opendir(r_dir_t *rdir, const char *dirname)
} }
rdir->stcap = 512; rdir->stcap = 512;
rdir->stack = (char**) s_malloc(rdir->stcap * sizeof(char*)); rdir->stack = (char**) emalloc(rdir->stcap * sizeof(char*));
rdir->stlen = 0; rdir->stlen = 0;
rdir->name = (char*) dirname; rdir->name = (char*) dirname;
@ -158,7 +157,7 @@ char* r_readdir(r_dir_t *rdir)
continue; continue;
len = strlen(rdir->name) + strlen(dentry->d_name) + 2; len = strlen(rdir->name) + strlen(dentry->d_name) + 2;
filename = (char*) s_malloc(len); filename = (char*) emalloc(len);
snprintf(filename, len, "%s%s%s", rdir->name, snprintf(filename, len, "%s%s%s", rdir->name,
rdir->name[strlen(rdir->name)-1] == '/' ? "" : "/", rdir->name[strlen(rdir->name)-1] == '/' ? "" : "/",
dentry->d_name); dentry->d_name);
@ -169,8 +168,8 @@ char* r_readdir(r_dir_t *rdir)
/* put subdirectory on the stack */ /* put subdirectory on the stack */
if (rdir->stlen == rdir->stcap) { if (rdir->stlen == rdir->stcap) {
rdir->stcap *= 2; rdir->stcap *= 2;
rdir->stack = (char**) s_realloc(rdir->stack, rdir->stack = (char**) erealloc(rdir->stack,
rdir->stcap * sizeof(char*)); rdir->stcap * sizeof(char*));
} }
rdir->stack[rdir->stlen++] = filename; rdir->stack[rdir->stlen++] = filename;
continue; continue;
@ -207,7 +206,7 @@ int r_mkdir(const char *path)
if (stat(path, &stats) == 0) if (stat(path, &stats) == 0)
return S_ISDIR(stats.st_mode) ? 0 : -1; return S_ISDIR(stats.st_mode) ? 0 : -1;
d = dir = (char*) s_malloc(strlen(path) + 1); d = dir = (char*) emalloc(strlen(path) + 1);
strcpy(dir, path); strcpy(dir, path);
while (d != NULL && err == 0) { while (d != NULL && err == 0) {

6
util.h
View File

@ -61,9 +61,9 @@ typedef struct {
int stlen; int stlen;
} r_dir_t; } r_dir_t;
void* s_malloc(size_t); void* emalloc(size_t);
void* s_realloc(void*, size_t); void* erealloc(void*, size_t);
char* s_strdup(const char*); char* estrdup(const char*);
void warn(const char*, ...); void warn(const char*, ...);
void die(const char*, ...); void die(const char*, ...);

View File

@ -165,8 +165,8 @@ void win_init(win_t *win)
win->bar.l.size = BAR_L_LEN; win->bar.l.size = BAR_L_LEN;
win->bar.r.size = BAR_R_LEN; win->bar.r.size = BAR_R_LEN;
win->bar.l.buf = s_malloc(win->bar.l.size); win->bar.l.buf = emalloc(win->bar.l.size);
win->bar.r.buf = s_malloc(win->bar.r.size); win->bar.r.buf = emalloc(win->bar.r.size);
win->bar.h = options->hide_bar ? 0 : barheight; win->bar.h = options->hide_bar ? 0 : barheight;
INIT_ATOM_(WM_DELETE_WINDOW); INIT_ATOM_(WM_DELETE_WINDOW);
@ -257,7 +257,7 @@ void win_open(win_t *win)
gc = XCreateGC(e->dpy, win->xwin, 0, None); gc = XCreateGC(e->dpy, win->xwin, 0, None);
n = icons[ARRLEN(icons)-1].size; n = icons[ARRLEN(icons)-1].size;
icon_data = s_malloc((n * n + 2) * sizeof(*icon_data)); icon_data = emalloc((n * n + 2) * sizeof(*icon_data));
for (i = 0; i < ARRLEN(icons); i++) { for (i = 0; i < ARRLEN(icons); i++) {
n = 0; n = 0;