Method on sb-sequence:length can return non-index

Bug #1899874 reported by Zach Shaftel on 2020-10-15
This bug affects 1 person
Affects Status Importance Assigned to Milestone

Bug Description

(defclass foo (sequence standard-object) ())

(defmethod sb-sequence:length ((seq foo))
  (1+ most-positive-fixnum))

(typep (cl:length (make-instance 'foo)) 'sb-int:index) ;; => nil

This breaks the defknown for cl:length. I don't know if this is technically a bug, but it seems like undesired behavior.

$ ros use sbcl && ros run -- --version && uname -a
SBCL 2.0.9
Linux arch-thinkpad 5.8.14-arch1-1 #1 SMP PREEMPT Wed, 07 Oct 2020 23:59:46 +0000 x86_64 GNU/Linux

Stas Boukarev (stassats) wrote :

They should return an index. It would be nice to produce a warning here, but generic functions currently do not play nice with ftype declarations.

Zach Shaftel (zach-shaftel) wrote :

Could this ever cause a serious problem? If the compiler assumes (1+ (length some-sequence)) will be a fixnum, I assume it would transform to a fixnum=>fixnum VOP, which could lead to a fault somewhere, right? Maybe this would only cause serious issues in 0 safety code, and if you're working with extended sequences you should probably be prepared for the worst. I'm still not familiar enough with the compiler to know how cautious it is when transforming based on function types.

Stas Boukarev (stassats) wrote :

> Could this ever cause a serious problem?

It could, that's why you shouldn't return arbitrary things from it.

To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers