Ticket #1135: 0001-C_substring_copy-use-C_memmove-instead-of-C_memcpy.patch

File 0001-C_substring_copy-use-C_memmove-instead-of-C_memcpy.patch, 2.6 KB (added by Mario Domenech Goulart, 8 years ago)
  • chicken.h

    From bdda44ba7e64513c4947f1e81bce3ad7b94603bf Mon Sep 17 00:00:00 2001
    From: Mario Domenech Goulart <mario.goulart@gmail.com>
    Date: Fri, 27 Jun 2014 17:43:38 -0300
    Subject: [PATCH] C_substring_copy: use C_memmove instead of C_memcpy
    
    memcpy will screw things up when given the same string as src and
    dest, and that can cause problems in some string-copy! use cases.
    
    Fixes #1135.
    ---
     chicken.h               |    6 +++---
     tests/srfi-13-tests.scm |   12 ++++++++++++
     2 files changed, 15 insertions(+), 3 deletions(-)
    
    diff --git a/chicken.h b/chicken.h
    index 4a3c77a..f5a103e 100644
    a b extern double trunc(double); 
    12601260#define C_i_char_greater_or_equal_p(x, y)  C_mk_bool(C_character_code(x) >= C_character_code(y))
    12611261#define C_i_char_less_or_equal_p(x, y)  C_mk_bool(C_character_code(x) <= C_character_code(y))
    12621262#define C_substring_copy(s1, s2, start1, end1, start2) \
    1263                                         (C_memcpy((C_char *)C_data_pointer(s2) + C_unfix(start2), \
    1264                                                   (C_char *)C_data_pointer(s1) + C_unfix(start1), \
    1265                                                   C_unfix(end1) - C_unfix(start1) ), C_SCHEME_UNDEFINED)
     1263                                        (C_memmove((C_char *)C_data_pointer(s2) + C_unfix(start2), \
     1264                                                   (C_char *)C_data_pointer(s1) + C_unfix(start1), \
     1265                                                   C_unfix(end1) - C_unfix(start1) ), C_SCHEME_UNDEFINED)
    12661266#define C_substring_compare(s1, s2, start1, start2, len) \
    12671267                                        C_mk_bool(C_memcmp((C_char *)C_data_pointer(s1) + C_unfix(start1), \
    12681268                                                           (C_char *)C_data_pointer(s2) + C_unfix(start2), \
  • tests/srfi-13-tests.scm

    diff --git a/tests/srfi-13-tests.scm b/tests/srfi-13-tests.scm
    index 1262b82..8df378a 100644
    a b  
    115115       (let ((x (string-copy "abcdefg")))
    116116         (string-copy! x 2 "ZABCDEFG" 3 6)
    117117         x))
     118
     119;; From Guile.  Thanks to Mark H Weaver.
     120(test "string-copy!: overlapping src and dest, moving right"
     121      "aabce"
     122      (let ((str (string-copy "abcde")))
     123        (string-copy! str 1 str 0 3) str))
     124
     125(test "string-copy!: overlapping src and dest, moving left"
     126      "bcdde"
     127      (let ((str (string-copy "abcde")))
     128        (string-copy! str 0 str 1 4) str))
     129
    118130(test "string-take" "Pete S"  (string-take "Pete Szilagyi" 6))
    119131(test "string-take" ""        (string-take "Pete Szilagyi" 0))
    120132(test "string-take" "Pete Szilagyi" (string-take "Pete Szilagyi" 13))