Skip to content

Commit

Permalink
platsch: fix sign compare for int/unsigned
Browse files Browse the repository at this point in the history
The sizeof operator will return a long unsigned int which raises a sign
compare issue when compared to int. Also fix similar issues where we
compare an int with an unsigned int by casting the unsigned int to int
using INT_MAX as the limit.

Signed-off-by: Rouven Czerwinski <[email protected]>
  • Loading branch information
Emantor committed Jul 19, 2024
1 parent e898f13 commit 29b89a5
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions platsch.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <limits.h>

#include <xf86drm.h>
#include <xf86drmMode.h>
Expand Down Expand Up @@ -124,7 +125,7 @@ void draw_buffer(struct modeset_dev *dev, const char *dir, const char *base)
ret = snprintf(filename, sizeof(filename),
"%s/%s-%ux%u-%s.bin",
dir, base, dev->width, dev->height, dev->format->name);
if (ret >= sizeof(filename)) {
if (ret >= ((int)sizeof(filename) & INT_MAX)) {
error("Failed to fit filename into buffer\n");
return;
}
Expand Down Expand Up @@ -183,7 +184,7 @@ static int drmprepare_crtc(int fd, drmModeRes *res, drmModeConnector *conn,
assert(crtc_id >= 0);

for (iter = modeset_list; iter; iter = iter->next) {
if (iter->crtc_id == crtc_id) {
if (((int)iter->crtc_id & INT_MAX) == crtc_id) {
crtc_id = -1;
break;
}
Expand Down Expand Up @@ -229,7 +230,7 @@ static int drmprepare_crtc(int fd, drmModeRes *res, drmModeConnector *conn,
/* check that no other device already uses this CRTC */
crtc_id = res->crtcs[j];
for (iter = modeset_list; iter; iter = iter->next) {
if (iter->crtc_id == crtc_id) {
if (((int)iter->crtc_id & INT_MAX) == crtc_id) {
crtc_id = -1;
break;
}
Expand Down Expand Up @@ -375,7 +376,7 @@ static int set_env_connector_mode(drmModeConnector *conn,
ret = snprintf(mode_env_name, sizeof(mode_env_name), "platsch_%s%u_mode",
connector_type_name, conn->connector_type_id);
free(connector_type_name);
if (ret >= sizeof(mode_env_name)) {
if (ret >= ((int)sizeof(mode_env_name) & INT_MAX)) {
error("failed to fit platsch env mode variable name into buffer\n");
return -EFAULT;
}
Expand Down Expand Up @@ -612,7 +613,7 @@ int main(int argc, char *argv[])
* XXX: Loop through drm devices to find one with connectors.
*/
ret = snprintf(drmdev, sizeof(drmdev), DRM_DEV_NAME, DRM_DIR_NAME, i);
if (ret >= sizeof(drmdev)) {
if (ret >= (int)sizeof(drmdev)) {
error("Huh, device name overflowed buffer\n");
goto execinit;
}
Expand Down

0 comments on commit 29b89a5

Please sign in to comment.