ByteInputString::ReadString returns std::string
This change makes sure ByteInputStream never returns
data backed by internal buffer.
Bug: http://b/321641092
Test: build, run nogrod_unit_tests
Change-Id: I51e11c1137a7b768e087f38289651216025d3d47
diff --git a/tools/nogrod/byte_input_stream.cc b/tools/nogrod/byte_input_stream.cc
index f0c12da..74ca6eb 100644
--- a/tools/nogrod/byte_input_stream.cc
+++ b/tools/nogrod/byte_input_stream.cc
@@ -16,7 +16,7 @@
#include "byte_input_stream.h"
-#include <berberis/base/checks.h>
+#include "berberis/base/checks.h"
#include "leb128.h"
@@ -101,15 +101,15 @@
return result;
}
-const char* ByteInputStream::ReadString() {
- CHECK(offset_ < size_); // there would be a place for at least one 0
+std::string ByteInputStream::ReadString() {
+ CHECK_LT(offset_, size_); // there should be a place for at least one 0
const char* candidate = reinterpret_cast<const char*>(buffer_ + offset_);
while (buffer_[offset_++] != 0) {
- CHECK(offset_ < size_);
+ CHECK_LT(offset_, size_);
}
- return candidate;
+ return std::string{candidate};
}
} // namespace nogrod
diff --git a/tools/nogrod/byte_input_stream.h b/tools/nogrod/byte_input_stream.h
index 4532dbd..543e9e2 100644
--- a/tools/nogrod/byte_input_stream.h
+++ b/tools/nogrod/byte_input_stream.h
@@ -17,9 +17,9 @@
#ifndef __NOGROD_BYTE_INPUT_STREAM_
#define __NOGROD_BYTE_INPUT_STREAM_
-#include <stddef.h>
-
+#include <cstddef>
#include <cstdint>
+#include <string>
#include <vector>
namespace nogrod {
@@ -38,7 +38,7 @@
[[nodiscard]] uint64_t ReadUint64();
[[nodiscard]] uint64_t ReadLeb128();
[[nodiscard]] int64_t ReadSleb128();
- [[nodiscard]] const char* ReadString();
+ [[nodiscard]] std::string ReadString();
[[nodiscard]] std::vector<uint8_t> ReadBytes(uint64_t size);
private:
diff --git a/tools/nogrod/byte_input_stream_tests.cc b/tools/nogrod/byte_input_stream_tests.cc
index 5c351ae..7e4cd82 100644
--- a/tools/nogrod/byte_input_stream_tests.cc
+++ b/tools/nogrod/byte_input_stream_tests.cc
@@ -48,7 +48,7 @@
ASSERT_TRUE(in.available());
ASSERT_EQ(-63, in.ReadSleb128());
ASSERT_TRUE(in.available());
- ASSERT_STREQ("bar", in.ReadString());
+ ASSERT_EQ("bar", in.ReadString());
ASSERT_TRUE(in.available());
auto byte_vector = in.ReadBytes(3);
ASSERT_EQ(3U, byte_vector.size());