Fix integer overflow issue
[platform/upstream/libxkbcommon.git] / tools / interactive-wayland.c
index 8cc41de..4c11e5c 100644 (file)
 #include "xdg-shell-client-protocol.h"
 #include <wayland-util.h>
 
-#define MAX(a, b) ((a) > (b) ? (a) : (b))
+#define MIN(a, b) ((a) < (b) ? (a) : (b))
+
+/* Offset between evdev keycodes (where KEY_ESCAPE is 1), and the evdev XKB
+ * keycode set (where ESC is 9). */
+#define EVDEV_OFFSET 8
 
 struct interactive_dpy {
     struct wl_display *dpy;
@@ -130,6 +134,27 @@ create_tmpfile_cloexec(char *tmpname)
 }
 #endif
 
+static int
+os_resize_anonymous_file(int fd, off_t size)
+{
+    int ret;
+#ifdef HAVE_POSIX_FALLOCATE
+    ret = posix_fallocate(fd, 0, size);
+    if (ret == 0)
+        return 0;
+    /*
+     * Filesystems that do support fallocate will return EINVAL
+     * or EOPNOTSUPP, fallback to ftruncate() then.
+     */
+    if (ret != EINVAL && ret != EOPNOTSUPP)
+        return ret;
+#endif
+    ret = ftruncate(fd, size);
+    if (ret != 0)
+        return errno;
+    return 0;
+}
+
 /*
  * Create a new, unique, anonymous file of the given size, and
  * return the file descriptor for it. The file descriptor is set
@@ -148,8 +173,8 @@ create_tmpfile_cloexec(char *tmpname)
  * If the C library implements posix_fallocate(), it is used to
  * guarantee that disk space is available for the file at the
  * given size. If disk space is insufficent, errno is set to ENOSPC.
- * If posix_fallocate() is not supported, program may receive
- * SIGBUS on accessing mmap()'ed file contents instead.
+ * If posix_fallocate() is not supported, program will fallback
+ * to ftruncate() instead.
  */
 static int
 os_create_anonymous_file(off_t size)
@@ -180,20 +205,12 @@ os_create_anonymous_file(off_t size)
     if (fd < 0)
         return -1;
 
-#ifdef HAVE_POSIX_FALLOCATE
-    ret = posix_fallocate(fd, 0, size);
+    ret = os_resize_anonymous_file(fd, size);
     if (ret != 0) {
         close(fd);
         errno = ret;
         return -1;
     }
-#else
-    ret = ftruncate(fd, size);
-    if (ret < 0) {
-        close(fd);
-        return -1;
-    }
-#endif
 
     return fd;
 }
@@ -382,11 +399,12 @@ kbd_key(void *data, struct wl_keyboard *wl_kbd, uint32_t serial, uint32_t time,
         return;
 
     printf("%s: ", seat->name_str);
-    tools_print_keycode_state(seat->state, NULL, key + 8,
-                              XKB_CONSUMED_MODE_XKB);
+    tools_print_keycode_state(seat->state, NULL, key + EVDEV_OFFSET,
+                              XKB_CONSUMED_MODE_XKB,
+                              PRINT_ALL_FIELDS);
 
     /* Exit on ESC. */
-    if (xkb_state_key_get_one_sym(seat->state, key + 8) == XKB_KEY_Escape)
+    if (xkb_state_key_get_one_sym(seat->state, key + EVDEV_OFFSET) == XKB_KEY_Escape)
         terminate = true;
 }
 
@@ -544,7 +562,7 @@ seat_create(struct interactive_dpy *inter, struct wl_registry *registry,
     seat->global_name = name;
     seat->inter = inter;
     seat->wl_seat = wl_registry_bind(registry, name, &wl_seat_interface,
-                                     MAX(version, 5));
+                                     MIN(version, 5));
     wl_seat_add_listener(seat->wl_seat, &seat_listener, seat);
     ret = asprintf(&seat->name_str, "seat:%d",
                    wl_proxy_get_id((struct wl_proxy *) seat->wl_seat));
@@ -594,17 +612,17 @@ registry_global(void *data, struct wl_registry *registry, uint32_t name,
     else if (strcmp(interface, "xdg_wm_base") == 0) {
         inter->shell = wl_registry_bind(registry, name,
                                         &xdg_wm_base_interface,
-                                        MAX(version, 2));
+                                        MIN(version, 2));
         xdg_wm_base_add_listener(inter->shell, &shell_listener, inter);
     }
     else if (strcmp(interface, "wl_compositor") == 0) {
         inter->compositor = wl_registry_bind(registry, name,
                                              &wl_compositor_interface,
-                                             MAX(version, 1));
+                                             MIN(version, 1));
     }
     else if (strcmp(interface, "wl_shm") == 0) {
         inter->shm = wl_registry_bind(registry, name, &wl_shm_interface,
-                                      MAX(version, 1));
+                                      MIN(version, 1));
     }
 }
 
@@ -662,6 +680,14 @@ main(int argc, char *argv[])
     struct interactive_dpy inter;
     struct wl_registry *registry;
 
+    if (argc != 1) {
+        ret = strcmp(argv[1], "--help");
+        fprintf(ret ? stderr : stdout, "Usage: %s [--help]\n", argv[0]);
+        if (ret)
+            fprintf(stderr, "unrecognized option: %s\n", argv[1]);
+        return ret ? EXIT_INVALID_USAGE : EXIT_SUCCESS;
+    }
+
     setlocale(LC_ALL, "");
 
     memset(&inter, 0, sizeof(inter));