Added string conversion utility functions 51/30651/1
authorImran Zaman <imran.zaman@intel.com>
Tue, 18 Nov 2014 14:07:11 +0000 (16:07 +0200)
committerImran Zaman <imran.zaman@intel.com>
Fri, 21 Nov 2014 14:53:07 +0000 (16:53 +0200)
Change-Id: I50900852311604a8c31313bbfb1d137c495d2269
Signed-off-by: Imran Zaman <imran.zaman@intel.com>
12 files changed:
Makefile.am
clients/multi-resource.c
clients/terminal.c
clients/wscreensaver-glue.c
shared/config-parser.c
shared/option-parser.c
shared/str-util.c [new file with mode: 0644]
shared/str-util.h [new file with mode: 0644]
src/compositor-rdp.c
src/libbacklight.c
tests/strutil-test.c [new file with mode: 0644]
xwayland/launcher.c

index b2d6893..b72d03e 100644 (file)
@@ -787,6 +787,8 @@ libshared_la_SOURCES =                              \
        shared/config-parser.c                  \
        shared/option-parser.c                  \
        shared/config-parser.h                  \
+       shared/str-util.c                       \
+       shared/str-util.h                       \
        shared/os-compatibility.c               \
        shared/os-compatibility.h
 
@@ -823,6 +825,7 @@ TESTS = $(shared_tests) $(module_tests) $(weston_tests)
 
 shared_tests =                                 \
        config-parser.test                      \
+       strutil.test                            \
        vertex-clip.test
 
 module_tests =                                 \
@@ -895,6 +898,9 @@ libtest_runner_la_CFLAGS = $(GCC_CFLAGS) $(COMPOSITOR_CFLAGS)
 config_parser_test_SOURCES = tests/config-parser-test.c
 config_parser_test_LDADD = libshared.la libtest-runner.la $(COMPOSITOR_LIBS)
 
+strutil_test_SOURCES = tests/strutil-test.c
+strutil_test_LDADD = libshared.la libtest-runner.la $(COMPOSITOR_LIBS)
+
 vertex_clip_test_SOURCES =                     \
        tests/vertex-clip-test.c                \
        src/vertex-clipping.c                   \
@@ -960,9 +966,11 @@ matrix_test_LDADD = -lm -lrt
 
 if BUILD_SETBACKLIGHT
 noinst_PROGRAMS += setbacklight
-setbacklight_SOURCES =                         \
-       tests/setbacklight.c                    \
-       src/libbacklight.c                      \
+setbacklight_SOURCES =                 \
+       tests/setbacklight.c            \
+       shared/str-util.c               \
+       shared/str-util.h               \
+       src/libbacklight.c              \
        src/libbacklight.h
 setbacklight_CFLAGS = $(AM_CFLAGS) $(SETBACKLIGHT_CFLAGS)
 setbacklight_LDADD = $(SETBACKLIGHT_LIBS)
index 0dc2c74..5d8d2ed 100644 (file)
@@ -39,6 +39,7 @@
 
 #include <wayland-client.h>
 #include "../shared/os-compatibility.h"
+#include "../shared/str-util.h"
 
 struct device {
        enum { KEYBOARD, POINTER } type;
@@ -443,14 +444,11 @@ create_device(struct display *display, const char *time_desc, int type)
                return -1;
        }
 
