Make printTableAddCell/printTableAddHeader string argument const These functions would sometimes overwrite the string argument they are passed, namely via mbvalidate(), which removes invalid UTF-8 characters (or potentially analogously in other encodings, but that is not implemented). However, many callers are not expecting that. In many callers, the input value comes directly from libpq structures, such as from PQgetvalue() or PQsslAttribute(). The latter actually has a const char * return type, and that was cast away. But even the former is not expecting its return value to be modified.
Fix that by making these arguments const. Internally, we add a separate function that does only the checking part of mbvalidate(). Only if the validation returns a negative result, we make a copy and run mbvalidate() on the copy. printTableAddCell() already had internal infrastructure for keeping track of what values needed to be freed. We add the same for printTableAddHeader(). In passing, also simplify the code a bit. There were essentially duplicate mechanisms for keeping track of the most recently added cell (fields .cell and .cellsadded). Make that consistent by using an integer counter for everything. That makes the code arguably easier to read than with the "current pointer" approaches. Reviewed-by: Chao Li <[email protected]> Discussion: https://www.postgresql.org/message-id/flat/c855d318-4545-4dc7-b466-2e24fbf64725%40eisentraut.org Branch ------ master Details ------- https://git.postgresql.org/pg/commitdiff/c83a133f2bb531fe56284e35809c14eed1d8bd6e Modified Files -------------- src/bin/psql/command.c | 20 ++++----- src/fe_utils/mbprint.c | 33 ++++++++++++++ src/fe_utils/print.c | 97 +++++++++++++++++++++++++++++++++--------- src/include/fe_utils/mbprint.h | 1 + src/include/fe_utils/print.h | 11 +++-- 5 files changed, 125 insertions(+), 37 deletions(-)
