Standardize error checking for strtol calls

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@osg.samsung.com>
Reviewed-by: Eric Engestrom <eric.engestrom@imgtec.com>
Reviewed-by: Peter Hutterer <peter.hutterer@who-t.net>
dev
Bryce Harrington 8 years ago
parent 295e9d004e
commit 913d7c15f7
  1. 3
      compositor/main.c
  2. 2
      compositor/systemd-notify.c
  3. 3
      libweston/compositor.c
  4. 10
      libweston/libbacklight.c

@ -1699,8 +1699,9 @@ int main(int argc, char *argv[])
server_socket = getenv("WAYLAND_SERVER_SOCKET"); server_socket = getenv("WAYLAND_SERVER_SOCKET");
if (server_socket) { if (server_socket) {
weston_log("Running with single client\n"); weston_log("Running with single client\n");
errno = 0;
fd = strtol(server_socket, &end, 10); fd = strtol(server_socket, &end, 10);
if (*end != '\0') if (errno != 0 || end == server_socket || *end != '\0')
fd = -1; fd = -1;
} else { } else {
fd = -1; fd = -1;

@ -146,7 +146,7 @@ module_init(struct weston_compositor *compositor,
errno = 0; errno = 0;
watchdog_time_conv = strtol(watchdog_time_env, &tail, 10); 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; return 0;
/* Convert 'WATCHDOG_USEC' to milliseconds and notify /* Convert 'WATCHDOG_USEC' to milliseconds and notify

@ -4628,8 +4628,9 @@ weston_environment_get_fd(const char *env)
e = getenv(env); e = getenv(env);
if (!e) if (!e)
return -1; return -1;
errno = 0;
fd = strtol(e, &end, 10); fd = strtol(e, &end, 10);
if (*end != '\0') if (errno != 0 || end == e || *end != '\0')
return -1; return -1;
flags = fcntl(fd, F_GETFD); flags = fcntl(fd, F_GETFD);

@ -48,6 +48,7 @@ static long backlight_get(struct backlight *backlight, char *node)
{ {
char buffer[100]; char buffer[100];
char *path; char *path;
char *end;
int fd; int fd;
long value, ret; long value, ret;
@ -65,8 +66,15 @@ static long backlight_get(struct backlight *backlight, char *node)
goto out; 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; ret = value;
out: out:
if (fd >= 0) if (fd >= 0)
close(fd); close(fd);

Loading…
Cancel
Save