Skip to content

Commit 49e74ba

Browse files
committed
samples: input: draw_touch_events: avoid avoid OOB writes
When CROSS_DIM doesn’t evenly divide the panel WIDTH/HEIGHT, the last tile on the right/bottom edge can extend past the display bounds. Fix by clipping the edge tiles. Signed-off-by: Charles Dias <charlesdias.cd@outlook.com>
1 parent 2a09063 commit 49e74ba

File tree

1 file changed

+17
-1
lines changed
  • samples/subsys/input/draw_touch_events/src

1 file changed

+17
-1
lines changed

samples/subsys/input/draw_touch_events/src/main.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,14 @@ static void clear_screen(void)
7070

7171
for (x = 0; x < WIDTH; x += CROSS_DIM) {
7272
for (y = 0; y < HEIGHT; y += CROSS_DIM) {
73-
display_write(display_dev, x, y, &buf_desc, buffer_cross_empty);
73+
struct display_buffer_descriptor ddesc = buf_desc;
74+
uint16_t rem_w = WIDTH - x;
75+
uint16_t rem_h = HEIGHT - y;
76+
ddesc.width = MIN(buf_desc.width, rem_w);
77+
ddesc.height = MIN(buf_desc.height, rem_h);
78+
ddesc.buf_size = ddesc.width * ddesc.height * BPP;
79+
80+
display_write(display_dev, x, y, &ddesc, buffer_cross_empty);
7481
}
7582
}
7683
}
@@ -126,6 +133,15 @@ int main(void)
126133
LOG_ERR("Unsupported BPP=%d", BPP);
127134
return 0;
128135
}
136+
137+
/* Ensure driver pixel format matches our buffer layout from DT */
138+
int rc = display_set_pixel_format(display_dev, PIXEL_FORMAT);
139+
140+
if (rc < 0) {
141+
LOG_WRN("display_set_pixel_format(%d) failed: %d, continuing with driver default",
142+
PIXEL_FORMAT, rc);
143+
}
144+
129145
fill_cross_buffer();
130146
display_blanking_off(display_dev);
131147

0 commit comments

Comments
 (0)