From ab6bf5f4f75e9aa16e71a5fed7c03f1ac8ce2762 Mon Sep 17 00:00:00 2001 From: rdb Date: Fri, 21 Aug 2020 14:51:42 +0200 Subject: [PATCH] general: Add workaround for Windows bug in various uses of isprint() See #994; there is a regression in certain versions of the Windows CRT that gives the wrong result for isprint(). This adds workarounds to various potentially affected locations where isprint() is being used. --- direct/src/dcparser/dcPacker.cxx | 2 +- direct/src/plugin/p3dStringObject.cxx | 5 +++++ dtool/src/interrogate/interfaceMakerPythonNative.cxx | 4 ++++ dtool/src/prckeys/makePrcKey.cxx | 9 ++++++--- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/direct/src/dcparser/dcPacker.cxx b/direct/src/dcparser/dcPacker.cxx index b6dbd60855..810744c2ab 100644 --- a/direct/src/dcparser/dcPacker.cxx +++ b/direct/src/dcparser/dcPacker.cxx @@ -1102,7 +1102,7 @@ enquote_string(ostream &out, char quote_mark, const string &str) { if ((*pi) == quote_mark || (*pi) == '\\') { out << '\\' << (*pi); - } else if (!isprint(*pi)) { + } else if (!isprint(*pi) || (*pi) == '\t') { char buffer[10]; sprintf(buffer, "%02x", (unsigned char)(*pi)); out << "\\x" << buffer; diff --git a/direct/src/plugin/p3dStringObject.cxx b/direct/src/plugin/p3dStringObject.cxx index 8d87a2dc8c..4cd1d1f513 100644 --- a/direct/src/plugin/p3dStringObject.cxx +++ b/direct/src/plugin/p3dStringObject.cxx @@ -83,6 +83,11 @@ output(std::ostream &out) { out << "\\\x22"; break; + case '\t': + // Certain versions of the Windows CRT classify tab as printable. + out << "\\t"; + break; + default: out << *si; } diff --git a/dtool/src/interrogate/interfaceMakerPythonNative.cxx b/dtool/src/interrogate/interfaceMakerPythonNative.cxx index 40572007fc..2a90a4b1db 100644 --- a/dtool/src/interrogate/interfaceMakerPythonNative.cxx +++ b/dtool/src/interrogate/interfaceMakerPythonNative.cxx @@ -8019,6 +8019,10 @@ output_quoted(ostream &out, int indent_level, const std::string &str, << '"'; continue; + case '\t': + out << "\\t"; + break; + default: if (!isprint(*si)) { out << "\\" << oct << std::setw(3) << std::setfill('0') << (unsigned int)(*si) diff --git a/dtool/src/prckeys/makePrcKey.cxx b/dtool/src/prckeys/makePrcKey.cxx index 23bf0914b0..fea188c87c 100644 --- a/dtool/src/prckeys/makePrcKey.cxx +++ b/dtool/src/prckeys/makePrcKey.cxx @@ -93,10 +93,13 @@ output_c_string(std::ostream &out, const string &string_name, last_nl = false; } - if (isprint(data_ptr[i])) { + if (data_ptr[i] == '\t') { + out << "\\t"; + } + else if (isprint(data_ptr[i])) { out << data_ptr[i]; - - } else { + } + else { out << "\\x" << std::hex << std::setw(2) << std::setfill('0') << (unsigned int)(unsigned char)data_ptr[i] << std::dec; }