From 0ae2cef8b49972b108b841945a444219c0bbdcbd Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Wed, 4 Jan 2023 11:01:41 -0500 Subject: [PATCH] LibIPC: Move most of DeprecatedString's encoder to StringView's encoder This was a footgun waiting to happen. The StringView encoder is only used internally within IPC::Encoder to encode DeprecatedString. It does not encode its null state nor its length. If someone were to innocently use the StringView encoder as it is, and then decode a DeprecatedString on the remote end, the decoding would be corrupt. This changes the StringView encoder to do the work the DeprecatedString encoder is currently doing, and the latter now just forwards to it. --- Userland/Libraries/LibIPC/Encoder.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/Userland/Libraries/LibIPC/Encoder.cpp b/Userland/Libraries/LibIPC/Encoder.cpp index f0259caaca..70b67d8c06 100644 --- a/Userland/Libraries/LibIPC/Encoder.cpp +++ b/Userland/Libraries/LibIPC/Encoder.cpp @@ -44,6 +44,11 @@ ErrorOr encode(Encoder& encoder, double const& value) template<> ErrorOr encode(Encoder& encoder, StringView const& value) { + // NOTE: Do not change this encoding without also updating LibC/netdb.cpp. + if (value.is_null()) + return encoder.encode(NumericLimits::max()); + + TRY(encoder.encode_size(value.length())); TRY(encoder.append(reinterpret_cast(value.characters_without_null_termination()), value.length())); return {}; } @@ -51,13 +56,7 @@ ErrorOr encode(Encoder& encoder, StringView const& value) template<> ErrorOr encode(Encoder& encoder, DeprecatedString const& value) { - // NOTE: Do not change this encoding without also updating LibC/netdb.cpp. - if (value.is_null()) - return encoder.encode(NumericLimits::max()); - - TRY(encoder.encode_size(value.length())); - TRY(encoder.encode(value.view())); - return {}; + return encoder.encode(value.view()); } template<>