Krolyxon
Commit 851e428

Branches

Refs →

nsxiv851e428

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

Author
Bert Münnich
Date
2015-10-28 22:29 +0100
Parent
b096cbd
Commit
851e4288c102cc4177d54d87aded43d003e85885

+34 −35

6 files changed

  1. M image.c +6 −6
  2. M main.c +5 −5
  3. M thumbs.c +3 −3
  4. M util.c +14 −15
  5. M util.h +3 −3
  6. M window.c +3 −3
M image.c View
@@ -138,7 +138,7 @@ bool img_load_gif(img_t *img, const fileinfo_t *file)
138138 if (img->multi.cap == 0) {
139139 img->multi.cap = 8;
140140 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);
142142 }
143143 img->multi.cnt = img->multi.sel = 0;
144144 img->multi.length = 0;
@@ -190,9 +190,9 @@ bool img_load_gif(img_t *img, const fileinfo_t *file)
190190 w = gif->Image.Width;
191191 h = gif->Image.Height;
192192
193− rows = (GifRowType*) s_malloc(h * sizeof(GifRowType));
193+ rows = (GifRowType*) emalloc(h * sizeof(GifRowType));
194194 for (i = 0; i < h; i++)
195− rows[i] = (GifRowType) s_malloc(w * sizeof(GifPixelType));
195+ rows[i] = (GifRowType) emalloc(w * sizeof(GifPixelType));
196196 if (gif->Image.Interlace) {
197197 for (i = 0; i < 4; i++) {
198198 for (j = intoffset[i]; j < h; j += intjump[i])
@@ -203,7 +203,7 @@ bool img_load_gif(img_t *img, const fileinfo_t *file)
203203 DGifGetLine(gif, rows[i], w);
204204 }
205205
206− ptr = data = (DATA32*) s_malloc(sizeof(DATA32) * sw * sh);
206+ ptr = data = (DATA32*) emalloc(sizeof(DATA32) * sw * sh);
207207 cmap = gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap;
208208 r = cmap->Colors[bg].Red;
209209 g = cmap->Colors[bg].Green;
@@ -257,8 +257,8 @@ bool img_load_gif(img_t *img, const fileinfo_t *file)
257257 if (img->multi.cnt == img->multi.cap) {
258258 img->multi.cap *= 2;
259259 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));
262262 }
263263 img->multi.frames[img->multi.cnt].im = im;
264264 img->multi.frames[img->multi.cnt].delay = delay > 0 ? delay : DEF_GIF_DELAY;
M main.c View
@@ -122,7 +122,7 @@ void check_add_file(char *filename, bool given)
122122
123123 if (fileidx == filecnt) {
124124 filecnt *= 2;
125− files = s_realloc(files, filecnt * sizeof(*files));
125+ files = erealloc(files, filecnt * sizeof(*files));
126126 memset(&files[filecnt/2], 0, filecnt/2 * sizeof(*files));
127127 }
128128
@@ -131,7 +131,7 @@ void check_add_file(char *filename, bool given)
131131 return;
132132 }
133133
134− files[fileidx].name = s_strdup(filename);
134+ files[fileidx].name = estrdup(filename);
135135 if ((bn = strrchr(files[fileidx].name , '/')) != NULL && bn[1] != '\0')
136136 files[fileidx].base = ++bn;
137137 else
@@ -493,7 +493,7 @@ void run_key_handler(const char *key, unsigned int mask)
493493 warn("could not open pipe for key handler");
494494 return;
495495 }
496− oldst = s_malloc(fcnt * sizeof(*oldst));
496+ oldst = emalloc(fcnt * sizeof(*oldst));
497497
498498 strncpy(win.bar.l.buf, "Running key handler...", win.bar.l.size);
499499 win_draw(&win);
@@ -800,7 +800,7 @@ int main(int argc, char **argv)
800800 else
801801 filecnt = options->filecnt;
802802
803− files = s_malloc(filecnt * sizeof(*files));
803+ files = emalloc(filecnt * sizeof(*files));
804804 memset(files, 0, filecnt * sizeof(*files));
805805 fileidx = 0;
806806
@@ -865,7 +865,7 @@ int main(int argc, char **argv)
865865
866866 for (i = 0; i < ARRLEN(cmd); i++) {
867867 n = strlen(homedir) + strlen(dsuffix) + strlen(name[i]) + 12;
868− *cmd[i] = (char*) s_malloc(n);
868+ *cmd[i] = (char*) emalloc(n);
869869 snprintf(*cmd[i], n, "%s%s/sxiv/exec/%s", homedir, dsuffix, name[i]);
870870 if (access(*cmd[i], X_OK) != 0) {
871871 free(*cmd[i]);
M thumbs.c View
@@ -48,7 +48,7 @@ char* tns_cache_filepath(const char *filepath)
4848 if (strncmp(filepath, cache_dir, strlen(cache_dir)) != 0) {
4949 /* don't cache images inside the cache directory! */
5050 len = strlen(cache_dir) + strlen(filepath) + 2;
51− cfile = (char*) s_malloc(len);
51+ cfile = (char*) emalloc(len);
5252 snprintf(cfile, len, "%s/%s", cache_dir, filepath + 1);
5353 }
5454 return cfile;
@@ -155,7 +155,7 @@ void tns_init(tns_t *tns, fileinfo_t *files, const int *cnt, int *sel,
155155 const char *homedir, *dsuffix = "";
156156
157157 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));
159159 memset(tns->thumbs, 0, *cnt * sizeof(thumb_t));
160160 } else {
161161 tns->thumbs = NULL;
@@ -178,7 +178,7 @@ void tns_init(tns_t *tns, fileinfo_t *files, const int *cnt, int *sel,
178178 if (homedir != NULL) {
179179 free(cache_dir);
180180 len = strlen(homedir) + strlen(dsuffix) + 6;
181− cache_dir = (char*) s_malloc(len);
181+ cache_dir = (char*) emalloc(len);
182182 snprintf(cache_dir, len, "%s%s/sxiv", homedir, dsuffix);
183183 } else {
184184 warn("could not locate thumbnail cache directory");
M util.c View
@@ -28,7 +28,7 @@
2828
2929 void cleanup(void);
3030
31−void* s_malloc(size_t size)
31+void* emalloc(size_t size)
3232 {
3333 void *ptr;
3434
@@ -38,7 +38,7 @@ void* s_malloc(size_t size)
3838 return ptr;
3939 }
4040
41−void* s_realloc(void *ptr, size_t size)
41+void* erealloc(void *ptr, size_t size)
4242 {
4343 ptr = realloc(ptr, size);
4444 if (ptr == NULL)
@@ -46,16 +46,15 @@ void* s_realloc(void *ptr, size_t size)
4646 return ptr;
4747 }
4848
49−char* s_strdup(const char *s)
49+char* estrdup(const char *s)
5050 {
51− char *d = NULL;
51+ char *d;
52+ size_t n = strlen(s) + 1;
5253
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);
5958 return d;
6059 }
6160
@@ -112,7 +111,7 @@ int r_opendir(r_dir_t *rdir, const char *dirname)
112111 }
113112
114113 rdir->stcap = 512;
115− rdir->stack = (char**) s_malloc(rdir->stcap * sizeof(char*));
114+ rdir->stack = (char**) emalloc(rdir->stcap * sizeof(char*));
116115 rdir->stlen = 0;
117116
118117 rdir->name = (char*) dirname;
@@ -158,7 +157,7 @@ char* r_readdir(r_dir_t *rdir)
158157 continue;
159158
160159 len = strlen(rdir->name) + strlen(dentry->d_name) + 2;
161− filename = (char*) s_malloc(len);
160+ filename = (char*) emalloc(len);
162161 snprintf(filename, len, "%s%s%s", rdir->name,
163162 rdir->name[strlen(rdir->name)-1] == '/' ? "" : "/",
164163 dentry->d_name);
@@ -169,8 +168,8 @@ char* r_readdir(r_dir_t *rdir)
169168 /* put subdirectory on the stack */
170169 if (rdir->stlen == rdir->stcap) {
171170 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*));
174173 }
175174 rdir->stack[rdir->stlen++] = filename;
176175 continue;
@@ -207,7 +206,7 @@ int r_mkdir(const char *path)
207206 if (stat(path, &stats) == 0)
208207 return S_ISDIR(stats.st_mode) ? 0 : -1;
209208
210− d = dir = (char*) s_malloc(strlen(path) + 1);
209+ d = dir = (char*) emalloc(strlen(path) + 1);
211210 strcpy(dir, path);
212211
213212 while (d != NULL && err == 0) {
M util.h View
@@ -61,9 +61,9 @@ typedef struct {
6161 int stlen;
6262 } r_dir_t;
6363
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*);
6767
6868 void warn(const char*, ...);
6969 void die(const char*, ...);
M window.c View
@@ -165,8 +165,8 @@ void win_init(win_t *win)
165165
166166 win->bar.l.size = BAR_L_LEN;
167167 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);
170170 win->bar.h = options->hide_bar ? 0 : barheight;
171171
172172 INIT_ATOM_(WM_DELETE_WINDOW);
@@ -257,7 +257,7 @@ void win_open(win_t *win)
257257 gc = XCreateGC(e->dpy, win->xwin, 0, None);
258258
259259 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));
261261
262262 for (i = 0; i < ARRLEN(icons); i++) {
263263 n = 0;