[PATCH v1] Added string conversion utility functions
Imran Zaman
imran.zaman at gmail.com
Fri Nov 21 07:38:28 PST 2014
Change-Id: I50900852311604a8c31313bbfb1d137c495d2269
Signed-off-by: Imran Zaman <imran.zaman at gmail.com>
---
Makefile.am | 14 +-
clients/multi-resource.c | 8 +-
clients/terminal.c | 8 +-
clients/wscreensaver-glue.c | 6 +-
shared/config-parser.c | 10 +-
shared/option-parser.c | 10 +-
shared/str-util.c | 133 ++++++++++++++++++
shared/str-util.h | 43 ++++++
src/compositor-rdp.c | 3 +-
src/compositor.c | 12 +-
src/libbacklight.c | 4 +-
tests/strutil-test.c | 322 ++++++++++++++++++++++++++++++++++++++++++++
xwayland/launcher.c | 5 +-
13 files changed, 542 insertions(+), 36 deletions(-)
create mode 100644 shared/str-util.c
create mode 100644 shared/str-util.h
create mode 100644 tests/strutil-test.c
diff --git a/Makefile.am b/Makefile.am
index 2fc171e..a78a8d2 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -802,6 +802,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
@@ -838,6 +840,7 @@ TESTS = $(shared_tests) $(module_tests) $(weston_tests)
shared_tests = \
config-parser.test \
+ strutil.test \
vertex-clip.test
module_tests = \
@@ -912,6 +915,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 \
@@ -988,9 +994,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)
diff --git a/clients/multi-resource.c b/clients/multi-resource.c
index 0dc2c74..5d8d2ed 100644
--- a/clients/multi-resource.c
+++ b/clients/multi-resource.c
@@ -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;
diff --git a/clients/terminal.c b/clients/terminal.c
index 7c37101..cef8437 100644
--- a/clients/terminal.c
+++ b/clients/terminal.c
@@ -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;
}
}
diff --git a/clients/wscreensaver-glue.c b/clients/wscreensaver-glue.c
index 55d0a8c..ce5f92b 100644
--- a/clients/wscreensaver-glue.c
+++ b/clients/wscreensaver-glue.c
@@ -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;
diff --git a/shared/config-parser.c b/shared/config-parser.c
index 4542ca6..df163e9 100644
--- a/shared/config-parser.c
+++ b/shared/config-parser.c
@@ -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;
diff --git a/shared/option-parser.c b/shared/option-parser.c
index 7061268..dad5e1d 100644
--- a/shared/option-parser.c
+++ b/shared/option-parser.c
@@ -27,21 +27,19 @@
#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
index 0000000..448f7b0
--- /dev/null
+++ b/shared/str-util.c
@@ -0,0 +1,133 @@
+/*
+ * Copyright © 2014 Intel Corporation.
+ *
+ * Contact: Imran Zaman <imran.zaman at 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
index 0000000..bb6322d
--- /dev/null
+++ b/shared/str-util.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright © 2014 Intel Corporation.
+ *
+ * Contact: Imran Zaman <imran.zaman at 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 */
diff --git a/src/compositor-rdp.c b/src/compositor-rdp.c
index 2048f8f..f467b82 100644
--- a/src/compositor-rdp.c
+++ b/src/compositor-rdp.c
@@ -1143,7 +1143,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;
}
diff --git a/src/compositor.c b/src/compositor.c
index 0db6d73..9394392 100644
--- a/src/compositor.c
+++ b/src/compositor.c
@@ -57,6 +57,7 @@
#include "scaler-server-protocol.h"
#include "presentation_timing-server-protocol.h"
#include "../shared/os-compatibility.h"
+#include "../shared/str-util.h"
#include "git-version.h"
#include "version.h"
@@ -4000,14 +4001,14 @@ log_uname(void)
WL_EXPORT int
weston_environment_get_fd(const char *env)
{
- char *e, *end;
+ char *e;
int fd, flags;
e = getenv(env);
if (!e)
return -1;
- fd = strtol(e, &end, 0);
- if (*end != '\0')
+
+ if (!weston_strtoi(e, NULL, 10, &fd))
return -1;
flags = fcntl(fd, F_GETFD);
@@ -4712,7 +4713,7 @@ int main(int argc, char *argv[])
char *modules = NULL;
char *option_modules = NULL;
char *log = NULL;
- char *server_socket = NULL, *end;
+ char *server_socket = NULL;
int32_t idle_time = -1;
int32_t help = 0;
char *socket_name = NULL;
@@ -4831,8 +4832,7 @@ int main(int argc, char *argv[])
server_socket = getenv("WAYLAND_SERVER_SOCKET");
if (server_socket) {
weston_log("Running with single client\n");
- fd = strtol(server_socket, &end, 0);
- if (*end != '\0')
+ if (!weston_strtoi(server_socket, NULL, 0, &fd))
fd = -1;
} else {
fd = -1;
diff --git a/src/libbacklight.c b/src/libbacklight.c
index 54f3318..692c007 100644
--- a/src/libbacklight.c
+++ b/src/libbacklight.c
@@ -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
index 0000000..e4996b4
--- /dev/null
+++ b/tests/strutil-test.c
@@ -0,0 +1,322 @@
+/*
+ * Copyright © 2014 Intel Corporation.
+ *
+ * Contact: Imran Zaman <imran.zaman at 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, 10, &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);
+}
diff --git a/xwayland/launcher.c b/xwayland/launcher.c
index df2efd2..98a43ff 100644
--- a/xwayland/launcher.c
+++ b/xwayland/launcher.c
@@ -33,7 +33,7 @@
#include <signal.h>
#include "xwayland.h"
-
+#include "../shared/str-util.h"
static int
handle_sigusr1(int signal_number, void *data)
@@ -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);
--
1.9.1
More information about the wayland-devel
mailing list