<div dir="ltr">I'm pretty certain the only two things that can happen is that str is unchanged or it is set to NULL. So presetting it to NULL would work.<br><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, May 28, 2015 at 7:01 PM, Jon A. Cruz <span dir="ltr"><<a href="mailto:jonc@osg.samsung.com" target="_blank">jonc@osg.samsung.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">If asprintf fails for any reason, the contents of the pointer<br>
are undefined. While some platforms set it to NULL, there is no<br>
guarantee that all will.<br>
<br>
</span>This change adds a simple wrapper to ensure proper NULL results<br>
on failure.<br>
<span class=""><br>
Signed-off-by: Jon A. Cruz <<a href="mailto:jonc@osg.samsung.com">jonc@osg.samsung.com</a>><br>
---<br>
</span> src/evdev-middle-button.c | 2 ++<br>
src/evdev-mt-touchpad-buttons.c | 2 ++<br>
src/evdev-mt-touchpad-edge-scroll.c | 2 ++<br>
src/libinput-util.h | 30 ++++++++++++++++++++++++++++++<br>
src/timer.c | 2 ++<br>
test/litest.c | 2 +-<br>
tools/libinput-list-devices.c | 14 +++++++-------<br>
7 files changed, 46 insertions(+), 8 deletions(-)<br>
<br>
diff --git a/src/evdev-middle-button.c b/src/evdev-middle-button.c<br>
index 903fa4d..c989486 100644<br>
--- a/src/evdev-middle-button.c<br>
+++ b/src/evdev-middle-button.c<br>
@@ -20,6 +20,8 @@<br>
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.<br>
*/<br>
<br>
+#include "config.h"<br>
+<br>
#include <stdint.h><br>
<br>
#include "evdev.h"<br>
diff --git a/src/evdev-mt-touchpad-buttons.c b/src/evdev-mt-touchpad-buttons.c<br>
index 64e9d28..4b1d6ab 100644<br>
--- a/src/evdev-mt-touchpad-buttons.c<br>
+++ b/src/evdev-mt-touchpad-buttons.c<br>
@@ -20,6 +20,8 @@<br>
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.<br>
*/<br>
<br>
+#include "config.h"<br>
+<br>
#include <errno.h><br>
#include <limits.h><br>
#include <math.h><br>
diff --git a/src/evdev-mt-touchpad-edge-scroll.c b/src/evdev-mt-touchpad-edge-scroll.c<br>
index 585c764..2302d2c 100644<br>
--- a/src/evdev-mt-touchpad-edge-scroll.c<br>
+++ b/src/evdev-mt-touchpad-edge-scroll.c<br>
@@ -20,6 +20,8 @@<br>
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.<br>
*/<br>
<br>
+#include "config.h"<br>
+<br>
#include <errno.h><br>
#include <limits.h><br>
#include <math.h><br>
diff --git a/src/libinput-util.h b/src/libinput-util.h<br>
index 732f813..7d5651a 100644<br>
--- a/src/libinput-util.h<br>
+++ b/src/libinput-util.h<br>
@@ -26,6 +26,8 @@<br>
<br>
#include <unistd.h><br>
#include <math.h><br>
+#include <stdarg.h><br>
+#include <stdio.h><br>
#include <string.h><br>
#include <time.h><br>
<br>
@@ -230,6 +232,34 @@ matrix_to_farray6(const struct matrix *m, float out[6])<br>
out[5] = m->val[1][2];<br>
}<br>
<br>
+/**<br>
+ * Simple wrapper for asprintf that ensures the passed in pointer is set<br>
+ * to NULL upon error.<br>
+ * The standard asprintf() call does not guarantee the passed in pointer<br>
+ * will be NULL'ed upon failure, whereas this wrapper does.<br>
+ *<br>
+ * @param strp pointer to set to newly allocated string.<br>
+ * This pointer should be passed to free() to release when done.<br>
+ * @param fmt the format string to use for printing.<br>
+ * @return The number of bytes printed (excluding the null byte terminator)<br>
+ * upon success or -1 upon failure. In the case of failure the pointer is set<br>
+ * to NULL.<br>
+ */<br>
+static inline int<br>
+xasprintf(char **strp, const char *fmt, ...)<br>
+{<br>
+ int rc = 0;<br>
+ va_list args;<br>
+<br>
+ va_start(args, fmt);<br>
+ rc = vasprintf(strp, fmt, args);<br>
+ va_end(args);<br>
+ if ((rc == -1) && strp)<br>
+ *strp = NULL;<br>
+<br>
+ return rc;<br>
+}<br>
+<br>
enum ratelimit_state {<br>
RATELIMIT_EXCEEDED,<br>
RATELIMIT_THRESHOLD,<br>
diff --git a/src/timer.c b/src/timer.c<br>
index dbd3894..1e507df 100644<br>
--- a/src/timer.c<br>
+++ b/src/timer.c<br>
@@ -20,6 +20,8 @@<br>
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.<br>
*/<br>
<br>
+#include "config.h"<br>
+<br>
#include <assert.h><br>
#include <errno.h><br>
#include <inttypes.h><br>
diff --git a/test/litest.c b/test/litest.c<br>
index 53c441b..b3410bd 100644<br>
--- a/test/litest.c<br>
+++ b/test/litest.c<br>
@@ -876,7 +876,7 @@ litest_init_udev_rules(struct litest_test_device *dev)<br>
ck_abort_msg("Failed to create udev rules directory (%s)\n",<br>
strerror(errno));<br>
<br>
- rc = asprintf(&path,<br>
+ rc = xasprintf(&path,<br>
"%s/%s%s.rules",<br>
UDEV_RULES_D,<br>
UDEV_RULE_PREFIX,<br>
diff --git a/tools/libinput-list-devices.c b/tools/libinput-list-devices.c<br>
index 6625173..68ddb61 100644<br>
--- a/tools/libinput-list-devices.c<br>
+++ b/tools/libinput-list-devices.c<br>
@@ -120,17 +120,17 @@ calibration_default(struct libinput_device *device)<br>
<span class=""> float calibration[6];<br>
<br>
if (!libinput_device_config_calibration_has_matrix(device)) {<br>
- asprintf(&str, "n/a");<br>
</span>+ xasprintf(&str, "n/a");<br>
<span class=""> return str;<br>
}<br>
<br>
if (libinput_device_config_calibration_get_default_matrix(device,<br>
calibration) == 0) {<br>
- asprintf(&str, "identity matrix");<br>
</span>+ xasprintf(&str, "identity matrix");<br>
return str;<br>
}<br>
<br>
- asprintf(&str,<br>
+ xasprintf(&str,<br>
<span class=""> "%.2f %.2f %.2f %.2f %.2f %.2f",<br>
</span> calibration[0],<br>
calibration[1],<br>
@@ -150,13 +150,13 @@ scroll_defaults(struct libinput_device *device)<br>
<span class=""><br>
scroll_methods = libinput_device_config_scroll_get_methods(device);<br>
if (scroll_methods == LIBINPUT_CONFIG_SCROLL_NO_SCROLL) {<br>
- asprintf(&str, "none");<br>
</span>+ xasprintf(&str, "none");<br>
<span class=""> return str;<br>
}<br>
<br>
method = libinput_device_config_scroll_get_default_method(device);<br>
<br>
- asprintf(&str,<br>
</span>+ xasprintf(&str,<br>
"%s%s%s%s%s%s",<br>
<span class=""> (method == LIBINPUT_CONFIG_SCROLL_2FG) ? "*" : "",<br>
</span><span class=""> (scroll_methods & LIBINPUT_CONFIG_SCROLL_2FG) ? "two-finger " : "",<br>
</span>@@ -176,12 +176,12 @@ click_defaults(struct libinput_device *device)<br>
<span class=""><br>
click_methods = libinput_device_config_click_get_methods(device);<br>
if (click_methods == LIBINPUT_CONFIG_CLICK_METHOD_NONE) {<br>
- asprintf(&str, "none");<br>
</span>+ xasprintf(&str, "none");<br>
<span class=""> return str;<br>
}<br>
<br>
method = libinput_device_config_click_get_default_method(device);<br>
- asprintf(&str,<br>
</span>+ xasprintf(&str,<br>
"%s%s%s%s",<br>
<span class="im HOEnZb"> (method == LIBINPUT_CONFIG_CLICK_METHOD_BUTTON_AREAS) ? "*" : "",<br>
</span><span class="im HOEnZb"> (click_methods & LIBINPUT_CONFIG_CLICK_METHOD_BUTTON_AREAS) ? "button-areas " : "",<br>
</span><div class="HOEnZb"><div class="h5">--<br>
2.1.0<br>
<br>
_______________________________________________<br>
wayland-devel mailing list<br>
<a href="mailto:wayland-devel@lists.freedesktop.org">wayland-devel@lists.freedesktop.org</a><br>
<a href="http://lists.freedesktop.org/mailman/listinfo/wayland-devel" target="_blank">http://lists.freedesktop.org/mailman/listinfo/wayland-devel</a><br>
</div></div></blockquote></div><br></div>