[m-rev.] diff: fix abort in bit_buffer.write.finalize_to_bitmap

Peter Wang novalazy at gmail.com
Mon Sep 22 11:25:00 AEST 2008


Branches: main

library/bit_buffer.write.m:
	Fix a bug where `finalize_to_bitmap' would abort if the size of the
	final bitmap is an exact multiple of the chunk size that the write
	buffer was created with.

tests/hard_coded/Mmakefile:
tests/hard_coded/finalize_to_bitmap.exp:
tests/hard_coded/finalize_to_bitmap.m:
	Add test case.

diff --git a/library/bit_buffer.write.m b/library/bit_buffer.write.m
index 65a9662..d40f80a 100644
--- a/library/bit_buffer.write.m
+++ b/library/bit_buffer.write.m
@@ -266,9 +266,13 @@ finalize_to_bitmap(write_buffer(Buffer)) = !:BM :-
 
 copy_out_bitmap(FilledBM, !Index, !BM) :-
     Size = FilledBM ^ num_bits,
-    !:Index = !.Index - Size,
-    !:BM = bitmap.copy_bits(FilledBM, 0, !.BM, !.Index, Size).
-   
+    ( Size > 0 ->
+        !:Index = !.Index - Size,
+        !:BM = bitmap.copy_bits(FilledBM, 0, !.BM, !.Index, Size)
+    ;
+        true
+    ).
+
 :- pred maybe_make_room(bit_buffer(Stream, State)::bit_buffer_di,
     bit_buffer(Stream, State)::bit_buffer_uo) is det
     <= stream.writer(Stream, bitmap.slice, State).
diff --git a/tests/hard_coded/Mmakefile b/tests/hard_coded/Mmakefile
index df68298..84138f5 100644
--- a/tests/hard_coded/Mmakefile
+++ b/tests/hard_coded/Mmakefile
@@ -82,6 +82,7 @@ ORDINARY_PROGS=	\
 	failure_unify \
 	field_syntax \
 	finalise_decl \
+	finalize_to_bitmap \
 	float_field \
 	float_map \
 	float_reg \
diff --git a/tests/hard_coded/finalize_to_bitmap.exp b/tests/hard_coded/finalize_to_bitmap.exp
new file mode 100644
index 0000000..2302ebc
--- /dev/null
+++ b/tests/hard_coded/finalize_to_bitmap.exp
@@ -0,0 +1 @@
+"<64:0102030405060708>"
diff --git a/tests/hard_coded/finalize_to_bitmap.m b/tests/hard_coded/finalize_to_bitmap.m
new file mode 100644
index 0000000..30d04a2
--- /dev/null
+++ b/tests/hard_coded/finalize_to_bitmap.m
@@ -0,0 +1,44 @@
+%-----------------------------------------------------------------------------%
+% Regression test.  bit_buffer.write.finalize_to_bitmap aborted on some input.
+%
+% Uncaught Mercury exception:
+% bitmap_error("copy_bits (source): 0 bits starting at bit 0 is out of bounds [0, 0).")
+% 
+%-----------------------------------------------------------------------------%
+
+:- module finalize_to_bitmap.
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+%-----------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module bitmap.
+:- import_module bit_buffer.
+:- import_module bit_buffer.write.
+
+%-----------------------------------------------------------------------------%
+
+main(!IO) :-
+    some [!Buf] (
+        !:Buf = new_bitmap_builder(8),
+        put_byte(1, !Buf),
+        put_byte(2, !Buf),
+        put_byte(3, !Buf),
+        put_byte(4, !Buf),
+        put_byte(5, !Buf),
+        put_byte(6, !Buf),
+        put_byte(7, !Buf),
+        put_byte(8, !Buf),
+        Bitmap = finalize_to_bitmap(!.Buf),
+        io.write(Bitmap, !IO),
+        io.nl(!IO)
+    ).
+
+%-----------------------------------------------------------------------------%
+% vim: ft=mercury ts=8 sts=4 sw=4 et

--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list