summaryrefslogtreecommitdiff
path: root/libguile/random.c
diff options
context:
space:
mode:
authorMark H Weaver <mhw@netris.org>2018-10-19 21:54:34 -0400
committerAndy Wingo <wingo@pobox.com>2019-05-23 17:11:40 +0200
commit4e24cca595f2fa8e803333da8bb00df60ba0e501 (patch)
tree505bcbfe809ff83edac99fed698b47d5533943ad /libguile/random.c
parentdcf8389a5a275cda10f693484883f15176e5561d (diff)
downloadguile-4e24cca595f2fa8e803333da8bb00df60ba0e501.tar.gz
scm_seed_to_random_state: Support wide string arguments.
Partially fixes <https://bugs.gnu.org/33044>. Reported by Tom de Vries <tdevries@suse.de>. * libguile/random.c (scm_seed_to_random_state): Use 'scm_to_utf8_string' (or 'scm_to_latin1_string' for a narrow string, for compatibility) to convert the string into raw bytes for use by 'scm_c_make_rstate'. Make sure the length in bytes fits within an 'int'.
Diffstat (limited to 'libguile/random.c')
-rw-r--r--libguile/random.c26
1 files changed, 24 insertions, 2 deletions
diff --git a/libguile/random.c b/libguile/random.c
index c09c53388..6fd567cca 100644
--- a/libguile/random.c
+++ b/libguile/random.c
@@ -447,11 +447,33 @@ SCM_DEFINE (scm_seed_to_random_state, "seed->random-state", 1, 0, 0,
#define FUNC_NAME s_scm_seed_to_random_state
{
SCM res;
+ char *c_str;
+ size_t len;
+
if (SCM_NUMBERP (seed))
seed = scm_number_to_string (seed, SCM_UNDEFINED);
SCM_VALIDATE_STRING (1, seed);
- res = make_rstate (scm_c_make_rstate (scm_i_string_chars (seed),
- scm_i_string_length (seed)));
+
+ if (scm_i_is_narrow_string (seed))
+ /* This special case of a narrow string, where latin1 is used, is
+ for backward compatibility during the 2.2 stable series. In
+ future major releases, we should use UTF-8 uniformly. */
+ c_str = scm_to_latin1_stringn (seed, &len);
+ else
+ c_str = scm_to_utf8_stringn (seed, &len);
+
+ /* 'scm_to_*_stringn' returns a 'size_t' for the length in bytes, but
+ 'scm_c_make_rstate' accepts an 'int'. Make sure the length fits in
+ an 'int'. */
+ if (len > INT_MAX)
+ {
+ free (c_str);
+ SCM_OUT_OF_RANGE (1, seed);
+ }
+
+ res = make_rstate (scm_c_make_rstate (c_str, len));
+ free (c_str);
+
scm_remember_upto_here_1 (seed);
return res;