using strndup in stead of strdup fixes this acording to the attached
git-diff.
so it remains to make sure strndup is portable I guess.
/Stefan
On Sat, Sep 10, 2011 at 3:33 AM, David Hansen <[email protected]> wrote:
> Hello,
>
> the attached code produces
>
> $ ./a.out
> foo,bar
> bar
>
> while I would expect
>
> $ ./a.out
> foo
> bar
>
> Similar behavior can be produced with `substring', but `substring/copy'
> will work as expected. So I suspect this is some issue with strings
> sharing memory and a missing '0' when scm_to_latin1_string calls
> scm_strdup.
>
> David
>
>
diff --git a/libguile/strings.c b/libguile/strings.c
index 24c82fc..dd70ce1 100644
--- a/libguile/strings.c
+++ b/libguile/strings.c
@@ -1779,14 +1779,15 @@ scm_to_latin1_stringn (SCM str, size_t *lenp)
if (scm_i_is_narrow_string (str))
{
+ size_t len = scm_i_string_length (str);
if (lenp)
- *lenp = scm_i_string_length (str);
-
- result = scm_strdup (scm_i_string_data (str));
+ *lenp = len;
+ result = scm_strndup (scm_i_string_data (str), len);
}
else
result = scm_to_stringn (str, lenp, NULL,
- SCM_FAILED_CONVERSION_ERROR);
+ SCM_FAILED_CONVERSION_ERROR);
+}
return result;
}