sharplispers / ironclad

A cryptographic toolkit written in Common Lisp
BSD 3-Clause "New" or "Revised" License
166 stars 28 forks source link

Using format ~A to build a symbol name is wrong. #57

Closed informatimago closed 1 year ago

informatimago commented 1 year ago

Using format ~A to build a symbol name is wrong, because ~A respects *print-case*. Similarly, using read-from-string is bad, since it respects the readtable as configured at macro expansion time! Instead, using concatenate and intern: eg. in define-digest-registers,

  (let* ((struct-name (read-from-string (format nil "~A-~A" digest-name '#:regs)))
         (constructor (read-from-string (format nil "~A-~A" '#:initial struct-name)))
         (copier (read-from-string (format nil "%~A-~A" '#:copy struct-name)))
         (digest-fun (read-from-string (format nil "~A~A" digest-name '#:regs-digest)))

should be:

  (let* ((struct-name (intern (concatenate 'string (string digest-name) (string '#:-regs))))
         (constructor (intern (concatenate 'string (string '#:initial-) (string struct-name))))
         (copier      (intern (concatenate 'string (string '#:copy-) (string struct-name))))
         (digest-fun  (intern (concatenate 'string (string digest-name) (string '#:-regs-digest))))

and similarly elsewhere!

informatimago commented 1 year ago

Of course, factoring that into a function such as alexandria:symbolicate:

user> (let ((bar 'quux)) (alexandria:SYMBOLICATE 'foo- bar))
foo-quux
user> (symbol-name *)
"FOO-QUUX"
informatimago commented 1 year ago

0001-Build-symbols-using-SYMBOLICATE.patch

glv2 commented 1 year ago

I added a few fixes to your patch for the tests to pass, and applied it (a3ad615a0ee1b418c2ccbc686b7a6c022c166fc8). Thanks.

informatimago commented 1 year ago

Thank you.