[m-rev.] for review: string__join_list

Zoltan Somogyi zs at cs.mu.OZ.AU
Mon Jul 30 14:11:57 AEST 2001


On 25-Jul-2001, Fergus Henderson <fjh at cs.mu.OZ.AU> wrote:
> If the list was empty, this will now try to allocate `(size_t)-sep_len'
> bytes of memory, which will no doubt fail.

The test case will come tomorrow.

library/string.m:
	Fix a bug in string__join_list pointed out by Fergus; the length
	calculation and the string copying now use the same logic.

Zoltan.

Index: library/string.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/string.m,v
retrieving revision 1.148
diff -u -b -r1.148 string.m
--- library/string.m	2001/07/26 09:49:31	1.148
+++ library/string.m	2001/07/30 04:07:55
@@ -948,7 +948,7 @@
 	MR_Word	tmp;
 	size_t	len;
 
-		/* Determine the total len of all strings */
+		/* Determine the total length of all strings */
 	len = 0;
 	while (!MR_list_is_empty(list)) {
 		len += strlen((MR_String) MR_list_head(list));
@@ -983,17 +983,19 @@
 
 	sep_len = strlen(Sep);
 
-		/* Determine the total len of all strings */
-	len = -sep_len; /* compensate for no separator before first string */
+		/* Determine the total length of all strings */
+	len = 0;
+	add_sep = FALSE;
 	while (!MR_list_is_empty(list)) {
-		len += sep_len + strlen((MR_String) MR_list_head(list));
-		list = MR_list_tail(list);
+		if (add_sep) {
+			len += sep_len;
 	}
 
-		/* Allocate enough word aligned memory for the string */
-	if (len <= 0) {
-		len = 0;
+		len += strlen((MR_String) MR_list_head(list));
+		list = MR_list_tail(list);
+		add_sep = FALSE;
 	}
+
 	MR_allocate_aligned_string_msg(Str, len, MR_PROC_LABEL);
 
 		/* Copy the strings into the new memory */
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list