Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Under ECL 24.5.10 byte-array-to-hex-string (v0.59) miscompiles due to (safety 0) #78

Open
cdmojoli opened this issue Oct 6, 2024 · 1 comment

Comments

@cdmojoli
Copy link

cdmojoli commented Oct 6, 2024

(In the exhibit below, scroll to the right to see the mismatch.)

CL-USER> (asdf:test-system :aws-sign4)
Expected presigned url:
"https://examplebucket.s3.amazonaws.com/test.txt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIOSFODNN7EXAMPLE%2F20130524%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20130524T000000Z&X-Amz-Expires=86400&X-Amz-SignedHeaders=host&X-Amz-Signature=aeeed9bbccd4d02ee5c0109b86d86835f995330da4c265957d157751f604d404"

Got:
"https://examplebucket.s3.amazonaws.com/test.txt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIOSFODNN7EXAMPLE%2F20130524%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20130524T000000Z&X-Amz-Expires=86400&X-Amz-SignedHeaders=host&X-Amz-Signature=3\0\0\0\0\0\0\0\0\0\001\0\0\0\0\0\0\0\03\0\0\0\02\03\0\0\0\0\01\0\0\01\003\02\0\0\03\0\0\02\03\0\03\0\0\0\0\0\00"

Simply changing safety to 1 solves this:


(defun byte-array-to-hex-string (vector &key (start 0) end (element-type 'base-char))
  "Return a string containing the hexadecimal representation of the
subsequence of VECTOR between START and END.  ELEMENT-TYPE controls
the element-type of the returned string."
  (declare (type (vector (unsigned-byte 8)) vector)
           (type fixnum start)
           (type (or null fixnum) end)
           (optimize (speed 3) (safety 1)))
  (let* ((end (or end (length vector)))
         (length (- end start))
         (hexdigits #.(coerce "0123456789abcdef" 'simple-base-string)))
    (loop with string = (ecase element-type
                          ;; so that the compiler optimization can jump in
                          (base-char (make-string (* length 2)
                                                  :element-type 'base-char))
                          (character (make-string (* length 2)
                                                  :element-type 'character)))
       for i from start below end
       for j from 0 below (* length 2) by 2
       do (let ((byte (aref vector i)))
#+ecl
            (declare (optimize (safety 1)))
#-ecl            
            (declare (optimize (safety 0)))
            (setf (aref string j)
                  (aref hexdigits (ldb (byte 4 4) byte))
                  (aref string (1+ j))
                  (aref hexdigits (ldb (byte 4 0) byte))))
       finally (return string))))

Notes:

  1. I am using Ubuntu 22.04 on x64.
  2. Ironclad tests pass.
@cdmojoli cdmojoli changed the title Under ECL 24.5.10 byte-array-to-hex-string miscompiles due to (safety 0) Under ECL 24.5.10 byte-array-to-hex-string (v0.59) miscompiles due to (safety 0) Oct 6, 2024
@glv2
Copy link
Collaborator

glv2 commented Oct 11, 2024

I still got the issue with (safety 1), so I used a different workaround (commit f651945).
Could you check if it works for you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants