[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Guile-commits] 18/55: scm_seed_to_random_state: Support wide string arg
From: |
Andy Wingo |
Subject: |
[Guile-commits] 18/55: scm_seed_to_random_state: Support wide string arguments. |
Date: |
Thu, 23 May 2019 11:52:39 -0400 (EDT) |
wingo pushed a commit to branch master
in repository guile.
commit 4e24cca595f2fa8e803333da8bb00df60ba0e501
Author: Mark H Weaver <address@hidden>
Date: Fri Oct 19 21:54:34 2018 -0400
scm_seed_to_random_state: Support wide string arguments.
Partially fixes <https://bugs.gnu.org/33044>.
Reported by Tom de Vries <address@hidden>.
* 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'.
---
libguile/random.c | 26 ++++++++++++++++++++++++--
1 file changed, 24 insertions(+), 2 deletions(-)
diff --git a/libguile/random.c b/libguile/random.c
index c09c533..6fd567c 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;
- [Guile-commits] 34/55: Don't mutate read-only string in ports test, (continued)
- [Guile-commits] 34/55: Don't mutate read-only string in ports test, Andy Wingo, 2019/05/23
- [Guile-commits] 36/55: Fix strftime compile with null threads, Andy Wingo, 2019/05/23
- [Guile-commits] 25/55: SRFI-19: Minor refactor of leap second table lookups., Andy Wingo, 2019/05/23
- [Guile-commits] 45/55: Update user-visible copyright years., Andy Wingo, 2019/05/23
- [Guile-commits] 46/55: Fix gc.test "after-gc-hook gets called" failures., Andy Wingo, 2019/05/23
- [Guile-commits] 55/55: Update iconv.m4 from gnulib., Andy Wingo, 2019/05/23
- [Guile-commits] 50/55: Fix indentation in scm_sum., Andy Wingo, 2019/05/23
- [Guile-commits] 48/55: Add 'scm_c_make_char' and use it where appropriate., Andy Wingo, 2019/05/23
- [Guile-commits] 07/55: Improve the documentation for 'nil?'., Andy Wingo, 2019/05/23
- [Guile-commits] 17/55: Fix typo in strings.h., Andy Wingo, 2019/05/23
- [Guile-commits] 18/55: scm_seed_to_random_state: Support wide string arguments.,
Andy Wingo <=
- [Guile-commits] 35/55: Remove redefinition of when & unless in snarf-check-and-output-texi, Andy Wingo, 2019/05/23
- [Guile-commits] 41/55: Make locale monetary conversion tests be less strict on terminal whitespace, Andy Wingo, 2019/05/23
- [Guile-commits] 39/55: Add a fallback value for the locale-monetary-decimal-point, Andy Wingo, 2019/05/23
- [Guile-commits] 40/55: Handle newlib C library's langinfo constant names, Andy Wingo, 2019/05/23
- [Guile-commits] 19/55: SRFI-19: Fix TAI->UTC conversions, leap second handling, etc., Andy Wingo, 2019/05/23