alvatar / spheres

Spheres - A set of tools and libraries for practical Scheme. Multiplatform and Mobile.
MIT License
69 stars 10 forks source link

Bug in string->url of sack server #7

Closed ovenpasta closed 9 years ago

ovenpasta commented 9 years ago

I've found a bug in Sack, the procedure string->uri is not handling empty values example:

(string->uri "/foo?x=1&y=1")

<uri #93 scheme: #f userinfo: #f host: #f port: #f path: "/foo" query: (("x" . "1") ("y" . "1")) fragment: #f>

(string->uri "/foo?x=1&y=") Value: 0 Predicate: (lambda (len) (fx> len 0)) Reason: The input u8vector is not proper UTF-8. *\ ERROR IN #<procedure #94> -- Failed argument check in 'utf-u8vector->string 1>

the result is that the request hangs

alvatar commented 9 years ago

Thank you @ovenpasta This needs a fix. Having a couple of hectic weeks here. But I think a good way to start is to add a bunch of tests to sack. Spheres' libraries are ready for that next step at the moment.

ovenpasta commented 9 years ago

the problem is with (check-arg (lambda (len) (fx> len 0)) len 'utf-u8vector->string "The input u8vector is not proper UTF-8.") in spheres/u8vector.scm. a possible fix maybe allowing to have a (fx>= 0 len) vector (to me seems reasonable) or fixing it in net/sack/url.scm like this:

diff --git a/spheres/net/sack/uri.scm b/spheres/net/sack/uri.scm
index 792856f..6ea5fc1 100644
--- a/spheres/net/sack/uri.scm
+++ b/spheres/net/sack/uri.scm
@@ -377,7 +377,9 @@
                   (loop (+ i 1)
                         (+ j 1)))))
           ;; result now contains the u8vector with the extracted bytes, so let's UTF8-decode them.
-          (utf8-u8vector->string result)))))
+          (if (fx> (u8vector-length result) 0)
+             (utf8-u8vector->string result)
+             "")))))

 ;; Extract a substring out of a URI / URI component string, depending on current decoding settings.
 ;;
ovenpasta commented 9 years ago

I think this is the solution anyway :)

diff --git a/spheres/string/u8vector.scm b/spheres/string/u8vector.scm
index 71c0457..3147e87 100644
--- a/spheres/string/u8vector.scm
+++ b/spheres/string/u8vector.scm
@@ -139,7 +139,7 @@
                                  1)))))))
     ;; It could be that we get a negative length because the string was very short and ended
     ;; with only the first 1-2 bytes of a 2-3 byte multibyte UTF-8 character.
-    (check-arg (lambda (len) (fx> len 0)) len 'utf-u8vector->string "The input u8vector is not proper UTF-8.")
+    (check-arg (lambda (len) (fx>= len 0)) len 'utf-u8vector->string "The input u8vector is not proper UTF-8.")
     ;; (print "output string length = " len " (u8vector length=" u8v-len ")\n")
     ;; Compiled with (declare (safe))
     (let ((s (make-string len)))
alvatar commented 9 years ago

Great! Can you open a PR please?