mirror of
				https://github.com/davidgiven/fluxengine.git
				synced 2025-10-24 11:11:02 -07:00 
			
		
		
		
	Fix Bytes slicing buffer overrun; fix bug in the IBM decoder which was
provoking it.
This commit is contained in:
		
							
								
								
									
										10
									
								
								lib/bytes.cc
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								lib/bytes.cc
									
									
									
									
									
								
							| @@ -124,12 +124,16 @@ uint8_t& Bytes::operator [] (unsigned pos) | ||||
| Bytes Bytes::slice(unsigned start, unsigned len) const | ||||
| { | ||||
|     start += _low; | ||||
|     boundsCheck(start); | ||||
|     unsigned end = start + len; | ||||
|     if (end > _high) | ||||
|     if (start >= _high) | ||||
|     { | ||||
|         /* Asking for a completely out-of-range slice --- just return zeroes. */ | ||||
|         return Bytes(len); | ||||
|     } | ||||
|     else if (end > _high) | ||||
|     { | ||||
|         /* Can't share the buffer, as we need to zero-pad the end. */ | ||||
|         Bytes b(end - start); | ||||
|         Bytes b(len); | ||||
|         std::uninitialized_copy(cbegin()+start, cend(), b.begin()); | ||||
|         return b; | ||||
|     } | ||||
|   | ||||
| @@ -20,6 +20,8 @@ SectorVector AbstractIbmDecoder::decodeToSectors(const RawRecordVector& rawRecor | ||||
|     { | ||||
|         const Bytes bytes = decodeFmMfm(rawrecord->data); | ||||
|         int headerSize = skipHeaderBytes(); | ||||
|         if (bytes.size() < (headerSize + 1)) | ||||
|             continue; | ||||
|         Bytes data = bytes.slice(headerSize, bytes.size() - headerSize); | ||||
|  | ||||
|         switch (data[0]) | ||||
|   | ||||
| @@ -98,6 +98,23 @@ static void test_writes() | ||||
|     assert((b == Bytes{ 1, 2, 3, 4, 5 })); | ||||
| } | ||||
|  | ||||
| static void test_slice() | ||||
| { | ||||
|     Bytes b = {1, 2, 3}; | ||||
|  | ||||
|     Bytes bs = b.slice(1, 1); | ||||
|     assert((bs == Bytes{ 2 })); | ||||
|  | ||||
|     bs = b.slice(1, 2); | ||||
|     assert((bs == Bytes{ 2, 3 })); | ||||
|  | ||||
|     bs = b.slice(1, 3); | ||||
|     assert((bs == Bytes{ 2, 3, 0 })); | ||||
|      | ||||
|     bs = b.slice(4, 2); | ||||
|     assert((bs == Bytes{ 0, 0 })); | ||||
| } | ||||
|  | ||||
| int main(int argc, const char* argv[]) | ||||
| { | ||||
|     test_bounds(); | ||||
| @@ -105,5 +122,6 @@ int main(int argc, const char* argv[]) | ||||
|     test_equality(); | ||||
|     test_reads(); | ||||
|     test_writes(); | ||||
|     test_slice(); | ||||
|     return 0; | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user