From e685bf38f9e3511f21159a61a1c16e169dc70b45 Mon Sep 17 00:00:00 2001 From: Demi Marie Obenour Date: Sun, 25 Dec 2022 18:37:41 -0500 Subject: [PATCH] Add fast path for ASCII No functional change intended. --- qrexec-lib/unpack.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/qrexec-lib/unpack.c b/qrexec-lib/unpack.c index 704f66e4..d74569d7 100644 --- a/qrexec-lib/unpack.c +++ b/qrexec-lib/unpack.c @@ -194,14 +194,12 @@ static int validate_utf8_char(const unsigned char *untrusted_c) { * - Surrogates and some values above 0x10FFFF are accepted here, but are * rejected as forbidden code points later. */ - - if (*untrusted_c >= 0x20 && *untrusted_c < 0x7F) { - return 1; // non-control ASCII - } else if (*untrusted_c >= 0xC2 && *untrusted_c <= 0xDF) { - total_size = 2; - tails_count = 1; - code_point = *untrusted_c & 0x1F; - } else switch (*untrusted_c) { + switch (*untrusted_c) { + case 0xC2 ... 0xDF: + total_size = 2; + tails_count = 1; + code_point = *untrusted_c & 0x1F; + break; case 0xE0: untrusted_c++; total_size = 3; @@ -279,11 +277,15 @@ static size_t validate_path(const char *const untrusted_name, size_t allowed_lea break; } } - int utf8_ret = validate_utf8_char((const unsigned char *)(untrusted_name + i)); - if (utf8_ret > 0) { - i += utf8_ret; + if (untrusted_name[i] >= 0x20 && untrusted_name[i] <= 0x7E) { + i++; } else { - do_exit(EILSEQ, untrusted_name); // not valid UTF-8 + int utf8_ret = validate_utf8_char((const unsigned char *)(untrusted_name + i)); + if (utf8_ret > 0) { + i += utf8_ret; + } else { + do_exit(EILSEQ, untrusted_name); // not valid UTF-8 + } } } while (untrusted_name[i]); return non_dotdot_components;