mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 18:27:35 +00:00
LibIPC+LibC: Add and use a helper to encode/decoder container sizes
While refactoring the IPC encoders and decoders for fallibility, the inconsistency in which we transfer container sizes was a frequent thing to trip over. We currently transfer sizes as any of i32, u32, and u64. This adds a helper to transfer sizes in one consistent way. Two special cases here are DeprecatedString and Vector, whose encoding is depended upon by netdb, so that is also updated here.
This commit is contained in:
parent
40165f5846
commit
7c6b5ed161
5 changed files with 48 additions and 35 deletions
|
@ -50,6 +50,8 @@ public:
|
|||
return {};
|
||||
}
|
||||
|
||||
ErrorOr<size_t> decode_size();
|
||||
|
||||
Core::Stream::LocalSocket& socket() { return m_socket; }
|
||||
|
||||
private:
|
||||
|
@ -96,11 +98,9 @@ ErrorOr<Empty> decode(Decoder&);
|
|||
template<Concepts::Vector T>
|
||||
ErrorOr<T> decode(Decoder& decoder)
|
||||
{
|
||||
auto size = TRY(decoder.decode<u64>());
|
||||
if (size > NumericLimits<i32>::max())
|
||||
return Error::from_string_literal("IPC: Invalid Vector size");
|
||||
|
||||
T vector;
|
||||
|
||||
auto size = TRY(decoder.decode_size());
|
||||
TRY(vector.try_ensure_capacity(size));
|
||||
|
||||
for (size_t i = 0; i < size; ++i) {
|
||||
|
@ -114,12 +114,11 @@ ErrorOr<T> decode(Decoder& decoder)
|
|||
template<Concepts::HashMap T>
|
||||
ErrorOr<T> decode(Decoder& decoder)
|
||||
{
|
||||
auto size = TRY(decoder.decode<u32>());
|
||||
if (size > NumericLimits<i32>::max())
|
||||
return Error::from_string_literal("IPC: Invalid HashMap size");
|
||||
|
||||
T hashmap;
|
||||
|
||||
auto size = TRY(decoder.decode_size());
|
||||
TRY(hashmap.try_ensure_capacity(size));
|
||||
|
||||
for (size_t i = 0; i < size; ++i) {
|
||||
auto key = TRY(decoder.decode<typename T::KeyType>());
|
||||
auto value = TRY(decoder.decode<typename T::ValueType>());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue