unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
* Weird scm_to_latin1_string() behavior.
@ 2011-09-10  1:33 David Hansen
  2011-09-10 14:03 ` Stefan Israelsson Tampe
  2011-09-10 18:39 ` Andy Wingo
  0 siblings, 2 replies; 3+ messages in thread
From: David Hansen @ 2011-09-10  1:33 UTC (permalink / raw)
  To: bug-guile

[-- Attachment #1: Type: text/plain, Size: 326 bytes --]

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


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: main.c --]
[-- Type: text/x-csrc, Size: 675 bytes --]

#include <libguile.h>

/*
  This outputs:

  dhansen@localhorst ~/tmp $ ./a.out
  foo,bar
  bar

*/

static void
inner_main (void *data, int argc, char **argv)
{
  char *cstr;

  SCM string = scm_from_latin1_string ("foo,bar");
  SCM tokens = scm_string_split (string, SCM_MAKE_CHAR (','));
  SCM tok;

  for (tok = tokens; !scm_is_null (tok); tok = scm_cdr (tok))
    {
      cstr = scm_to_latin1_string (scm_car (tok));
      printf ("%s\n", cstr);
      free (cstr);
    }
}

int
main (int argc, char **argv)
{
  scm_boot_guile (argc, argv, inner_main, NULL);
}

/* Local Variables: */
/* compile-command: "gcc `pkg-config --cflags --libs guile-2.0` main.c" */
/* End: */

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

* Re: Weird scm_to_latin1_string() behavior.
  2011-09-10  1:33 Weird scm_to_latin1_string() behavior David Hansen
@ 2011-09-10 14:03 ` Stefan Israelsson Tampe
  2011-09-10 18:39 ` Andy Wingo
  1 sibling, 0 replies; 3+ messages in thread
From: Stefan Israelsson Tampe @ 2011-09-10 14:03 UTC (permalink / raw)
  To: bug-guile


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

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

* Re: Weird scm_to_latin1_string() behavior.
  2011-09-10  1:33 Weird scm_to_latin1_string() behavior David Hansen
  2011-09-10 14:03 ` Stefan Israelsson Tampe
@ 2011-09-10 18:39 ` Andy Wingo
  1 sibling, 0 replies; 3+ messages in thread
From: Andy Wingo @ 2011-09-10 18:39 UTC (permalink / raw)
  To: bug-guile

On Fri 09 Sep 2011 18:33, David Hansen <david.hansen@gmx.net> writes:

> the attached code produces
>
> $ ./a.out
> foo,bar
> bar
>
> while I would expect
>
> $ ./a.out
> foo
> bar

I applied Stefan's fix.  Thanks for the report, and thanks to Stefan for
the patch.

Andy
-- 
http://wingolog.org/



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

end of thread, other threads:[~2011-09-10 18:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-09-10  1:33 Weird scm_to_latin1_string() behavior David Hansen
2011-09-10 14:03 ` Stefan Israelsson Tampe
2011-09-10 18:39 ` Andy Wingo

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).