Loosely derived from an earlier patch by Imran Zaman. Signed-off-by: Bryce Harrington <br...@osg.samsung.com> --- Makefile.am | 7 ++++ compositor/main.c | 7 ++-- compositor/systemd-notify.c | 9 ++--- libweston/compositor.c | 9 ++--- libweston/libbacklight.c | 11 +++--- shared/config-parser.c | 7 ++-- shared/option-parser.c | 5 ++- tests/string-test.c | 87 +++++++++++++++++++++++++++++++++++++++++++++ xwayland/launcher.c | 7 ++-- 9 files changed, 115 insertions(+), 34 deletions(-) create mode 100644 tests/string-test.c
diff --git a/Makefile.am b/Makefile.am index cdf7bdb..2114b5c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1110,6 +1110,7 @@ internal_tests = \ shared_tests = \ config-parser.test \ + string.test \ vertex-clip.test \ zuctest @@ -1208,6 +1209,12 @@ config_parser_test_CFLAGS = \ $(AM_CFLAGS) \ -I$(top_srcdir)/tools/zunitc/inc +string_test_SOURCES = \ + tests/string-test.c \ + shared/string-helpers.h +string_test_CFLAGS = $(AM_CFLAGS) $(TEST_CLIENT_CFLAGS) +string_test_LDADD = libtest-client.la + vertex_clip_test_SOURCES = \ tests/vertex-clip-test.c \ shared/helpers.h \ diff --git a/compositor/main.c b/compositor/main.c index 0f7a0c0..4cf8468 100644 --- a/compositor/main.c +++ b/compositor/main.c @@ -52,6 +52,7 @@ #include "compositor.h" #include "../shared/os-compatibility.h" #include "../shared/helpers.h" +#include "../shared/string-helpers.h" #include "git-version.h" #include "version.h" #include "weston.h" @@ -1568,7 +1569,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; @@ -1685,9 +1686,7 @@ int main(int argc, char *argv[]) server_socket = getenv("WAYLAND_SERVER_SOCKET"); if (server_socket) { weston_log("Running with single client\n"); - errno = 0; - fd = strtol(server_socket, &end, 10); - if (errno != 0 || end == server_socket || *end != '\0') + if (!safe_strtoint(server_socket, &fd)) fd = -1; } else { fd = -1; diff --git a/compositor/systemd-notify.c b/compositor/systemd-notify.c index 6104124..49e51f4 100644 --- a/compositor/systemd-notify.c +++ b/compositor/systemd-notify.c @@ -25,12 +25,13 @@ #include "config.h" -#include <errno.h> #include <stdlib.h> #include <systemd/sd-daemon.h> #include <sys/socket.h> #include <wayland-server.h> + #include "shared/helpers.h" +#include "shared/string-helpers.h" #include "shared/zalloc.h" #include "compositor.h" @@ -116,7 +117,6 @@ WL_EXPORT int module_init(struct weston_compositor *compositor, int *argc, char *argv[]) { - char *tail; char *watchdog_time_env; struct wl_event_loop *loop; long watchdog_time_conv; @@ -140,13 +140,10 @@ module_init(struct weston_compositor *compositor, * by systemd to transfer 'WatchdogSec' watchdog timeout * setting from service file.*/ watchdog_time_env = getenv("WATCHDOG_USEC"); - if (!watchdog_time_env) return 0; - errno = 0; - watchdog_time_conv = strtol(watchdog_time_env, &tail, 10); - if (errno != 0 || tail == watchdog_time_env || *tail != '\0') + if (!safe_strtoint(watchdog_time_env, &watchdog_time_conv)) return 0; /* Convert 'WATCHDOG_USEC' to milliseconds and notify diff --git a/libweston/compositor.c b/libweston/compositor.c index e9c2a83..b17c76d 100644 --- a/libweston/compositor.c +++ b/libweston/compositor.c @@ -58,6 +58,7 @@ #include "presentation-time-server-protocol.h" #include "shared/helpers.h" #include "shared/os-compatibility.h" +#include "shared/string-helpers.h" #include "shared/timespec-util.h" #include "git-version.h" #include "version.h" @@ -4622,15 +4623,11 @@ compositor_bind(struct wl_client *client, 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; - errno = 0; - fd = strtol(e, &end, 10); - if (errno != 0 || end == e || *end != '\0') + if (!e || !safe_strtoint(e, &fd)) return -1; flags = fcntl(fd, F_GETFD); diff --git a/libweston/libbacklight.c b/libweston/libbacklight.c index 59f4e44..4bbc6db 100644 --- a/libweston/libbacklight.c +++ b/libweston/libbacklight.c @@ -44,13 +44,14 @@ #include <string.h> #include <errno.h> +#include "shared/string-helpers.h" + static long backlight_get(struct backlight *backlight, char *node) { char buffer[100]; char *path; - char *end; - int fd; - long value, ret; + int fd, value; + long ret; if (asprintf(&path, "%s/%s", backlight->path, node) < 0) return -ENOMEM; @@ -66,9 +67,7 @@ static long backlight_get(struct backlight *backlight, char *node) goto out; } - errno = 0; - value = strtol(buffer, &end, 10); - if (errno != 0 || end == buffer || *end != '\0') { + if (!safe_strtoint(buffer, &value)) { ret = -1; goto out; } diff --git a/shared/config-parser.c b/shared/config-parser.c index 1edfd60..d773cc9 100644 --- a/shared/config-parser.c +++ b/shared/config-parser.c @@ -41,6 +41,7 @@ #include <wayland-util.h> #include "config-parser.h" #include "helpers.h" +#include "string-helpers.h" struct weston_config_entry { char *key; @@ -161,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) { @@ -170,11 +170,8 @@ weston_config_section_get_int(struct weston_config_section *section, return -1; } - errno = 0; - *value = strtol(entry->value, &end, 10); - if (errno != 0 || end == entry->value || *end != '\0') { + if (!safe_strtoint(entry->value, value)) { *value = default_value; - errno = EINVAL; return -1; } diff --git a/shared/option-parser.c b/shared/option-parser.c index fb4a342..eee7546 100644 --- a/shared/option-parser.c +++ b/shared/option-parser.c @@ -33,6 +33,7 @@ #include <errno.h> #include "config-parser.h" +#include "string-helpers.h" static int handle_option(const struct weston_option *option, char *value) @@ -41,9 +42,7 @@ handle_option(const struct weston_option *option, char *value) switch (option->type) { case WESTON_OPTION_INTEGER: - errno = 0; - * (int32_t *) option->data = strtol(value, &p, 10); - if (errno != 0 || p == value || *p != '\0') + if (!safe_strtoint(value, option->data)) return 0; return 1; case WESTON_OPTION_UNSIGNED_INTEGER: diff --git a/tests/string-test.c b/tests/string-test.c new file mode 100644 index 0000000..a72ec30 --- /dev/null +++ b/tests/string-test.c @@ -0,0 +1,87 @@ +/* + * Copyright © 2016 Samsung Electronics Co., Ltd + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial + * portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "config.h" + +#include <stdlib.h> +#include <stdbool.h> +#include <stdio.h> +#include <assert.h> + +#include "shared/string-helpers.h" + +#include "weston-test-client-helper.h" + +TEST(strtol_conversions) +{ + bool ret; + int32_t val = -1; + char *str = NULL; + + str = ""; val = -1; + ret = safe_strtoint(str, &val); + assert(ret == false); + assert(val == -1); + + str = "."; val = -1; + ret = safe_strtoint(str, &val); + assert(ret == false); + assert(val == -1); + + str = "42"; val = -1; + ret = safe_strtoint(str, &val); + assert(ret == true); + assert(val == 42); + + str = "-42"; val = -1; + ret = safe_strtoint(str, &val); + assert(ret == true); + assert(val == -42); + + str = "0042"; val = -1; + ret = safe_strtoint(str, &val); + assert(ret == true); + assert(val == 42); + + str = "x42"; val = -1; + ret = safe_strtoint(str, &val); + assert(ret == false); + assert(val == -1); + + str = "42x"; val = -1; + ret = safe_strtoint(str, &val); + assert(ret == false); + assert(val == -1); + + str = "0x42424242"; val = -1; + ret = safe_strtoint(str, &val); + assert(ret == false); + assert(val == -1); + + str = "424748364789L"; val = -1; + ret = safe_strtoint(str, &val); + assert(ret == false); + assert(val == -1); +} diff --git a/xwayland/launcher.c b/xwayland/launcher.c index a83784c..8972319 100644 --- a/xwayland/launcher.c +++ b/xwayland/launcher.c @@ -39,6 +39,7 @@ #include "xwayland.h" #include "xwayland-api.h" #include "shared/helpers.h" +#include "shared/string-helpers.h" #include "compositor/weston.h" static int @@ -147,7 +148,7 @@ bind_to_unix_socket(int display) static int create_lockfile(int display, char *lockfile, size_t lsize) { - char pid[16], *end; + char pid[16]; int fd, size; pid_t other; @@ -165,9 +166,7 @@ create_lockfile(int display, char *lockfile, size_t lsize) return -1; } - errno = 0; - other = strtol(pid, &end, 10); - if (errno != 0 || end == pid || *end != '\0') { + if (!safe_strtoint(pid, &other)) { weston_log("can't parse lock file %s\n", lockfile); close(fd); -- 1.9.1 _______________________________________________ wayland-devel mailing list wayland-devel@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/wayland-devel