From: Matthew Fernandez Date: Sat, 10 Dec 2022 20:54:40 +0000 (-0800) Subject: common printTok: remove use of 'agxbstart' to access agxbuf data X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1349b895eca0bb04855ba46162d0ec7ba3c65010;p=graphviz common printTok: remove use of 'agxbstart' to access agxbuf data Reaching into something that is notionally the internals of the `agxbuf` type was causing some complications in upcoming changes. Specifically, the lifetime of the pointer returned from `agxbstart` makes it error prone. Any operation that appends more data to the `agxbuf` must conservatively be assumed to invalidate a pointer previously returned from `agxbstart`. Although this instance was not retaining the returned pointer, the `agxbstart` interface seems dangerous to provide given that it is difficult to use safely. By removing its sole use here, we clear the way for its removal in future. This new phrasing is potentially less efficient, as data is now extracted from the buffer and then written back into the same buffer. However (1) a good contemporary compiler will inline all calls and realize the accumulated operation on the buffer is a no-op and (2) this is debug code, so performance is not critical. Gitlab: related to #2325 --- diff --git a/lib/common/htmllex.c b/lib/common/htmllex.c index 5fb76ac1c..ca3ed3b60 100644 --- a/lib/common/htmllex.c +++ b/lib/common/htmllex.c @@ -1032,9 +1032,9 @@ static void printTok(int tok) s = ""; } if (tok == T_string) { - fprintf(stderr, "%s \"", s); - fwrite(agxbstart(state.xb), 1, agxblen(state.xb), stderr); - fprintf(stderr, "\"\n"); + const char *token_text = agxbuse(state.xb); + fprintf(stderr, "%s \"%s\"\n", s, token_text); + agxbput(state.xb, token_text); } else fprintf(stderr, "%s\n", s); }