Belts and suspenders: make sure outgoing log messages don't contain * potentially suspicious characters, such as terminal control codes. * * This escapes control characters except newline ('\n') in C syntax. * It escapes instead of removes them to still allow for troubleshooting * issues where they accidentally end up in strings. */
| 278 | * issues where they accidentally end up in strings. |
| 279 | */ |
| 280 | std::string LogEscapeMessage(const std::string& str) { |
| 281 | std::string ret; |
| 282 | for (char ch_in : str) { |
| 283 | uint8_t ch = (uint8_t)ch_in; |
| 284 | if ((ch >= 32 || ch == '\n') && ch != '\x7f') { |
| 285 | ret += ch_in; |
| 286 | } else { |
| 287 | ret += strprintf("\\x%02x", ch); |
| 288 | } |
| 289 | } |
| 290 | return ret; |
| 291 | } |
| 292 | |
| 293 | } |
| 294 |