Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 54 additions & 13 deletions services/tracer/TracerAdapterPrintf.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
#include "services/tracer/TracerAdapterPrintf.hpp"
#include "infra/util/Compatibility.hpp"
#include <cstring>

namespace
{
size_t GetStringLengthBounded(const char* str, int maxSize)
{
const char* end = static_cast<const char*>(std::memchr(str, 0, maxSize));
return end == nullptr ? maxSize : static_cast<size_t>(end - str);
}
}

namespace services
{
Expand All @@ -15,7 +25,11 @@ namespace services
for (; *format != 0; ++format)
{
if (*format == '%')
{
HandleFormat(format, args);
if (*format == '\0')
break;
}
else if (*format == '\n')
tracer.Trace();
else
Expand All @@ -28,8 +42,9 @@ namespace services
++format;

const auto width = ReadSize(format);
const auto precision = ReadPrecision(format, args);
const auto lengthSpecifier = ReadLength(format);
ParseFormat(*format, lengthSpecifier, width, args);
ParseFormat(*format, lengthSpecifier, width, precision, args);
}
Comment thread
heinwessels-philips marked this conversation as resolved.

int TracerAdapterPrintf::ReadLength(const char*& format) const
Expand All @@ -45,6 +60,29 @@ namespace services
return lengthSpecifier;
}

int TracerAdapterPrintf::ReadPrecision(const char*& format, va_list* args) const
{
if (*format != '.')
return -1;

++format;

if (*format == '*')
{
++format;
return va_arg(*args, int);
}

int precision = 0;
while (*format >= '0' && *format <= '9')
{
precision = (precision * 10) + (*format - '0');
++format;
}
Comment thread
heinwessels-philips marked this conversation as resolved.

return precision;
}

infra::Width TracerAdapterPrintf::ReadSize(const char*& format) const
{
infra::Width w(0);
Expand All @@ -55,24 +93,18 @@ namespace services
format++;
}

while (*format > '0' && *format <= '9')
{
w.width = w.width * 10 + *format - '0';
++format;
}

if (*format == '.')
while (*format >= '0' && *format <= '9')
{
w.width = (w.width * 10) + (*format - '0');
++format;
while (*format > '0' && *format <= '9')
++format;
}

return w;
}

void TracerAdapterPrintf::ParseFormat(char format, int lengthSpecifier, const infra::Width& width, va_list* args)
void TracerAdapterPrintf::ParseFormat(char format, int lengthSpecifier, const infra::Width& width, int precision, va_list* args)
{
// Note: precision is only supported for string currently.
switch (format)
{
case '\0':
Expand All @@ -85,8 +117,17 @@ namespace services
break;
case 's':
{
const auto* s = va_arg(*args, char*);
tracer.Continue() << (s != nullptr ? s : "(null)");
const auto* str = va_arg(*args, const char*);
if (str == nullptr)
tracer.Continue() << "(null)";
else if (precision >= 0)
{
auto boundedStringSize = GetStringLengthBounded(str, precision);
auto trimmedString = infra::BoundedConstString(str, boundedStringSize);
tracer.Continue() << trimmedString;
Comment thread
heinwessels-philips marked this conversation as resolved.
}
Comment thread
heinwessels-philips marked this conversation as resolved.
else
tracer.Continue() << str;
break;
}
case 'd':
Expand Down
5 changes: 3 additions & 2 deletions services/tracer/TracerAdapterPrintf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ namespace services

private:
void HandleFormat(const char*& format, va_list* args);
int ReadLength(const char*& format) const;
infra::Width ReadSize(const char*& format) const;
void ParseFormat(char format, int lengthSpecifier, const infra::Width& width, va_list* args);
int ReadPrecision(const char*& format, va_list* args) const;
int ReadLength(const char*& format) const;
void ParseFormat(char format, int lengthSpecifier, const infra::Width& width, int precision, va_list* args);

private:
services::Tracer& tracer;
Expand Down
81 changes: 81 additions & 0 deletions services/tracer/test/TestTracerAdapterPrintf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,24 @@ TEST_F(TracerAdapterPrintfTest, print_int_in_hex_with_width_to_tracer)
EXPECT_EQ(" abcd", stream.Storage());
}

TEST_F(TracerAdapterPrintfTest, print_int_in_hex_with_width_containing_zero_to_tracer)
{
Print("%10x", 0xABCD);
EXPECT_EQ(" abcd", stream.Storage());
}

TEST_F(TracerAdapterPrintfTest, print_int_in_hex_with_width_padding_0_to_tracer)
{
Print("%08x", 0xABCD);
EXPECT_EQ("0000abcd", stream.Storage());
}

TEST_F(TracerAdapterPrintfTest, print_long_long_in_hex_with_width_padding_0_to_tracer)
{
Print("%016llx", 0xABCDLL);
EXPECT_EQ("000000000000abcd", stream.Storage());
}

TEST_F(TracerAdapterPrintfTest, print_int_in_hex_with_X_to_tracer)
{
Print("%X", 0x123A);
Expand All @@ -138,3 +150,72 @@ TEST_F(TracerAdapterPrintfTest, print_multiple_values)
Print("%u%c%s", 100, '2', "300");
EXPECT_EQ("1002300", stream.Storage());
}

TEST_F(TracerAdapterPrintfTest, print_string_with_dynamic_precision)
{
const char* str = "No admittance except on party business";
Print("%.*s", 13, str);
EXPECT_EQ("No admittance", stream.Storage());
}

TEST_F(TracerAdapterPrintfTest, print_string_with_static_precision)
{
Print("%.5s", "Hello, Tracer!");
EXPECT_EQ("Hello", stream.Storage());
}

TEST_F(TracerAdapterPrintfTest, print_string_with_zero_precision)
{
Print("%.0s", "Hello, Tracer!");
EXPECT_EQ("", stream.Storage());
}

TEST_F(TracerAdapterPrintfTest, print_string_with_dynamic_zero_precision)
{
Print("%.*s", 0, "Hello, Tracer!");
EXPECT_EQ("", stream.Storage());
}

TEST_F(TracerAdapterPrintfTest, print_string_with_precision_larger_than_string)
{
Print("%.*s", 20, "Hello");
EXPECT_EQ("Hello", stream.Storage());
}

TEST_F(TracerAdapterPrintfTest, print_null_string_with_precision)
{
Print("%.*s", 5, nullptr);
Comment thread
heinwessels-philips marked this conversation as resolved.
EXPECT_EQ("(null)", stream.Storage());
}

TEST_F(TracerAdapterPrintfTest, print_string_with_dynamic_precision_ignores_width)
{
// Width is not implemented for strings
Print("%10.*s", 5, "Hello, Tracer!");
EXPECT_EQ("Hello", stream.Storage());
}

TEST_F(TracerAdapterPrintfTest, print_string_with_static_precision_ignores_width)
{
// Width is not implemented for strings
Print("%10.5s", "Hello, Tracer!");
EXPECT_EQ("Hello", stream.Storage());
}

TEST_F(TracerAdapterPrintfTest, trailing_percent_does_not_read_out_of_bounds)
{
Print("hello%");
EXPECT_EQ("hello", stream.Storage());
}

TEST_F(TracerAdapterPrintfTest, lone_trailing_percent_does_not_read_out_of_bounds)
{
Print("%");
EXPECT_EQ("", stream.Storage());
}

TEST_F(TracerAdapterPrintfTest, trailing_percent_after_valid_format_does_not_read_out_of_bounds)
{
Print("%d%", 42);
EXPECT_EQ("42", stream.Storage());
}
Loading