[PATCH weston 1/3] Standardize error checking for strtol calls

Bryce Harrington bryce at osg.samsung.com
Wed Jul 13 04:16:31 UTC 2016


This tightens up the strtol() error checking in several places where it
is used for parsing environment variables, and in the backlight
interface that is reading numbers from files under /sys/class/backlight.
All of these uses are expecting strings containing decimal numbers and
nothing else, so the error checking can all be tightened up and made
consistent with other strtol() calls.

This follows the error checking style used in Wayland
(c.f. wayland-client.c and scanner.c) and c.f. commit cbc05378.

Signed-off-by: Bryce Harrington <bryce at osg.samsung.com>
---
 compositor/main.c           |  3 ++-
 compositor/systemd-notify.c |  2 +-
 libweston/compositor.c      |  3 ++-
 libweston/libbacklight.c    | 10 +++++++++-
 4 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/compositor/main.c b/compositor/main.c
index 1f75ae0..27be973 100644
--- a/compositor/main.c
+++ b/compositor/main.c
@@ -1684,8 +1684,9 @@ 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 (*end != '\0')
+		if (errno != 0 || end == server_socket || *end != '\0')
 			fd = -1;
 	} else {
 		fd = -1;
diff --git a/compositor/systemd-notify.c b/compositor/systemd-notify.c
index 9fbd5ee..6104124 100644
--- a/compositor/systemd-notify.c
+++ b/compositor/systemd-notify.c
@@ -146,7 +146,7 @@ module_init(struct weston_compositor *compositor,
 
 	errno = 0;
 	watchdog_time_conv = strtol(watchdog_time_env, &tail, 10);
-	if ((errno != 0) || (*tail != '\0'))
+	if (errno != 0 || tail == watchdog_time_env || *tail != '\0')
 		return 0;
 
 	/* Convert 'WATCHDOG_USEC' to milliseconds and notify
diff --git a/libweston/compositor.c b/libweston/compositor.c
index 96eeb17..67f334c 100644
--- a/libweston/compositor.c
+++ b/libweston/compositor.c
@@ -4617,8 +4617,9 @@ weston_environment_get_fd(const char *env)
 	e = getenv(env);
 	if (!e)
 		return -1;
+	errno = 0;
 	fd = strtol(e, &end, 10);
-	if (*end != '\0')
+	if (errno != 0 || end == e || *end != '\0')
 		return -1;
 
 	flags = fcntl(fd, F_GETFD);
diff --git a/libweston/libbacklight.c b/libweston/libbacklight.c
index 722d66f..f1205e8 100644
--- a/libweston/libbacklight.c
+++ b/libweston/libbacklight.c
@@ -47,6 +47,7 @@ static long backlight_get(struct backlight *backlight, char *node)
 {
 	char buffer[100];
 	char *path;
+	char *end;
 	int fd;
 	long value, ret;
 
@@ -64,8 +65,15 @@ static long backlight_get(struct backlight *backlight, char *node)
 		goto out;
 	}
 
-	value = strtol(buffer, NULL, 10);
+	errno = 0;
+	value = strtol(buffer, &end, 10);
+	if (errno != 0 || end == buffer || *end != '\0') {
+		ret = -1;
+		goto out;
+	}
+
 	ret = value;
+
 out:
 	if (fd >= 0)
 		close(fd);
-- 
1.9.1



More information about the wayland-devel mailing list