throw error when we get superfluous octets at the end v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 11 Jul 2021 18:09:19 +0200
branchv_0
changeset 30 e27e133731ee
parent 29 c232d8b8efbf
child 31 a87c97aecbf6
throw error when we get superfluous octets at the end
src/lib/AbstractParser.cpp
src/lib/BasicASN1Reader.h
--- a/src/lib/AbstractParser.cpp	Sun Jul 11 00:44:15 2021 +0200
+++ b/src/lib/AbstractParser.cpp	Sun Jul 11 18:09:19 2021 +0200
@@ -79,7 +79,7 @@
 }
 
 void AbstractParser::peek(uint8_t* buffer, const size_t length) {
-	implementation->buffer.read(buffer, length);
+	implementation->buffer.peek(buffer, length);
 }
 
 size_t AbstractParser::getBytesRead() {
--- a/src/lib/BasicASN1Reader.h	Sun Jul 11 00:44:15 2021 +0200
+++ b/src/lib/BasicASN1Reader.h	Sun Jul 11 18:09:19 2021 +0200
@@ -267,6 +267,17 @@
 		commit();
 	}
 
+	bool hasAvailableForReading() {
+		// TODO: API in AbstractParser for checking available bytes?
+		uint8_t tmp;
+		try {
+			peek(&tmp, 1);
+			return true;
+		} catch (...) {
+			return false;
+		}
+	}
+
 protected:
 
 	void update() override {
@@ -276,6 +287,8 @@
 public:
 
 	void close() override {
+		if (hasAvailableForReading()) throw std::logic_error("Unexpected content at the end of the stream"); // TODO: better exception
+
 		checkRemainingItems();
 		// TODO: check the bytes remaining in the buffer
 		if (started) handlers.writeStreamEnd();