<div dir="ltr"><span style="font-family:arial,sans-serif;font-size:13px">Hi</span><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">The patch is used to replace strtol and strtoul with wl_strtol and wl_strtoul with inputs and result checks.</div><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px"><div>The utility functions are used extensively in wayland and weston so added appropriate<br></div><div>input and output checks; test cases are also updated; will push the patch for weston as well.</div></div><div style="font-family:arial,sans-serif;font-size:13px">----</div><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style><div style><font face="arial, sans-serif">diff --git a/src/scanner.c b/src/scanner.c</font></div><div style><font face="arial, sans-serif">index 809130b..3e30fe7 100644</font></div><div style><font face="arial, sans-serif">--- a/src/scanner.c</font></div><div style><font face="arial, sans-serif">+++ b/src/scanner.c</font></div><div style><font face="arial, sans-serif">@@ -315,7 +315,6 @@ start_element(void *data, const char *element_name, const char **atts)</font></div><div style><font face="arial, sans-serif"> <span class="" style="white-space:pre">   </span>struct description *description;</font></div><div style><font face="arial, sans-serif"> <span class="" style="white-space:pre">       </span>const char *name, *type, *interface_name, *value, *summary, *since;</font></div><div style><font face="arial, sans-serif"> <span class="" style="white-space:pre">    </span>const char *allow_null;</font></div><div style><font face="arial, sans-serif">-<span class="" style="white-space:pre"> </span>char *end;</font></div><div style><font face="arial, sans-serif"> <span class="" style="white-space:pre">     </span>int i, version;</font></div><div style><font face="arial, sans-serif"> </font></div><div style><font face="arial, sans-serif"> <span class="" style="white-space:pre">     </span>ctx->loc.line_number = XML_GetCurrentLineNumber(ctx->parser);</font></div><div style><font face="arial, sans-serif">@@ -404,9 +403,7 @@ start_element(void *data, const char *element_name, const char **atts)</font></div><div style><font face="arial, sans-serif"> <span class="" style="white-space:pre">                 </span>message->destructor = 0;</font></div><div style><font face="arial, sans-serif"> </font></div><div style><font face="arial, sans-serif"> <span class="" style="white-space:pre">         </span>if (since != NULL) {</font></div><div style><font face="arial, sans-serif">-<span class="" style="white-space:pre">                    </span>errno = 0;</font></div><div style><font face="arial, sans-serif">-<span class="" style="white-space:pre">                      </span>version = strtol(since, &end, 0);</font></div><div style><font face="arial, sans-serif">-<span class="" style="white-space:pre">                   </span>if (errno == EINVAL || end == since || *end != '\0')</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">                    </span>if (!wl_strtol(since, NULL, 0, &version))</font></div><div style><font face="arial, sans-serif"> <span class="" style="white-space:pre">                          </span>fail(&ctx->loc,</font></div><div style><font face="arial, sans-serif"> <span class="" style="white-space:pre">                         </span>     "invalid integer (%s)\n", since);</font></div><div style><font face="arial, sans-serif"> <span class="" style="white-space:pre">             </span>} else {</font></div><div style><font face="arial, sans-serif">diff --git a/src/wayland-client.c b/src/wayland-client.c</font></div><div style><font face="arial, sans-serif">index b0f77b9..1229b5f 100644</font></div><div style><font face="arial, sans-serif">--- a/src/wayland-client.c</font></div><div style><font face="arial, sans-serif">+++ b/src/wayland-client.c</font></div><div style><font face="arial, sans-serif">@@ -824,13 +824,12 @@ wl_display_connect_to_fd(int fd)</font></div><div style><font face="arial, sans-serif"> WL_EXPORT struct wl_display *</font></div><div style><font face="arial, sans-serif"> wl_display_connect(const char *name)</font></div><div style><font face="arial, sans-serif"> {</font></div><div style><font face="arial, sans-serif">-<span class="" style="white-space:pre"> </span>char *connection, *end;</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre"> </span>char *connection;</font></div><div style><font face="arial, sans-serif"> <span class="" style="white-space:pre">      </span>int flags, fd;</font></div><div style><font face="arial, sans-serif"> </font></div><div style><font face="arial, sans-serif"> <span class="" style="white-space:pre">      </span>connection = getenv("WAYLAND_SOCKET");</font></div><div style><font face="arial, sans-serif"> <span class="" style="white-space:pre">       </span>if (connection) {</font></div><div style><font face="arial, sans-serif">-<span class="" style="white-space:pre">               </span>fd = strtol(connection, &end, 0);</font></div><div style><font face="arial, sans-serif">-<span class="" style="white-space:pre">           </span>if (*end != '\0')</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">               </span>if (!wl_strtol(connection, NULL, 0, &fd))</font></div><div style><font face="arial, sans-serif"> <span class="" style="white-space:pre">                  </span>return NULL;</font></div><div style><font face="arial, sans-serif"> </font></div><div style><font face="arial, sans-serif"> <span class="" style="white-space:pre">                </span>flags = fcntl(fd, F_GETFD);</font></div><div style><font face="arial, sans-serif">diff --git a/src/wayland-util.c b/src/wayland-util.c</font></div><div style><font face="arial, sans-serif">index b099882..f8267f3 100644</font></div><div style><font face="arial, sans-serif">--- a/src/wayland-util.c</font></div><div style><font face="arial, sans-serif">+++ b/src/wayland-util.c</font></div><div style><font face="arial, sans-serif">@@ -26,6 +26,9 @@</font></div><div style><font face="arial, sans-serif"> #include <stdio.h></font></div><div style><font face="arial, sans-serif"> #include <string.h></font></div><div style><font face="arial, sans-serif"> #include <stdarg.h></font></div><div style><font face="arial, sans-serif">+#include <errno.h></font></div><div style><font face="arial, sans-serif">+#include <limits.h></font></div><div style><font face="arial, sans-serif">+#include <stdlib.h></font></div><div style><font face="arial, sans-serif"> </font></div><div style><font face="arial, sans-serif"> #include "wayland-util.h"</font></div><div style><font face="arial, sans-serif"> #include "wayland-private.h"</font></div><div style><font face="arial, sans-serif">@@ -361,6 +364,42 @@ wl_map_for_each(struct wl_map *map, wl_iterator_func_t func, void *data)</font></div><div style><font face="arial, sans-serif"> <span class="" style="white-space:pre">  </span>for_each_helper(&map->server_entries, func, data);</font></div><div style><font face="arial, sans-serif"> }</font></div><div style><font face="arial, sans-serif"> </font></div><div style><font face="arial, sans-serif">+WL_EXPORT int</font></div><div style><font face="arial, sans-serif">+wl_strtol(const char *str, char **endptr, int base, int32_t *val)</font></div><div style><font face="arial, sans-serif">+{</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">   </span>char *end = NULL;</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">       </span>long v;</font></div><div style><font face="arial, sans-serif">+</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">       </span>if (!str || !val) return 0;</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">     </span>if (!endptr) endptr = &end;</font></div><div style><font face="arial, sans-serif">+</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">       </span>errno = 0;</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">      </span>v = strtol(str, endptr, base);</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">  </span>if (errno != 0 || *endptr == str || **endptr != '\0')</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">           </span>return 0;</font></div><div style><font face="arial, sans-serif">+</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">     </span>*val = v;</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">       </span>return 1;</font></div><div style><font face="arial, sans-serif">+}</font></div><div style><font face="arial, sans-serif">+</font></div><div style><font face="arial, sans-serif">+WL_EXPORT int</font></div><div style><font face="arial, sans-serif">+wl_strtoul(const char *str, char **endptr, int base, uint32_t *val)</font></div><div style><font face="arial, sans-serif">+{</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">   </span>char *end = NULL;</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">       </span>unsigned long v;</font></div><div style><font face="arial, sans-serif">+</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">      </span>if (!str || !val) return 0;</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">     </span>if (!endptr) endptr = &end;</font></div><div style><font face="arial, sans-serif">+</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">       </span>errno = 0;</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">      </span>v = strtoul(str, endptr, base);</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre"> </span>if (errno != 0 || *endptr == str || **endptr != '\0')</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">           </span>return 0;</font></div><div style><font face="arial, sans-serif">+</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">     </span>*val = v;</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">       </span>return 1;</font></div><div style><font face="arial, sans-serif">+}</font></div><div style><font face="arial, sans-serif">+</font></div><div style><font face="arial, sans-serif"> static void</font></div><div style><font face="arial, sans-serif"> wl_log_stderr_handler(const char *fmt, va_list arg)</font></div><div style><font face="arial, sans-serif"> {</font></div><div style><font face="arial, sans-serif">diff --git a/src/wayland-util.h b/src/wayland-util.h</font></div><div style><font face="arial, sans-serif">index fd32826..b77d4e3 100644</font></div><div style><font face="arial, sans-serif">--- a/src/wayland-util.h</font></div><div style><font face="arial, sans-serif">+++ b/src/wayland-util.h</font></div><div style><font face="arial, sans-serif">@@ -243,6 +243,9 @@ static inline wl_fixed_t wl_fixed_from_int(int i)</font></div><div style><font face="arial, sans-serif"> <span class="" style="white-space:pre">      </span>return i * 256;</font></div><div style><font face="arial, sans-serif"> }</font></div><div style><font face="arial, sans-serif"> </font></div><div style><font face="arial, sans-serif">+int wl_strtol(const char *str, char **endptr, int base, int32_t *val);</font></div><div style><font face="arial, sans-serif">+int wl_strtoul(const char *str, char **endptr, int base, uint32_t *val);</font></div><div style><font face="arial, sans-serif">+</font></div><div style><font face="arial, sans-serif"> /**</font></div><div style><font face="arial, sans-serif">  * \brief A union representing all of the basic data types that can be passed</font></div><div style><font face="arial, sans-serif">  * along the wayland wire format.</font></div><div style><font face="arial, sans-serif">diff --git a/tests/fixed-test.c b/tests/fixed-test.c</font></div><div style><font face="arial, sans-serif">index 739a3b1..349cc48 100644</font></div><div style><font face="arial, sans-serif">--- a/tests/fixed-test.c</font></div><div style><font face="arial, sans-serif">+++ b/tests/fixed-test.c</font></div><div style><font face="arial, sans-serif">@@ -88,3 +88,61 @@ TEST(fixed_int_conversions)</font></div><div style><font face="arial, sans-serif"> <span class="" style="white-space:pre">       </span>i = wl_fixed_to_int(f);</font></div><div style><font face="arial, sans-serif"> <span class="" style="white-space:pre">        </span>assert(i == -0x50);</font></div><div style><font face="arial, sans-serif"> }</font></div><div style><font face="arial, sans-serif">+</font></div><div style><font face="arial, sans-serif">+TEST(strtol_conversions)</font></div><div style><font face="arial, sans-serif">+{</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">  </span>int ret;</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">        </span>int32_t val = -1;</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">       </span>char *end = NULL;</font></div><div style><font face="arial, sans-serif">+</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">     </span>char *str = "12";</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">     </span>ret = wl_strtol(str, NULL, 10, &val);</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">       </span>assert(ret == 1);</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">       </span>assert(val == 12);</font></div><div style><font face="arial, sans-serif">+</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">    </span>ret = wl_strtol(str, &end, 10, &val);</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">   </span>assert(end != NULL);</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">    </span>assert(*end == '\0');</font></div><div style><font face="arial, sans-serif">+</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre"> </span>str = "s12"; val = -1;</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">        </span>ret = wl_strtol(str, NULL, 10, &val);</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">       </span>assert(ret == 0);</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">       </span>assert(val == -1);</font></div><div style><font face="arial, sans-serif">+</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">    </span>ret = wl_strtol(str, &end, 10, &val);</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">   </span>assert(end == str);</font></div><div style><font face="arial, sans-serif">+</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">   </span>str = ""; val = -1;</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">   </span>ret = wl_strtol(str, NULL, 10, &val);</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">       </span>assert(ret == 0);</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">       </span>assert(val == -1);</font></div><div style><font face="arial, sans-serif">+}</font></div><div style><font face="arial, sans-serif">+</font></div><div style><font face="arial, sans-serif">+TEST(strtoul_conversions)</font></div><div style><font face="arial, sans-serif">+{</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">   </span>int ret;</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">        </span>uint32_t val = 0;</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">       </span>char *end = NULL;</font></div><div style><font face="arial, sans-serif">+</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">     </span>char *str = "15";</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">     </span>ret = wl_strtoul(str, NULL, 10, &val);</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">      </span>assert(ret == 1);</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">       </span>assert(val == 15);</font></div><div style><font face="arial, sans-serif">+</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">    </span>ret = wl_strtoul(str, &end, 10, &val);</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">  </span>assert(end != NULL);</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">    </span>assert(*end == '\0');</font></div><div style><font face="arial, sans-serif">+</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre"> </span>str = "s15"; val = 0;</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre"> </span>ret = wl_strtoul(str, NULL, 10, &val);</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">      </span>assert(ret == 0);</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">       </span>assert(val == 0);</font></div><div style><font face="arial, sans-serif">+</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">     </span>ret = wl_strtoul(str, &end, 10, &val);</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">  </span>assert(end == str);</font></div><div style><font face="arial, sans-serif">+</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">   </span>str = ""; val = 0;</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">    </span>ret = wl_strtoul(str, NULL, 10, &val);</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">      </span>assert(ret == 0);</font></div><div style><font face="arial, sans-serif">+<span class="" style="white-space:pre">       </span>assert(val == 0);</font></div><div style><font face="arial, sans-serif">+}</font></div></div></div>