From 2ad25b1460400e66ea26bd6489b04072be7d9dbb Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Sun, 18 Sep 2022 15:20:39 +0200 Subject: [PATCH] output: fix wlr_output_set_gamma() with zero size This is documented to reset the gamma LUT, but we don't handle this properly. While at it, make sure we leave wlr_output.pending in a good state on allocation failure. --- types/output/output.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/types/output/output.c b/types/output/output.c index a4cb8980..cd0cc934 100644 --- a/types/output/output.c +++ b/types/output/output.c @@ -925,18 +925,22 @@ void wlr_output_send_present(struct wlr_output *output, void wlr_output_set_gamma(struct wlr_output *output, size_t size, const uint16_t *r, const uint16_t *g, const uint16_t *b) { + uint16_t *gamma_lut = NULL; + if (size > 0) { + gamma_lut = malloc(3 * size * sizeof(uint16_t)); + if (gamma_lut == NULL) { + wlr_log_errno(WLR_ERROR, "Allocation failed"); + return; + } + memcpy(gamma_lut, r, size * sizeof(uint16_t)); + memcpy(gamma_lut + size, g, size * sizeof(uint16_t)); + memcpy(gamma_lut + 2 * size, b, size * sizeof(uint16_t)); + } + output_state_clear_gamma_lut(&output->pending); output->pending.gamma_lut_size = size; - output->pending.gamma_lut = malloc(3 * size * sizeof(uint16_t)); - if (output->pending.gamma_lut == NULL) { - wlr_log_errno(WLR_ERROR, "Allocation failed"); - return; - } - memcpy(output->pending.gamma_lut, r, size * sizeof(uint16_t)); - memcpy(output->pending.gamma_lut + size, g, size * sizeof(uint16_t)); - memcpy(output->pending.gamma_lut + 2 * size, b, size * sizeof(uint16_t)); - + output->pending.gamma_lut = gamma_lut; output->pending.committed |= WLR_OUTPUT_STATE_GAMMA_LUT; }