1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 13:27:35 +00:00

LibIPC: Allow opt-in UTF-8 validation on message parameters

You can now mark String message parameters with the [UTF8] attribute.
This will cause the generated decoder to perform UTF-8 validation and
reject the message if the given parameter is not a valid UTF-8 string.

This frees up the receiving side from having to do this validation at
a higher level.
This commit is contained in:
Andreas Kling 2020-05-16 14:11:35 +02:00
parent f7a75598bb
commit cd29844632
6 changed files with 39 additions and 16 deletions

View file

@ -35,6 +35,7 @@
//#define GENERATE_DEBUG_CODE
struct Parameter {
Vector<String> attributes;
String type;
String name;
};
@ -127,6 +128,23 @@ int main(int argc, char** argv)
consume_whitespace();
if (peek() == ')')
break;
if (peek() == '[') {
consume_one();
for (;;) {
if (peek() == ']') {
consume_one();
consume_whitespace();
break;
}
if (peek() == ',') {
consume_one();
consume_whitespace();
}
auto attribute = extract_while([](char ch) { return ch != ']' && ch != ','; });
parameter.attributes.append(attribute);
consume_whitespace();
}
}
parameter.type = extract_while([](char ch) { return !isspace(ch); });
consume_whitespace();
parameter.name = extract_while([](char ch) { return !isspace(ch) && ch != ',' && ch != ')'; });
@ -226,6 +244,7 @@ int main(int argc, char** argv)
out() << "#pragma once";
out() << "#include <AK/BufferStream.h>";
out() << "#include <AK/OwnPtr.h>";
out() << "#include <AK/Utf8View.h>";
out() << "#include <LibGfx/Color.h>";
out() << "#include <LibGfx/Rect.h>";
out() << "#include <LibGfx/ShareableBitmap.h>";
@ -312,6 +331,10 @@ int main(int argc, char** argv)
out() << " " << parameter.type << " " << parameter.name << " = " << initial_value << ";";
out() << " if (!decoder.decode(" << parameter.name << "))";
out() << " return nullptr;";
if (parameter.attributes.contains_slow("UTF8")) {
out() << " if (!Utf8View(" << parameter.name << ").validate())";
out() << " return nullptr;";
}
}
StringBuilder builder;