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
s = "<unknown>";
}
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);
}