return ARCHIVE_EOF;
}
- data += (byte >> 8) << (i * 8);
+ /* Cast to uint32_t will ensure the shift operation will not produce
+ * undefined result. */
+ data += ((uint32_t) byte >> 8) << (i * 8);
skip_bits(rar, 8);
}
dist += dist_slot;
} else {
dbits = dist_slot / 2 - 1;
- dist += (2 | (dist_slot & 1)) << dbits;
+
+ /* Cast to uint32_t will make sure the shift left operation
+ * won't produce undefined result. Then, the uint32_t type will
+ * be implicitly casted to int. */
+ dist += (uint32_t) (2 | (dist_slot & 1)) << dbits;
}
if(dbits > 0) {
EPILOGUE();
}
+
+DEFINE_TEST(test_read_format_rar5_leftshift1)
+{
+ uint8_t buf[16];
+
+ PROLOGUE("test_read_format_rar5_leftshift1.rar");
+
+ assertA(0 == archive_read_next_header(a, &ae));
+ /* This archive is invalid. However, processing it shouldn't cause any
+ * errors related to undefined operations when using -fsanitize. */
+ assertA(ARCHIVE_FATAL == archive_read_data(a, buf, sizeof(buf)));
+ assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
+
+ EPILOGUE();
+}
+
+DEFINE_TEST(test_read_format_rar5_leftshift2)
+{
+ uint8_t buf[16];
+
+ PROLOGUE("test_read_format_rar5_leftshift2.rar");
+
+ assertA(0 == archive_read_next_header(a, &ae));
+ /* This archive is invalid. However, processing it shouldn't cause any
+ * errors related to undefined operations when using -fsanitize. */
+ assertA(ARCHIVE_FATAL == archive_read_data(a, buf, sizeof(buf)));
+ assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
+
+ EPILOGUE();
+}
--- /dev/null
+begin 644 test_read_format_rar5_leftshift1.rar
+M4F%R(1H'`0"-[P+2``(''(`'`"``_R4``B$<`0(`#@```0```"#2````____
+M_P`(`/__^P#_W0`"(8#_`(:&;;%DS+?,L=:NL0#(3`$`````````````````
+M``"``````````+!DS+*RL[*RL@```-P``````````````````(``````````
+ML&3,LK*RLK*R````W`````#X____````````````````````````%5H>;&@T
+M+3HW"2!SB^)_<Z3_`````?40'Q\?'Q\?'Q\?'Q\?'Q\?'Q\?'Q\?'Q\`````
+5`````````````````/H`>@``````
+`
+end
--- /dev/null
+begin 644 test_read_format_rar5_leftshift2.rar
+M4F%R(1H'`0"-[P+2``(''(`'`2``_RL``B'+`0(`,O__````-WJ\KR<<)0`"
+M(;<*`BY*`!```&;%T#%24%"`_R4`[@K+(2Y*`&$``'__`/\E``(N2@`0`0(`
+0(?__`%N&?Q2UH.CHZ.CHZ```
+`
+end