summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWerner Almesberger <werner@almesberger.net>2016-08-30 06:49:49 -0300
committerWerner Almesberger <werner@almesberger.net>2016-08-30 06:49:49 -0300
commit9a0fe02922549d804ba0e4a9c8535ce25f7930d9 (patch)
tree44caf72d27bd070597a7a846fbb9f44f40a92568
parentfe2d12a590aa637881c7f3fc53d3cca9e246c195 (diff)
downloadeeshow-9a0fe02922549d804ba0e4a9c8535ce25f7930d9.tar.gz
eeshow-9a0fe02922549d804ba0e4a9c8535ce25f7930d9.tar.bz2
eeshow-9a0fe02922549d804ba0e4a9c8535ce25f7930d9.zip
misc/util.h (alloc_printf): error-checking vasprintf wrapper; use it
-rw-r--r--Makefile2
-rw-r--r--gui/fmt-pango.c7
-rw-r--r--gui/gui.c5
-rw-r--r--gui/timer.c6
-rw-r--r--kicad/dwg.c3
-rw-r--r--kicad/pl-render.c5
-rw-r--r--kicad/sch-render.c6
-rw-r--r--misc/util.c34
-rw-r--r--misc/util.h4
9 files changed, 52 insertions, 20 deletions
diff --git a/Makefile b/Makefile
index cd58c0a..aa24beb 100644
--- a/Makefile
+++ b/Makefile
@@ -23,7 +23,7 @@ OBJS = main.o version.o \
file/file.o file/git-util.o file/git-file.o file/git-hist.o \
gfx/style.o gfx/fig.o gfx/record.o gfx/cro.o gfx/diff.o gfx/gfx.o \
gfx/text.o gfx/misc.o gfx/pdftoc.o \
- misc/diag.o
+ misc/diag.o misc/util.o
ICONS = delta diff
diff --git a/gui/fmt-pango.c b/gui/fmt-pango.c
index 8e975dc..48b8516 100644
--- a/gui/fmt-pango.c
+++ b/gui/fmt-pango.c
@@ -11,7 +11,6 @@
*/
-#define _GNU_SOURCE /* for asprintf */
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
@@ -48,7 +47,7 @@ unsigned vsfmt_pango(char *buf, const char *fmt, va_list ap)
switch (*q) {
case 's':
s = va_arg(ap, const char *);
- len = asprintf(&tmp, tmp_fmt, s);
+ len = alloc_printf(&tmp, tmp_fmt, s);
extra = 0;
for (t = tmp; *t; t++)
@@ -97,13 +96,13 @@ unsigned vsfmt_pango(char *buf, const char *fmt, va_list ap)
/* @@@ we don't filter markup meta-characters */
case 'd':
case 'x':
- len = asprintf(&tmp, tmp_fmt, va_arg(ap, int));
+ len = alloc_printf(&tmp, tmp_fmt, va_arg(ap, int));
if (buf)
memcpy(res, tmp, len);
res += len;
break;
case 'u':
- len = asprintf(&tmp, tmp_fmt, va_arg(ap, unsigned));
+ len = alloc_printf(&tmp, tmp_fmt, va_arg(ap, unsigned));
if (buf)
memcpy(res, tmp, len);
res += len;
diff --git a/gui/gui.c b/gui/gui.c
index c566d1d..5c96c6c 100644
--- a/gui/gui.c
+++ b/gui/gui.c
@@ -17,7 +17,6 @@
* https://developer.gnome.org/gtk3/stable/gtk-migrating-2-to-3.html
*/
-#define _GNU_SOURCE /* for asprintf */
#include <stddef.h>
#include <stdbool.h>
#include <stdlib.h>
@@ -478,7 +477,7 @@ int run_gui(const struct file_names *fn, bool recurse, int limit,
gtk_container_add(GTK_CONTAINER(window), gui.da);
gtk_window_set_default_size(GTK_WINDOW(window), 640, 480);
- if (asprintf(&title, "eeshow (rev %s)", version)) {};
+ alloc_printf(&title, "eeshow (rev %s)", version);
gtk_window_set_title(GTK_WINDOW(window), title);
gtk_widget_set_events(gui.da,
@@ -517,7 +516,7 @@ int run_gui(const struct file_names *fn, bool recurse, int limit,
gdk_x11_window_get_xid(gtk_widget_get_window(window));
char *s;
- if (asprintf(&s, "0x%lx", xid)) {}
+ alloc_printf(&s, "0x%lx", xid);
setenv("EESHOW_WINDOW_ID", s, 1);
while (n_commands--) {
diff --git a/gui/timer.c b/gui/timer.c
index 6d6cdd6..37304d6 100644
--- a/gui/timer.c
+++ b/gui/timer.c
@@ -10,7 +10,6 @@
* (at your option) any later version.
*/
-#define _GNU_SOURCE /* for asprintf */
#include <stdbool.h>
#include <stdio.h>
#include <sys/time.h>
@@ -64,9 +63,8 @@ void timer_show(cairo_t *cr)
now.tv_usec += 1000000;
}
-
- if (asprintf(&s , "%u.%03d",
- (unsigned) now.tv_sec, (int) now.tv_usec / 1000)) {}
+ alloc_printf(&s , "%u.%03d",
+ (unsigned) now.tv_sec, (int) now.tv_usec / 1000);
timer_show_cairo(cr, s);
}
diff --git a/kicad/dwg.c b/kicad/dwg.c
index 2779867..fc0efa2 100644
--- a/kicad/dwg.c
+++ b/kicad/dwg.c
@@ -11,7 +11,6 @@
*/
-#define _GNU_SOURCE /* for asprintf */
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
@@ -267,7 +266,7 @@ void dwg_glabel(struct gfx *gfx, int x, int y, const char *s, int dir, int dim,
if (bbox)
bbox_from_poly(bbox, n, vx, vy);
- if (asprintf(&tag, "G:%s", s)) {}
+ alloc_printf(&tag, "G:%s", s);
gfx_tag(gfx, tag, n, vx, vy);
}
diff --git a/kicad/pl-render.c b/kicad/pl-render.c
index 914eee7..c56519c 100644
--- a/kicad/pl-render.c
+++ b/kicad/pl-render.c
@@ -10,7 +10,6 @@
* (at your option) any later version.
*/
-#define _GNU_SOURCE /* for asprintf */
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
@@ -88,7 +87,7 @@ static char *expand(const struct pl_ctx *pl, const char *s,
n = 0;
for (sch = sheets; sch; sch = sch->next)
n++;
- if (asprintf(&x, "%u", n)) {}
+ alloc_printf(&x, "%u", n);
break;
case 'P':
x = "%P"; // sheet path
@@ -101,7 +100,7 @@ static char *expand(const struct pl_ctx *pl, const char *s,
for (sch = sheets; sch != sheet;
sch = sch->next)
n++;
- if (asprintf(&x, "%u", n)) {}
+ alloc_printf(&x, "%u", n);
break;
case 'T':
x = (char *) sheet->title;
diff --git a/kicad/sch-render.c b/kicad/sch-render.c
index 5d3c9e0..52be40f 100644
--- a/kicad/sch-render.c
+++ b/kicad/sch-render.c
@@ -11,10 +11,10 @@
*/
-#define _GNU_SOURCE /* for asprintf */
#include <stdio.h>
#include <assert.h>
+#include "misc/util.h"
#include "misc/diag.h"
#include "gfx/misc.h"
#include "gfx/style.h"
@@ -82,7 +82,7 @@ static void do_hsheet_text(const struct sch_obj *obj,
.vert = text_min,
.style = text_normal,
};
- if (asprintf(&s, "Sheet: %s", sheet->name)) {}
+ alloc_printf(&s, "Sheet: %s", sheet->name);
sheet_txt.s = s; /* work around "const" mismatch */
struct text file_txt = {
@@ -94,7 +94,7 @@ static void do_hsheet_text(const struct sch_obj *obj,
.vert = text_max,
.style = text_normal,
};
- if (asprintf(&s, "File: %s", sheet->file)) {}
+ alloc_printf(&s, "File: %s", sheet->file);
file_txt.s = s; /* work around "const" mismatch */
if (sheet->rotated) {
diff --git a/misc/util.c b/misc/util.c
new file mode 100644
index 0000000..0ca5e6b
--- /dev/null
+++ b/misc/util.c
@@ -0,0 +1,34 @@
+/*
+ * misc/util.c - Common utility functions
+ *
+ * Written 2016 by Werner Almesberger
+ * Copyright 2016 Werner Almesberger
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#define _GNU_SOURCE /* for vasprintf */
+#include <stdarg.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "misc/util.h"
+
+
+int alloc_printf(char **s, const char *fmt, ...)
+{
+ va_list ap;
+ int res;
+
+ va_start(ap, fmt);
+ res = vasprintf(s, fmt, ap);
+ va_end(ap);
+ if (res == -1) {
+ perror("vasprintf");
+ exit(1);
+ }
+ return res;
+}
diff --git a/misc/util.h b/misc/util.h
index b71a8ea..5c195f1 100644
--- a/misc/util.h
+++ b/misc/util.h
@@ -70,4 +70,8 @@ static inline bool strbegins(const char *s, const char *prefix)
return !strncmp(s, prefix, strlen(prefix));
}
+
+int alloc_printf(char **s, const char *fmt, ...)
+ __attribute__((format(printf, 2, 3)));
+
#endif /* !MISC_UTIL_H */