-       errno = 0;
-       start_time = strtoul(time_desc, &tail, 10);
-       if (errno)
+       if (!weston_strtoi(time_desc, &tail, 10, &start_time))
                goto error;
 
        if (*tail == ':') {
-               end_time = strtoul(tail + 1, &tail, 10);
-               if (errno || *tail != '\0')
+               if (!weston_strtoi(tail + 1, &tail, 10, &end_time))
                        goto error;
        } else if (*tail != '\0') {
                goto error;
index 7c37101..cef8437 100644 (file)
@@ -43,6 +43,7 @@
 #include <wayland-client.h>
 
 #include "../shared/config-parser.h"
+#include "../shared/str-util.h"
 #include "window.h"
 
 static int option_fullscreen;
@@ -1277,11 +1278,12 @@ static void
 handle_osc(struct terminal *terminal)
 {
        char *p;
-       int code;
+       int code = -1;
 
        terminal->escape[terminal->escape_length++] = '\0';
        p = &terminal->escape[2];
-       code = strtol(p, &p, 10);
+
+       weston_strtoi(p, &p, 10, &code);
        if (*p == ';') p++;
 
        switch (code) {
@@ -1324,7 +1326,7 @@ handle_escape(struct terminal *terminal)
                        p++;
                        i++;
                } else {
-                       args[i] = strtol(p, &p, 10);
+                       weston_strtoi(p, &p, 10, &args[i]);
                        set[i] = 1;
                }
        }
index 55d0a8c..ce5f92b 100644 (file)
@@ -21,6 +21,7 @@
  */
 
 #include "wscreensaver-glue.h"
+#include "../shared/str-util.h"
 
 double frand(double f)
 {
@@ -70,6 +71,7 @@ read_xpm_color(uint32_t *ctable, const char *line)
        char cstr[10];
        char *end;
        uint32_t value;
+       bool conv;
 
        if (sscanf(line, "%1c c %9s", &key, cstr) < 2) {
                fprintf(stderr, "%s: error in XPM color definition '%s'\n",
@@ -77,11 +79,11 @@ read_xpm_color(uint32_t *ctable, const char *line)
                return;
        }
 
-       value = strtol(&cstr[1], &end, 16);
+       conv = weston_strtoui(&cstr[1], NULL, 16, &value);
 
        if (strcmp(cstr, "None") == 0)
                ctable[key] = 0x00000000;
-       else if (cstr[0] != '#' || !(cstr[1] != '\0' && *end == '\0')) {
+       else if (cstr[0] != '#' || !conv) {
                fprintf(stderr, "%s: error interpreting XPM color '%s'\n",
                        progname, cstr);
                return;
index 4542ca6..df163e9 100644 (file)
@@ -35,7 +35,9 @@
 #include <errno.h>
 
 #include <wayland-util.h>
+
 #include "config-parser.h"
+#include "str-util.h"
 
 #define container_of(ptr, type, member) ({                             \
        const __typeof__( ((type *)0)->member ) *__mptr = (ptr);        \
@@ -160,7 +162,6 @@ weston_config_section_get_int(struct weston_config_section *section,
                              int32_t *value, int32_t default_value)
 {
        struct weston_config_entry *entry;
-       char *end;
 
        entry = config_section_get_entry(section, key);
        if (entry == NULL) {
@@ -169,8 +170,7 @@ weston_config_section_get_int(struct weston_config_section *section,
                return -1;
        }
 
-       *value = strtol(entry->value, &end, 0);
-       if (*end != '\0') {
+       if (!weston_strtoi(entry->value, NULL, 0, value)) {
                *value = default_value;
                errno = EINVAL;
                return -1;
@@ -186,7 +186,6 @@ weston_config_section_get_uint(struct weston_config_section *section,
                               uint32_t *value, uint32_t default_value)
 {
        struct weston_config_entry *entry;
-       char *end;
 
        entry = config_section_get_entry(section, key);
        if (entry == NULL) {
@@ -195,8 +194,7 @@ weston_config_section_get_uint(struct weston_config_section *section,
                return -1;
        }
 
-       *value = strtoul(entry->value, &end, 0);
-       if (*end != '\0') {
+       if (!weston_strtoui(entry->value, NULL, 0, value)) {
                *value = default_value;
                errno = EINVAL;
                return -1;
index 7061268..dad5e1d 100644 (file)
 #include <stdio.h>
 #include <string.h>
 #include <assert.h>
+#include <wayland-util.h>
 
 #include "config-parser.h"
+#include "str-util.h"
 
 static int
 handle_option(const struct weston_option *option, char *value)
 {
-       char* p;
-
        switch (option->type) {
        case WESTON_OPTION_INTEGER:
-               * (int32_t *) option->data = strtol(value, &p, 0);
-               return *value && !*p;
+               return weston_strtoi(value, NULL, 0, (int32_t *)option->data);
        case WESTON_OPTION_UNSIGNED_INTEGER:
-               * (uint32_t *) option->data = strtoul(value, &p, 0);
-               return *value && !*p;
+               return weston_strtoui(value, NULL, 0, (uint32_t *)option->data);
        case WESTON_OPTION_STRING:
                * (char **) option->data = strdup(value);
                return 1;
diff --git a/shared/str-util.c b/shared/str-util.c
new file mode 100644 (file)
index 0000000..448f7b0
--- /dev/null
@@ -0,0 +1,133 @@
+/*
+ * Copyright © 2014 Intel Corporation.
+ *
+ * Contact: Imran Zaman <imran.zaman@linux.intel.com>
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that the above copyright notice appear in all copies and that both that
+ * copyright notice and this permission notice appear in supporting
+ * documentation, and that the name of the copyright holders not be used in
+ * advertising or publicity pertaining to distribution of the software
+ * without specific, written prior permission.  The copyright holders make
+ * no representations about the suitability of this software for any
+ * purpose.  It is provided "as is" without express or implied warranty.
+ *
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
+ * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <ctype.h>
+#include <limits.h>
+
+#ifdef IN_WESTON
+#include <wayland-util.h>
+#else
+#define WL_EXPORT
+#endif
+
+#include "str-util.h"
+
+static bool
+convert_strtol(const char *str, char **endptr, int base, long *val)
+{
+       char *end = NULL;
+       long v;
+       int prev_errno = errno;
+
+       if (!str || !val)
+               return false;
+       if (!endptr)
+               endptr = &end;
+
+       errno = 0;
+       v = strtol(str, endptr, base);
+       if (errno != 0 || *endptr == str || **endptr != '\0')
+               return false;
+
+       errno = prev_errno;
+       *val = v;
+       return true;
+}
+
+static bool
+convert_strtoul (const char *str, char **endptr, int base, unsigned long *val)
+{
+       char *end = NULL;
+       unsigned long v;
+       int i = 0;
+       int prev_errno = errno;
+
+       if (!str || !val)
+              return false;
+
+       /* check for negative numbers */
+       while (str[i]) {
+              if (!isspace(str[i])) {
+                      if (str[i] == '-')
+                              return false;
+                      else
+                              break;
+              }
+              i++;
+       }
+
+       if (!endptr)
+              endptr = &end;
+
+       errno = 0;
+       v = strtoul(str, endptr, base);
+       if (errno != 0 || *endptr == str || **endptr != '\0')
+              return false;
+
+       errno = prev_errno;
+       *val = v;
+       return true;
+}
+
+WL_EXPORT bool
+weston_strtoi(const char *str, char **endptr, int base, int *val)
+{
+       long v;
+
+       if (!convert_strtol(str, endptr, base, &v) || v > INT_MAX
+                       || v < INT_MIN)
+               return false;
+
+       *val = (int)v;
+       return true;
+}
+
+WL_EXPORT bool
+weston_strtol(const char *str, char **endptr, int base, long *val)
+{
+       return convert_strtol(str, endptr, base, val);
+}
+
+WL_EXPORT bool
+weston_strtoui(const char *str, char **endptr, int base, unsigned int *val)
+{
+       unsigned long v;
+
+       if (!convert_strtoul(str, endptr, base, &v) || v > UINT_MAX)
+               return false;
+
+       *val = (unsigned int)v;
+       return true;
+}
+
+WL_EXPORT bool
+weston_strtoul(const char *str, char **endptr, int base, unsigned long *val)
+{
+       return convert_strtoul(str, endptr, base, val);
+}
diff --git a/shared/str-util.h b/shared/str-util.h
new file mode 100644 (file)
index 0000000..bb6322d
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Copyright © 2014 Intel Corporation.
+ *
+ * Contact: Imran Zaman <imran.zaman@linux.intel.com>
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that the above copyright notice appear in all copies and that both that
+ * copyright notice and this permission notice appear in supporting
+ * documentation, and that the name of the copyright holders not be used in
+ * advertising or publicity pertaining to distribution of the software
+ * without specific, written prior permission.  The copyright holders make
+ * no representations about the suitability of this software for any
+ * purpose.  It is provided "as is" without express or implied warranty.
+ *
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
+ * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef WESTON_STR_UTIL_H
+#define WESTON_STR_UTIL_H
+
+#include <stdbool.h>
+
+#ifdef  __cplusplus
+extern "C" {
+#endif
+
+bool weston_strtoi(const char *str, char **endptr, int base, int *val);
+bool weston_strtol(const char *str, char **endptr, int base, long *val);
+bool weston_strtoui(const char *str, char **endptr, int base, unsigned int *val);
+bool weston_strtoul(const char *str, char **endptr, int base, unsigned long *val);
+
+#ifdef  __cplusplus
+}
+#endif
+
+#endif /* WESTON_STR_UTIL_H */
index b749129..6af37b0 100644 (file)
@@ -1142,7 +1142,8 @@ rdp_compositor_create(struct wl_display *display,
                        goto err_output;
                }
 
-               fd = strtoul(fd_str, NULL, 10);
+               if (!weston_strtoi(fd_str, NULL, 10, &fd))
+                       fd = -1;
                if (rdp_peer_init(freerdp_peer_new(fd), c))
                        goto err_output;
        }
index 54f3318..692c007 100644 (file)
@@ -43,6 +43,8 @@
 #include <string.h>
 #include <errno.h>
 
+#include "../shared/str-util.h"
+
 static long backlight_get(struct backlight *backlight, char *node)
 {
        char buffer[100];
@@ -64,7 +66,7 @@ static long backlight_get(struct backlight *backlight, char *node)
                goto out;
        }
 
-       value = strtol(buffer, NULL, 10);
+       weston_strtol(buffer, NULL, 10, &value);
        ret = value;
 out:
        if (fd >= 0)
diff --git a/tests/strutil-test.c b/tests/strutil-test.c
new file mode 100644 (file)
index 0000000..7616549
--- /dev/null
@@ -0,0 +1,322 @@
+/*
+ * Copyright © 2014 Intel Corporation.
+ *
+ * Contact: Imran Zaman <imran.zaman@linux.intel.com>
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that the above copyright notice appear in all copies and that both that
+ * copyright notice and this permission notice appear in supporting
+ * documentation, and that the name of the copyright holders not be used in
+ * advertising or publicity pertaining to distribution of the software
+ * without specific, written prior permission.  The copyright holders make
+ * no representations about the suitability of this software for any
+ * purpose.  It is provided "as is" without express or implied warranty.
+ *
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
+ * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include "config.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+
+#include "weston-test-runner.h"
+
+#include "../shared/str-util.h"
+
+TEST(test_weston_strtol)
+{
+       bool ret;
+       long val = -1;
+       char *end = NULL, *str = NULL;
+
+       ret = weston_strtol(NULL, NULL, 10, &val);
+       assert(ret == false);
+       assert(val == -1);
+
+       ret = weston_strtol(NULL, NULL, 10, NULL);
+       assert(ret == false);
+
+       str = "12";
+       ret = weston_strtol(str, NULL, 10, &val);
+       assert(ret == true);
+       assert(val == 12);
+
+       ret = weston_strtol(str, &end, 10, &val);
+       assert(end != NULL);
+       assert(*end == '\0');
+
+       str = "-12"; val = -1;
+       ret = weston_strtol(str, &end, 10, &val);
+       assert(ret == true);
+       assert(val == -12);
+
+       str = "0x12"; val = -1;
+       ret = weston_strtol(str, &end, 16, &val);
+       assert(ret == true);
+       assert(val == 0x12);
+
+       str = "A"; val = -1;
+       ret = weston_strtol(str, &end, 16, &val);
+       assert(ret == true);
+       assert(val == 10);
+
+       str = "-0x20"; val = -1;
+       ret = weston_strtol(str, &end, 16, &val);
+       assert(ret == true);
+       assert(val == -0x20);
+
+       str = "0012"; val = -1;
+       ret = weston_strtol(str, &end, 8, &val);
+       assert(ret == true);
+       assert(val == 10);
+
+       str = "0101"; val = -1;
+       ret = weston_strtol(str, &end, 2, &val);
+       assert(ret == true);
+       assert(val == 5);
+
+       str = "s12"; val = -1;
+       ret = weston_strtol(str, NULL, 10, &val);
+       assert(ret == false);
+       assert(val == -1);
+
+       ret = weston_strtol(str, &end, 10, &val);
+       assert(end == str);
+
+       str = "214748364789L"; val = -1;
+       ret = weston_strtol(str, NULL, 10, &val);
+       assert(ret == false);
+       assert(val == -1);
+
+       str = ""; val = -1;
+       ret = weston_strtol(str, NULL, 10, &val);
+       assert(ret == false);
+       assert(val == -1);
+}
+
+TEST(test_weston_strtoul)
+{
+       bool ret;
+       unsigned long val = 0;
+       char *end = NULL, *str = NULL;
+
+       ret = weston_strtoul(NULL, NULL, 10, &val);
+       assert(ret == false);
+       assert(val == 0);
+
+       ret = weston_strtoul(NULL, NULL, 10, NULL);
+       assert(ret == false);
+
+       str = "15";
+       ret = weston_strtoul(str, NULL, 10, &val);
+       assert(ret == true);
+       assert(val == 15);
+
+       ret = weston_strtoul(str, &end, 10, &val);
+       assert(end != NULL);
+       assert(*end == '\0');
+
+       str = "0x12"; val = 0;
+       ret = weston_strtoul(str, &end, 16, &val);
+       assert(ret == true);
+       assert(val == 18);
+
+       str = "A"; val = 0;
+       ret = weston_strtoul(str, &end, 16, &val);
+       assert(ret == true);
+       assert(val == 10);
+
+       str = "0012"; val = 0;
+       ret = weston_strtoul(str, &end, 8, &val);
+       assert(ret == true);
+       assert(val == 10);
+
+       str = "0101"; val = 0;
+       ret = weston_strtoul(str, &end, 2, &val);
+       assert(ret == true);
+       assert(val == 5);
+
+       str = "s15"; val = 0;
+       ret = weston_strtoul(str, NULL, 10, &val);
+       assert(ret == false);
+       assert(val == 0);
+
+       ret = weston_strtoul(str, &end, 10, &val);
+       assert(end == str);
+
+       str = "429496729533UL"; val = 0;
+       ret = weston_strtoul(str, NULL, 10, &val);
+       assert(ret == false);
+       assert(val == 0);
+
+       str = "-1"; val = 0;
+       ret = weston_strtoul(str, NULL, 10, &val);
+       assert(ret == false);
+       assert(val == 0);
+
+       str = "    -1234"; val = 0;
+       ret = weston_strtoul(str, NULL, 10, &val);
+       assert(ret == false);
+       assert(val == 0);
+
+       str = ""; val = 0;
+       ret = weston_strtoul(str, NULL, 10, &val);
+       assert(ret == false);
+       assert(val == 0);
+}
+
+TEST(test_weston_strtoi)
+{
+       bool ret;
+       int val = -1;
+       char *end = NULL, *str = NULL;
+
+       ret = weston_strtoi(NULL, NULL, 10, &val);
+       assert(ret == false);
+       assert(val == -1);
+
+       ret = weston_strtoi(NULL, NULL, 10, NULL);
+       assert(ret == false);
+
+       str = "12";
+       ret = weston_strtoi(str, NULL, 10, &val);
+       assert(ret == true);
+       assert(val == 12);
+
+       ret = weston_strtoi(str, &end, 10, &val);
+       assert(end != NULL);
+       assert(*end == '\0');
+
+       str = "-12"; val = -1;
+       ret = weston_strtoi(str, &end, 10, &val);
+       assert(ret == true);
+       assert(val == -12);
+
+       str = "0x12"; val = -1;
+       ret = weston_strtoi(str, &end, 16, &val);
+       assert(ret == true);
+       assert(val == 0x12);
+
+       str = "A"; val = -1;
+       ret = weston_strtoi(str, &end, 16, &val);
+       assert(ret == true);
+       assert(val == 10);
+
+       str = "-0x20"; val = -1;
+       ret = weston_strtoi(str, &end, 16, &val);
+       assert(ret == true);
+       assert(val == -0x20);
+
+       str = "0012"; val = -1;
+       ret = weston_strtoi(str, &end, 8, &val);
+       assert(ret == true);
+       assert(val == 10);
+
+       str = "0101"; val = -1;
+       ret = weston_strtoi(str, &end, 2, &val);
+       assert(ret == true);
+       assert(val == 5);
+
+       str = "-5"; val = -1;
+       ret = weston_strtoi(str, &end, 2, &val);
+       assert(ret == true);
+       assert(val == -5);
+
+       str = "s12"; val = -1;
+       ret = weston_strtoi(str, NULL, 10, &val);
+       assert(ret == false);
+       assert(val == -1);
+
+       ret = weston_strtoi(str, &end, 10, &val);
+       assert(end == str);
+
+       str = "214748364789L"; val = -1;
+       ret = weston_strtoi(str, NULL, 10, &val);
+       assert(ret == false);
+       assert(val == -1);
+
+       str = ""; val = -1;
+       ret = weston_strtoi(str, NULL, 10, &val);
+       assert(ret == false);
+       assert(val == -1);
+}
+
+TEST(test_weston_strtoui)
+{
+       bool ret;
+       unsigned int val = 0;
+       char *end = NULL, *str = NULL;
+
+       ret = weston_strtoui(NULL, NULL, 10, &val);
+       assert(ret == false);
+       assert(val == 0);
+
+       ret = weston_strtoui(NULL, NULL, 10, NULL);
+       assert(ret == false);
+
+       str = "15";
+       ret = weston_strtoui(str, NULL, 10, &val);
+       assert(ret == true);
+       assert(val == 15);
+
+       ret = weston_strtoui(str, &end, 10, &val);
+       assert(end != NULL);
+       assert(*end == '\0');
+
+       str = "0x12"; val = 0;
+       ret = weston_strtoui(str, &end, 16, &val);
+       assert(ret == true);
+       assert(val == 18);
+
+       str = "A"; val = 0;
+       ret = weston_strtoui(str, &end, 16, &val);
+       assert(ret == true);
+       assert(val == 10);
+
+       str = "0012"; val = 0;
+       ret = weston_strtoui(str, &end, 8, &val);
+       assert(ret == true);
+       assert(val == 10);
+
+       str = "0101"; val = 0;
+       ret = weston_strtoui(str, &end, 2, &val);
+       assert(ret == true);
+       assert(val == 5);
+
+       str = "s15"; val = 0;
+       ret = weston_strtoui(str, NULL, 10, &val);
+       assert(ret == false);
+       assert(val == 0);
+
+       ret = weston_strtoui(str, &end, 10, &val);
+       assert(end == str);
+
+       str = "429496729533UL"; val = 0;
+       ret = weston_strtoui(str, NULL, 10, &val);
+       assert(ret == false);
+       assert(val == 0);
+
+       str = "-1"; val = 0;
+       ret = weston_strtoui(str, NULL, 10, &val);
+       assert(ret == false);
+       assert(val == 0);
+
+       str = "    -1234"; val = 0;
+       ret = weston_strtoui(str, NULL, 10, &val);
+       assert(ret == false);
+       assert(val == 0);
+
+       str = ""; val = 0;
+       ret = weston_strtoui(str, NULL, 10, &val);
+       assert(ret == false);
+       assert(val == 0);
+}
index df2efd2..4ba2031 100644 (file)
@@ -284,8 +284,7 @@ create_lockfile(int display, char *lockfile, size_t lsize)
                        return -1;
                }
 
-               other = strtol(pid, &end, 0);
-               if (end != pid + 10) {
+               if (!weston_strtoi(pid, &end, 0, &other) || end != pid + 10) {
                        weston_log("can't parse lock file %s\n",
                                lockfile);
                        close(fd);