Date: Sat, 5 Jul 1997 23:30:01 -0700 (PDT) From: ac199@hwcn.org To: freebsd-bugs Subject: Re: bin/3451 : vasprintf() doesn't work. Message-ID: <199707060630.XAA10113@hub.freebsd.org>
next in thread | raw e-mail | index | archive | help
The following reply was made to PR bin/3451; it has been noted by GNATS. From: ac199@hwcn.org To: freebsd-gnats-submit@freebsd.org, tim@X2296 Cc: Subject: Re: bin/3451 : vasprintf() doesn't work. Date: Sun, 6 Jul 1997 02:26:47 -0400 (EDT) > > How-To-Repeat As a demonstration of my dedication and intelligence :), this pr now includes, in addition to the ~5 line good fix and detailed explanation of it, demonstration code! Read the Caveat, though... Tested on 2.2.2 (the bug exists in -current, as of July 5, but I've not tested this code there). /* * CAVEAT: This code depends on a specific behaviour of malloc(3). * If, at some point in the future, it stops demonstrating the * vasprintf(3) bug, it could be because the behaviour of malloc() * has been changed subtly, instead of being because vasprintf() * has been fixed. */ #include <stdio.h> #include <string.h> #include <stdlib.h> char d[] = "In an effort to keep this demonstration as simple and to the point as possible, I use this string instead of mallocing and memsetting. This string will be cut to exactly 128 characters."; main() { static char * a, * b, * c; /* static makes debugging w/ * watches easier */ d[128] = '\0'; /* cut d to proper size */ a = malloc (128); /* 128 == CHUNK_SPARE in vasprintf.c */ b = malloc (70); /* This is very tricky. If we alloc too little * (or too much) space here, malloc() will send * the next malloc(128) off into the boonies, * even though we free(a) 128. */ free (a); /* asprintf will malloc(128) and get what I just * free()'d. The memory at b should be contigious. */ strcpy (b, "string"); asprintf (&c, d); printf ("b %s equals \"string\". It is \"%s\".\n", strcmp(b,"string") ? "no longer" : "still", b); /* At no point have we touched b since strcpy(). It should still * say "string", but it won't... */ }
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199707060630.XAA10113>