justinethier / cyclone

:cyclone: A brand-new compiler that allows practical application development using R7RS Scheme. We provide modern features and a stable system capable of generating fast native binaries.
http://justinethier.github.io/cyclone/
MIT License
825 stars 44 forks source link

Fix a crash in record predicates #427

Closed seepel closed 3 years ago

seepel commented 3 years ago

Hi there, I ran into a bug where I needed to branch on a procedure receiving either a record or a vector, and noticed that record type predicates don't check the length of the target before checking if the vector is actually a record so when my procedure received a vector with less than two entries it would crash when trying to access the record marker or record type name.

For example:

(define-record-type <my-record> (my-record) my-record?)
(let ((v (vector)))
  (if (my-record? v) #t #f))

would crash with

Error: vector-ref - invalid index: 0 

So I added a check to the procedure returned from make-type-predicate to verify that the vector has three entries.

Of course, there is still another problem in that the result of the following expression results in #t

(vector? (my-record))
justinethier commented 3 years ago

This is great, thank you very much for the bug report and the fix @seepel !

Good point about vector?. Let me look into that...