unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
From: Stefan Israelsson Tampe <stefan.itampe@gmail.com>
To: bug-guile@gnu.org
Subject: Re: Weird scm_to_latin1_string() behavior.
Date: Sat, 10 Sep 2011 16:03:55 +0200	[thread overview]
Message-ID: <CAGua6m0cj8y+ezK-x86uDqU7+2OR2cTi60OZ=7j_g91T8GuWgQ@mail.gmail.com> (raw)
In-Reply-To: <877h5hji54.fsf@localhorst.mine.nu>


[-- Attachment #1.1: Type: text/plain, Size: 585 bytes --]

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 <david.hansen@gmx.net> 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
>
>

[-- Attachment #1.2: Type: text/html, Size: 941 bytes --]

[-- Attachment #2: strndupfix.diff --]
[-- Type: text/x-patch, Size: 675 bytes --]

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;
 }

  reply	other threads:[~2011-09-10 14:03 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-10  1:33 Weird scm_to_latin1_string() behavior David Hansen
2011-09-10 14:03 ` Stefan Israelsson Tampe [this message]
2011-09-10 18:39 ` Andy Wingo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAGua6m0cj8y+ezK-x86uDqU7+2OR2cTi60OZ=7j_g91T8GuWgQ@mail.gmail.com' \
    --to=stefan.itampe@gmail.com \
    --cc=bug-guile@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).