Skip to content

Commit

Permalink
fix: line termination mismatch in hex files (philips-software#307)
Browse files Browse the repository at this point in the history
* fix: incorrect range returned by PeekContinuousRange

* fix: Discard trailing spaces (\r) in hex files

* Update upgrade/pack_builder/BinaryObject.cpp

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Process review comments

* Update upgrade/pack_builder/BinaryObject.cpp

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* fix formatting

* Use std::ispace

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
andjordan and github-actions[bot] committed May 31, 2023
1 parent 320356b commit 47482c3
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
3 changes: 1 addition & 2 deletions infra/stream/StdStringInputStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ namespace infra

ConstByteRange StdStringInputStreamReader::PeekContiguousRange(std::size_t start)
{
ConstByteRange result(reinterpret_cast<const uint8_t*>(string.data()) + offset + start, reinterpret_cast<const uint8_t*>(string.size()));

ConstByteRange result(reinterpret_cast<const uint8_t*>(string.data()) + offset + start, reinterpret_cast<const uint8_t*>(string.data()) + string.size());
return result;
}

Expand Down
10 changes: 10 additions & 0 deletions infra/stream/test/TestStdStringInputStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,13 @@ TEST(StdStringInputStreamTest, PeekLiteral)
stream.Consume(1);
EXPECT_TRUE(stream.Empty());
}

TEST(StdStringInputStreamTest, PeekContiguousRange_returns_expected_range_size)
{
infra::StdStringInputStream::WithStorage stream(infra::inPlace, "abcdef");

EXPECT_EQ(6, stream.PeekContiguousRange().size());

stream.Consume(2);
EXPECT_EQ(4, stream.PeekContiguousRange().size());
}
16 changes: 13 additions & 3 deletions upgrade/pack_builder/BinaryObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
#include "infra/stream/StdStringInputStream.hpp"
#include <algorithm>

namespace
{
bool StringHasNonSpaces(const std::string_view string)
{
return !std::all_of(string.begin(), string.end(), [](auto c)
{ return std::isspace(c); });
}
}

namespace application
{
LineException::LineException(const std::string& prependMessage, const std::string& file, int line)
Expand Down Expand Up @@ -39,7 +48,7 @@ namespace application
this->offset = offset;

int lineNumber = 0;
for (auto line : data)
for (const auto& line : data)
{
++lineNumber;
if (!line.empty())
Expand Down Expand Up @@ -148,7 +157,7 @@ namespace application
memory.Insert(lineContents.data[i], linearAddress + offset + lineContents.address + i);
}

BinaryObject::LineContents::LineContents(std::string line, const std::string& fileName, int lineNumber)
BinaryObject::LineContents::LineContents(const std::string& line, const std::string& fileName, int lineNumber)
{
infra::StdStringInputStream stream(line, infra::softFail);

Expand Down Expand Up @@ -178,7 +187,8 @@ namespace application
throw RecordTooShortException(fileName, lineNumber);
if (sum != 0)
throw IncorrectCrcException(fileName, lineNumber);
if (!stream.Empty())

if (!stream.Empty() && StringHasNonSpaces(std::string_view(line).substr(line.size() - stream.Available())))
throw RecordTooLongException(fileName, lineNumber);
}
}
2 changes: 1 addition & 1 deletion upgrade/pack_builder/BinaryObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace application
private:
struct LineContents
{
LineContents(std::string line, const std::string& fileName, int lineNumber);
LineContents(const std::string& line, const std::string& fileName, int lineNumber);

uint8_t recordType;
uint16_t address;
Expand Down
7 changes: 7 additions & 0 deletions upgrade/pack_builder/test/TestBinaryObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ TEST(BinaryObjectTest, Hex_TooLongRecord)
EXPECT_THROW(object.AddHex(std::vector<std::string>{ ":0100000001fe0" }, 0, "file"), application::RecordTooLongException);
}

TEST(BinaryObjectTest, Hex_TrailingLineTerminator)
{
application::BinaryObject object;
object.AddHex(std::vector<std::string>{ ":00000001FF\r" }, 0, "file");
EXPECT_EQ(application::SparseVector<uint8_t>{}, object.Memory());
}

TEST(BinaryObjectTest, Bin_AddByte)
{
application::BinaryObject object;
Expand Down

0 comments on commit 47482c3

Please sign in to comment.