#include "wayland-private.h"
#include "wayland-os.h"
-#define DIV_ROUNDUP(n, a) ( ((n) + ((a) - 1)) / (a) )
+static inline uint32_t
+div_roundup(uint32_t n, size_t a)
+{
+ /* The cast to uint64_t is necessary to prevent overflow when rounding
+ * values close to UINT32_MAX. After the division it is again safe to
+ * cast back to uint32_t.
+ */
+ return (uint32_t) (((uint64_t) n + (a - 1)) / a);
+}
struct wl_buffer {
char data[4096];
break;
}
- next = p + DIV_ROUNDUP(length, sizeof *p);
+ next = p + div_roundup(length, sizeof *p);
if (next > end) {
wl_log("message too short, "
"object (%d), message %s(%s)\n",
case 'a':
length = *p++;
- next = p + DIV_ROUNDUP(length, sizeof *p);
+ next = p + div_roundup(length, sizeof *p);
if (next > end) {
wl_log("message too short, "
"object (%d), message %s(%s)\n",
}
size = strlen(closure->args[i].s) + 1;
- buffer_size += 1 + DIV_ROUNDUP(size, sizeof(uint32_t));
+ buffer_size += 1 + div_roundup(size, sizeof(uint32_t));
break;
case 'a':
if (closure->args[i].a == NULL) {
}
size = closure->args[i].a->size;
- buffer_size += (1 + DIV_ROUNDUP(size, sizeof(uint32_t)));
+ buffer_size += (1 + div_roundup(size, sizeof(uint32_t)));
break;
default:
break;
size = strlen(closure->args[i].s) + 1;
*p++ = size;
- if (p + DIV_ROUNDUP(size, sizeof *p) > end)
+ if (p + div_roundup(size, sizeof *p) > end)
goto overflow;
memcpy(p, closure->args[i].s, size);
- p += DIV_ROUNDUP(size, sizeof *p);
+ p += div_roundup(size, sizeof *p);
break;
case 'a':
if (closure->args[i].a == NULL) {
size = closure->args[i].a->size;
*p++ = size;
- if (p + DIV_ROUNDUP(size, sizeof *p) > end)
+ if (p + div_roundup(size, sizeof *p) > end)
goto overflow;
memcpy(p, closure->args[i].a->data, size);
- p += DIV_ROUNDUP(size, sizeof *p);
+ p += div_roundup(size, sizeof *p);
break;
default:
break;