return ARCHIVE_FATAL;
}
+ if(dist >= INT_MAX - low_dist - 1) {
+ /* This only happens in invalid archives. */
+ archive_set_error(&a->archive,
+ ARCHIVE_ERRNO_FILE_FORMAT,
+ "Distance pointer overflow");
+ return ARCHIVE_FATAL;
+ }
+
dist += low_dist;
} else {
/* dbits is one of [0,1,2,3] */
assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
EPILOGUE();
-}
\ No newline at end of file
+}
+
+DEFINE_TEST(test_read_format_rar5_distance_overflow)
+{
+ uint8_t buf[16];
+
+ PROLOGUE("test_read_format_rar5_distance_overflow.rar");
+
+ assertA(0 == archive_read_next_header(a, &ae));
+ /* This archive is invalid. However, processing it shouldn't cause any
+ * errors related to variable overflows 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_distance_overflow.rar
+M4F%R(1H'`0"-[P+2``(''/\@("`@_R4``B`@("`@("`@("`@(/__("`@("`@
+M(/\@("`@("`@((9ML63,PX"&AK%:S+?_(/\@_R#_(/\@_R#_(/\@`"``!R`@
+MR<G)``#_(,G)R?___R#___\@____(/___R#___\@____R4#)R<G___\@____
+M(/\@____("`@("`@("`@("`@("`@("`@("`@("`@("`@("`@(/__________
+M____________________________________________________("`@("`@
+.("`@("`@("`@("#_("``
+`
+end