FOREIGN-STRING-ALLOC: don't call NULL-TERMINATOR-LEN twice
authorStas Boukarev <stassats@gmail.com>
Sat, 24 Nov 2012 00:33:08 +0000 (04:33 +0400)
committerStelian Ionescu <sionescu@cddr.org>
Sat, 24 Nov 2012 20:03:29 +0000 (21:03 +0100)
src/strings.lisp

index d61f438..34f483d 100644 (file)
@@ -187,14 +187,14 @@ The string must be freed with FOREIGN-STRING-FREE."
     (declare (type simple-string string))
     (let* ((mapping (lookup-mapping *foreign-string-mappings* encoding))
            (count (funcall (octet-counter mapping) string start end 0))
-           (length (if null-terminated-p
-                       (+ count (null-terminator-len encoding))
-                       count))
+           (nul-length (if null-terminated-p
+                           (null-terminator-len encoding)
+                           0))
+           (length (+ count nul-length))
            (ptr (foreign-alloc :char :count length)))
       (funcall (encoder mapping) string start end ptr 0)
-      (when null-terminated-p
-        (dotimes (i (null-terminator-len encoding))
-          (setf (mem-ref ptr :char (+ count i)) 0)))
+      (dotimes (i nul-length)
+        (setf (mem-ref ptr :char (+ count i)) 0))
       (values ptr length))))
 
 (defun foreign-string-free (ptr)