Fix new GCC warning when building ivlpp.

Using the old buffer pointer value after a realloc to adjust the pointers
into that buffer is technically undefined behaviour.
This commit is contained in:
Martin Whitaker 2024-02-09 11:40:20 +00:00
parent 2948e9a89f
commit 855dd3a7e6
1 changed files with 7 additions and 5 deletions

View File

@ -1,7 +1,7 @@
%option prefix="yy" %option prefix="yy"
%{ %{
/* /*
* Copyright (c) 1999-2023 Stephen Williams (steve@icarus.com) * Copyright (c) 1999-2024 Stephen Williams (steve@icarus.com)
* *
* This source code is free software; you can redistribute it * This source code is free software; you can redistribute it
* and/or modify it in source code form under the terms of the GNU * and/or modify it in source code form under the terms of the GNU
@ -1407,14 +1407,16 @@ static void do_define(void)
added_cnt += 2 - argl; added_cnt += 2 - argl;
if (added_cnt > 0) { if (added_cnt > 0) {
char* base = define_text; size_t head_idx = head - define_text;
size_t tail_idx = tail - define_text;
size_t cp_idx = cp - define_text;
define_cnt += added_cnt; define_cnt += added_cnt;
define_text = realloc(define_text, define_cnt + 1); define_text = realloc(define_text, define_cnt + 1);
head = &define_text[head - base]; head = &define_text[head_idx];
tail = &define_text[tail - base]; tail = &define_text[tail_idx];
cp = &define_text[cp - base]; cp = &define_text[cp_idx];
added_cnt = 0; added_cnt = 0;
} }