[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-hackers] [PATCH] alist-update: don't segfault on non-list
From: |
Jim Ursetto |
Subject: |
Re: [Chicken-hackers] [PATCH] alist-update: don't segfault on non-list |
Date: |
Thu, 14 Mar 2013 02:12:54 -0500 |
Nice catch. Actually, it might be better rewritten with (##sys#check-list lst
'alist-update), although that is not that important.
Related, alist-update! behaves weird but doesn't crash on non-alists:
(alist-update! 'foo 'bar 3)
;=> ((foo . bar) . 3)
And it's inconsistent when you give it a comparator:
;; this uses assoc
(alist-update! 'foo 'bar '((a . b) 3 (c . d)) equal?)
Error: (assoc) bad argument type: 3
;; this uses an assoc-like loop which does not check for lists
(alist-update! 'foo 'bar '((a . b) 3 (c . d)) (cut equal? <> <>))
;=> ((foo . bar) (a . b) 3 (c . d))
Jim
On Mar 13, 2013, at 11:43 PM, Florian Zumbiehl wrote:
> Check the alist passed to alist-update is actually a pair before
> using ##sys#slot on it.
> ---
> data-structures.scm | 23 +++++++++++++----------
> 1 files changed, 13 insertions(+), 10 deletions(-)
>
> diff --git a/data-structures.scm b/data-structures.scm
> index 419e1ad..1c504f6 100644
> --- a/data-structures.scm
> +++ b/data-structures.scm
> @@ -229,16 +229,19 @@
>
> (define (alist-update k v lst #!optional (cmp eqv?))
> (let loop ((lst lst))
> - (if (null? lst)
> - (list (cons k v))
> - (let ((a (##sys#slot lst 0)))
> - (cond ((not (pair? a))
> - (error 'alist-update "bad argument type" a))
> - ((cmp (##sys#slot a 0) k)
> - (cons (cons k v) (##sys#slot lst 1)))
> - (else
> - (cons (cons (##sys#slot a 0) (##sys#slot a 1))
> - (loop (##sys#slot lst 1)))))))))
> + (cond ((null? lst)
> + (list (cons k v)))
> + ((not (pair? lst))
> + (error 'alist-update "bad argument type" lst))
> + (else
> + (let ((a (##sys#slot lst 0)))
> + (cond ((not (pair? a))
> + (error 'alist-update "bad argument type" a))
> + ((cmp (##sys#slot a 0) k)
> + (cons (cons k v) (##sys#slot lst 1)))
> + (else
> + (cons (cons (##sys#slot a 0) (##sys#slot a 1))
> + (loop (##sys#slot lst 1))))))))))
>
> (define (alist-ref x lst #!optional (cmp eqv?) (default #f))
> (let* ([aq (cond [(eq? eq? cmp) assq]
> --
> 1.7.2.5
>
>
> _______________________________________________
> Chicken-hackers mailing list
> address@hidden
> https://lists.nongnu.org/mailman/listinfo/chicken-hackers