nsxiv851e428
Prefix safe allocation functions with 'e' instead of 's_'
+34 −35
6 files changed
| @@ -138,7 +138,7 @@ bool img_load_gif(img_t *img, const fileinfo_t *file) | |||
| 138 | 138 | if (img->multi.cap == 0) { | |
| 139 | 139 | img->multi.cap = 8; | |
| 140 | 140 | img->multi.frames = (img_frame_t*) | |
| 141 | − | s_malloc(sizeof(img_frame_t) * img->multi.cap); | |
| 141 | + | emalloc(sizeof(img_frame_t) * img->multi.cap); | |
| 142 | 142 | } | |
| 143 | 143 | img->multi.cnt = img->multi.sel = 0; | |
| 144 | 144 | img->multi.length = 0; | |
| @@ -190,9 +190,9 @@ bool img_load_gif(img_t *img, const fileinfo_t *file) | |||
| 190 | 190 | w = gif->Image.Width; | |
| 191 | 191 | h = gif->Image.Height; | |
| 192 | 192 | ||
| 193 | − | rows = (GifRowType*) s_malloc(h * sizeof(GifRowType)); | |
| 193 | + | rows = (GifRowType*) emalloc(h * sizeof(GifRowType)); | |
| 194 | 194 | for (i = 0; i < h; i++) | |
| 195 | − | rows[i] = (GifRowType) s_malloc(w * sizeof(GifPixelType)); | |
| 195 | + | rows[i] = (GifRowType) emalloc(w * sizeof(GifPixelType)); | |
| 196 | 196 | if (gif->Image.Interlace) { | |
| 197 | 197 | for (i = 0; i < 4; i++) { | |
| 198 | 198 | for (j = intoffset[i]; j < h; j += intjump[i]) | |
| @@ -203,7 +203,7 @@ bool img_load_gif(img_t *img, const fileinfo_t *file) | |||
| 203 | 203 | DGifGetLine(gif, rows[i], w); | |
| 204 | 204 | } | |
| 205 | 205 | ||
| 206 | − | ptr = data = (DATA32*) s_malloc(sizeof(DATA32) * sw * sh); | |
| 206 | + | ptr = data = (DATA32*) emalloc(sizeof(DATA32) * sw * sh); | |
| 207 | 207 | cmap = gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap; | |
| 208 | 208 | r = cmap->Colors[bg].Red; | |
| 209 | 209 | g = cmap->Colors[bg].Green; | |
| @@ -257,8 +257,8 @@ bool img_load_gif(img_t *img, const fileinfo_t *file) | |||
| 257 | 257 | if (img->multi.cnt == img->multi.cap) { | |
| 258 | 258 | img->multi.cap *= 2; | |
| 259 | 259 | img->multi.frames = (img_frame_t*) | |
| 260 | − | s_realloc(img->multi.frames, | |
| 261 | − | img->multi.cap * sizeof(img_frame_t)); | |
| 260 | + | erealloc(img->multi.frames, | |
| 261 | + | img->multi.cap * sizeof(img_frame_t)); | |
| 262 | 262 | } | |
| 263 | 263 | img->multi.frames[img->multi.cnt].im = im; | |
| 264 | 264 | img->multi.frames[img->multi.cnt].delay = delay > 0 ? delay : DEF_GIF_DELAY; | |
| @@ -122,7 +122,7 @@ void check_add_file(char *filename, bool given) | |||
| 122 | 122 | ||
| 123 | 123 | if (fileidx == filecnt) { | |
| 124 | 124 | filecnt *= 2; | |
| 125 | − | files = s_realloc(files, filecnt * sizeof(*files)); | |
| 125 | + | files = erealloc(files, filecnt * sizeof(*files)); | |
| 126 | 126 | memset(&files[filecnt/2], 0, filecnt/2 * sizeof(*files)); | |
| 127 | 127 | } | |
| 128 | 128 | ||
| @@ -131,7 +131,7 @@ void check_add_file(char *filename, bool given) | |||
| 131 | 131 | return; | |
| 132 | 132 | } | |
| 133 | 133 | ||
| 134 | − | files[fileidx].name = s_strdup(filename); | |
| 134 | + | files[fileidx].name = estrdup(filename); | |
| 135 | 135 | if ((bn = strrchr(files[fileidx].name , '/')) != NULL && bn[1] != '\0') | |
| 136 | 136 | files[fileidx].base = ++bn; | |
| 137 | 137 | else | |
| @@ -493,7 +493,7 @@ void run_key_handler(const char *key, unsigned int mask) | |||
| 493 | 493 | warn("could not open pipe for key handler"); | |
| 494 | 494 | return; | |
| 495 | 495 | } | |
| 496 | − | oldst = s_malloc(fcnt * sizeof(*oldst)); | |
| 496 | + | oldst = emalloc(fcnt * sizeof(*oldst)); | |
| 497 | 497 | ||
| 498 | 498 | strncpy(win.bar.l.buf, "Running key handler...", win.bar.l.size); | |
| 499 | 499 | win_draw(&win); | |
| @@ -800,7 +800,7 @@ int main(int argc, char **argv) | |||
| 800 | 800 | else | |
| 801 | 801 | filecnt = options->filecnt; | |
| 802 | 802 | ||
| 803 | − | files = s_malloc(filecnt * sizeof(*files)); | |
| 803 | + | files = emalloc(filecnt * sizeof(*files)); | |
| 804 | 804 | memset(files, 0, filecnt * sizeof(*files)); | |
| 805 | 805 | fileidx = 0; | |
| 806 | 806 | ||
| @@ -865,7 +865,7 @@ int main(int argc, char **argv) | |||
| 865 | 865 | ||
| 866 | 866 | for (i = 0; i < ARRLEN(cmd); i++) { | |
| 867 | 867 | n = strlen(homedir) + strlen(dsuffix) + strlen(name[i]) + 12; | |
| 868 | − | *cmd[i] = (char*) s_malloc(n); | |
| 868 | + | *cmd[i] = (char*) emalloc(n); | |
| 869 | 869 | snprintf(*cmd[i], n, "%s%s/sxiv/exec/%s", homedir, dsuffix, name[i]); | |
| 870 | 870 | if (access(*cmd[i], X_OK) != 0) { | |
| 871 | 871 | free(*cmd[i]); | |
| @@ -48,7 +48,7 @@ char* tns_cache_filepath(const char *filepath) | |||
| 48 | 48 | if (strncmp(filepath, cache_dir, strlen(cache_dir)) != 0) { | |
| 49 | 49 | /* don't cache images inside the cache directory! */ | |
| 50 | 50 | len = strlen(cache_dir) + strlen(filepath) + 2; | |
| 51 | − | cfile = (char*) s_malloc(len); | |
| 51 | + | cfile = (char*) emalloc(len); | |
| 52 | 52 | snprintf(cfile, len, "%s/%s", cache_dir, filepath + 1); | |
| 53 | 53 | } | |
| 54 | 54 | return cfile; | |
| @@ -155,7 +155,7 @@ void tns_init(tns_t *tns, fileinfo_t *files, const int *cnt, int *sel, | |||
| 155 | 155 | const char *homedir, *dsuffix = ""; | |
| 156 | 156 | ||
| 157 | 157 | if (cnt != NULL && *cnt > 0) { | |
| 158 | − | tns->thumbs = (thumb_t*) s_malloc(*cnt * sizeof(thumb_t)); | |
| 158 | + | tns->thumbs = (thumb_t*) emalloc(*cnt * sizeof(thumb_t)); | |
| 159 | 159 | memset(tns->thumbs, 0, *cnt * sizeof(thumb_t)); | |
| 160 | 160 | } else { | |
| 161 | 161 | tns->thumbs = NULL; | |
| @@ -178,7 +178,7 @@ void tns_init(tns_t *tns, fileinfo_t *files, const int *cnt, int *sel, | |||
| 178 | 178 | if (homedir != NULL) { | |
| 179 | 179 | free(cache_dir); | |
| 180 | 180 | len = strlen(homedir) + strlen(dsuffix) + 6; | |
| 181 | − | cache_dir = (char*) s_malloc(len); | |
| 181 | + | cache_dir = (char*) emalloc(len); | |
| 182 | 182 | snprintf(cache_dir, len, "%s%s/sxiv", homedir, dsuffix); | |
| 183 | 183 | } else { | |
| 184 | 184 | warn("could not locate thumbnail cache directory"); | |
| @@ -28,7 +28,7 @@ | |||
| 28 | 28 | ||
| 29 | 29 | void cleanup(void); | |
| 30 | 30 | ||
| 31 | − | void* s_malloc(size_t size) | |
| 31 | + | void* emalloc(size_t size) | |
| 32 | 32 | { | |
| 33 | 33 | void *ptr; | |
| 34 | 34 | ||
| @@ -38,7 +38,7 @@ void* s_malloc(size_t size) | |||
| 38 | 38 | return ptr; | |
| 39 | 39 | } | |
| 40 | 40 | ||
| 41 | − | void* s_realloc(void *ptr, size_t size) | |
| 41 | + | void* erealloc(void *ptr, size_t size) | |
| 42 | 42 | { | |
| 43 | 43 | ptr = realloc(ptr, size); | |
| 44 | 44 | if (ptr == NULL) | |
| @@ -46,16 +46,15 @@ void* s_realloc(void *ptr, size_t size) | |||
| 46 | 46 | return ptr; | |
| 47 | 47 | } | |
| 48 | 48 | ||
| 49 | − | char* s_strdup(const char *s) | |
| 49 | + | char* estrdup(const char *s) | |
| 50 | 50 | { | |
| 51 | − | char *d = NULL; | |
| 51 | + | char *d; | |
| 52 | + | size_t n = strlen(s) + 1; | |
| 52 | 53 | ||
| 53 | − | if (s != NULL) { | |
| 54 | − | d = malloc(strlen(s) + 1); | |
| 55 | − | if (d == NULL) | |
| 56 | − | die("could not allocate memory"); | |
| 57 | − | strcpy(d, s); | |
| 58 | − | } | |
| 54 | + | d = malloc(n); | |
| 55 | + | if (d == NULL) | |
| 56 | + | die("could not allocate memory"); | |
| 57 | + | memcpy(d, s, n); | |
| 59 | 58 | return d; | |
| 60 | 59 | } | |
| 61 | 60 | ||
| @@ -112,7 +111,7 @@ int r_opendir(r_dir_t *rdir, const char *dirname) | |||
| 112 | 111 | } | |
| 113 | 112 | ||
| 114 | 113 | rdir->stcap = 512; | |
| 115 | − | rdir->stack = (char**) s_malloc(rdir->stcap * sizeof(char*)); | |
| 114 | + | rdir->stack = (char**) emalloc(rdir->stcap * sizeof(char*)); | |
| 116 | 115 | rdir->stlen = 0; | |
| 117 | 116 | ||
| 118 | 117 | rdir->name = (char*) dirname; | |
| @@ -158,7 +157,7 @@ char* r_readdir(r_dir_t *rdir) | |||
| 158 | 157 | continue; | |
| 159 | 158 | ||
| 160 | 159 | len = strlen(rdir->name) + strlen(dentry->d_name) + 2; | |
| 161 | − | filename = (char*) s_malloc(len); | |
| 160 | + | filename = (char*) emalloc(len); | |
| 162 | 161 | snprintf(filename, len, "%s%s%s", rdir->name, | |
| 163 | 162 | rdir->name[strlen(rdir->name)-1] == '/' ? "" : "/", | |
| 164 | 163 | dentry->d_name); | |
| @@ -169,8 +168,8 @@ char* r_readdir(r_dir_t *rdir) | |||
| 169 | 168 | /* put subdirectory on the stack */ | |
| 170 | 169 | if (rdir->stlen == rdir->stcap) { | |
| 171 | 170 | rdir->stcap *= 2; | |
| 172 | − | rdir->stack = (char**) s_realloc(rdir->stack, | |
| 173 | − | rdir->stcap * sizeof(char*)); | |
| 171 | + | rdir->stack = (char**) erealloc(rdir->stack, | |
| 172 | + | rdir->stcap * sizeof(char*)); | |
| 174 | 173 | } | |
| 175 | 174 | rdir->stack[rdir->stlen++] = filename; | |
| 176 | 175 | continue; | |
| @@ -207,7 +206,7 @@ int r_mkdir(const char *path) | |||
| 207 | 206 | if (stat(path, &stats) == 0) | |
| 208 | 207 | return S_ISDIR(stats.st_mode) ? 0 : -1; | |
| 209 | 208 | ||
| 210 | − | d = dir = (char*) s_malloc(strlen(path) + 1); | |
| 209 | + | d = dir = (char*) emalloc(strlen(path) + 1); | |
| 211 | 210 | strcpy(dir, path); | |
| 212 | 211 | ||
| 213 | 212 | while (d != NULL && err == 0) { | |
| @@ -61,9 +61,9 @@ typedef struct { | |||
| 61 | 61 | int stlen; | |
| 62 | 62 | } r_dir_t; | |
| 63 | 63 | ||
| 64 | − | void* s_malloc(size_t); | |
| 65 | − | void* s_realloc(void*, size_t); | |
| 66 | − | char* s_strdup(const char*); | |
| 64 | + | void* emalloc(size_t); | |
| 65 | + | void* erealloc(void*, size_t); | |
| 66 | + | char* estrdup(const char*); | |
| 67 | 67 | ||
| 68 | 68 | void warn(const char*, ...); | |
| 69 | 69 | void die(const char*, ...); | |
| @@ -165,8 +165,8 @@ void win_init(win_t *win) | |||
| 165 | 165 | ||
| 166 | 166 | win->bar.l.size = BAR_L_LEN; | |
| 167 | 167 | win->bar.r.size = BAR_R_LEN; | |
| 168 | − | win->bar.l.buf = s_malloc(win->bar.l.size); | |
| 169 | − | win->bar.r.buf = s_malloc(win->bar.r.size); | |
| 168 | + | win->bar.l.buf = emalloc(win->bar.l.size); | |
| 169 | + | win->bar.r.buf = emalloc(win->bar.r.size); | |
| 170 | 170 | win->bar.h = options->hide_bar ? 0 : barheight; | |
| 171 | 171 | ||
| 172 | 172 | INIT_ATOM_(WM_DELETE_WINDOW); | |
| @@ -257,7 +257,7 @@ void win_open(win_t *win) | |||
| 257 | 257 | gc = XCreateGC(e->dpy, win->xwin, 0, None); | |
| 258 | 258 | ||
| 259 | 259 | n = icons[ARRLEN(icons)-1].size; | |
| 260 | − | icon_data = s_malloc((n * n + 2) * sizeof(*icon_data)); | |
| 260 | + | icon_data = emalloc((n * n + 2) * sizeof(*icon_data)); | |
| 261 | 261 | ||
| 262 | 262 | for (i = 0; i < ARRLEN(icons); i++) { | |
| 263 | 263 | n = 0; | |