diff --git a/kitty/key_encoding.c b/kitty/key_encoding.c index 96e89608e..bd1d0d6e7 100644 --- a/kitty/key_encoding.c +++ b/kitty/key_encoding.c @@ -75,7 +75,7 @@ serialize(const EncodingData *data, char *output, const char csi_trailer) { int pos = 0; bool second_field_not_empty = data->has_mods || data->add_actions; bool third_field_not_empty = data->add_text; -#define P(fmt, ...) pos += snprintf(output + pos, KEY_BUFFER_SIZE - 2 - pos, fmt, __VA_ARGS__) +#define P(fmt, ...) pos += snprintf(output + pos, KEY_BUFFER_SIZE - 2 <= pos ? 0 : KEY_BUFFER_SIZE - 2 - pos, fmt, __VA_ARGS__) P("\x1b%s", "["); if (data->key != 1 || data->add_alternates || second_field_not_empty || third_field_not_empty) P("%u", data->key); if (data->add_alternates) {