public inbox for lvm2-cvs@sourceware.org
help / color / mirror / Atom feed
* LVM2 ./WHATS_NEW_DM libdm/libdm-string.c
@ 2012-02-10 13:56 zkabelac
  0 siblings, 0 replies; 2+ messages in thread
From: zkabelac @ 2012-02-10 13:56 UTC (permalink / raw)
  To: lvm-devel, lvm2-cvs

CVSROOT:	/cvs/lvm2
Module name:	LVM2
Changes by:	zkabelac@sourceware.org	2012-02-10 13:56:20

Modified files:
	.              : WHATS_NEW_DM 
	libdm          : libdm-string.c 

Log message:
	Return error for failing allocation
	
	Fix case, where final strdup would have failed and it would miss to return
	failure for this case and return success and NULL pointer.

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/LVM2/WHATS_NEW_DM.diff?cvsroot=lvm2&r1=1.544&r2=1.545
http://sourceware.org/cgi-bin/cvsweb.cgi/LVM2/libdm/libdm-string.c.diff?cvsroot=lvm2&r1=1.17&r2=1.18

--- LVM2/WHATS_NEW_DM	2012/02/10 13:52:05	1.544
+++ LVM2/WHATS_NEW_DM	2012/02/10 13:56:19	1.545
@@ -1,5 +1,6 @@
 Version 1.02.70 - 
 ===================================
+  Return error for failing allocation in dm_asprintf().
   Add missing test for failing allocation in dm_realloc() code.
   Add test for memory allocation failures in regex matcher code.
   Simplify dm_task_set_geometry() and use dm_asprintf().
--- LVM2/libdm/libdm-string.c	2011/09/14 16:07:07	1.17
+++ LVM2/libdm/libdm-string.c	2012/02/10 13:56:20	1.18
@@ -156,7 +156,9 @@
 		}
 	}
 
-	*result = dm_strdup(buf);
+	if (!(*result = dm_strdup(buf)))
+		n = -2; /* return -1 */
+
 	dm_free(buf);
 	return n + 1;
 }


^ permalink raw reply	[flat|nested] 2+ messages in thread

* LVM2 ./WHATS_NEW_DM libdm/libdm-string.c
@ 2012-02-23 18:05 zkabelac
  0 siblings, 0 replies; 2+ messages in thread
From: zkabelac @ 2012-02-23 18:05 UTC (permalink / raw)
  To: lvm-devel, lvm2-cvs

CVSROOT:	/cvs/lvm2
Module name:	LVM2
Changes by:	zkabelac@sourceware.org	2012-02-23 18:05:13

Modified files:
	.              : WHATS_NEW_DM 
	libdm          : libdm-string.c 

Log message:
	Limit number of mem allocs and copies
	
	If we have good enough glibc to return number of needed chars, do not
	loop try to reach good size, but use this size directly for allocation,
	saving also last strdup.
	
	Since now we start with 16 bytes - skip buffer realloc for shorter string.

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/LVM2/WHATS_NEW_DM.diff?cvsroot=lvm2&r1=1.570&r2=1.571
http://sourceware.org/cgi-bin/cvsweb.cgi/LVM2/libdm/libdm-string.c.diff?cvsroot=lvm2&r1=1.18&r2=1.19

--- LVM2/WHATS_NEW_DM	2012/02/20 21:11:06	1.570
+++ LVM2/WHATS_NEW_DM	2012/02/23 18:05:12	1.571
@@ -1,5 +1,6 @@
 Version 1.02.72 - 
 ====================================
+  Avoid memory reallocation for dm_asprintf.
 
 Version 1.02.71 - 20th February 2012
 ====================================
--- LVM2/libdm/libdm-string.c	2012/02/10 13:56:20	1.18
+++ LVM2/libdm/libdm-string.c	2012/02/23 18:05:12	1.19
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2006-2007 Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2006-2012 Red Hat, Inc. All rights reserved.
  *
  * This file is part of the device-mapper userspace tools.
  *
@@ -131,7 +131,7 @@
 
 int dm_asprintf(char **result, const char *format, ...)
 {
-	int n, ok = 0, size = 32;
+	int i, n, size = 16;
 	va_list ap;
 	char *buf = dm_malloc(size);
 
@@ -140,26 +140,30 @@
 	if (!buf)
 		return -1;
 
-	while (!ok) {
+	for (i = 0;; i++) {
 		va_start(ap, format);
 		n = vsnprintf(buf, size, format, ap);
 		va_end(ap);
 
 		if (0 <= n && n < size)
-			ok = 1;
-		else {
-			dm_free(buf);
-			size *= 2;
-			buf = dm_malloc(size);
-			if (!buf)
-				return -1;
-		}
+			break;
+
+		dm_free(buf);
+		/* Up to glibc 2.0.6 returns -1 */
+		size = (n < 0) ? size * 2 : n + 1;
+		if (!(buf = dm_malloc(size)))
+			return -1;
 	}
 
-	if (!(*result = dm_strdup(buf)))
-		n = -2; /* return -1 */
+	if (i > 1) {
+		/* Reallocating more then once? */
+		if (!(*result = dm_strdup(buf))) {
+			dm_free(buf);
+			return -1;
+		}
+	} else
+		*result = buf;
 
-	dm_free(buf);
 	return n + 1;
 }
 


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-02-23 18:05 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-02-10 13:56 LVM2 ./WHATS_NEW_DM libdm/libdm-string.c zkabelac
2012-02-23 18:05 zkabelac

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).