chicken-hackers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-hackers] substring function and bounds checks


From: Peter Bex
Subject: Re: [Chicken-hackers] substring function and bounds checks
Date: Tue, 5 Feb 2013 23:26:29 +0100
User-agent: Mutt/1.4.2.3i

On Tue, Feb 05, 2013 at 11:11:51PM +0100, Michele La Monaca wrote:
> Maybe this is not the right list for that, sorry. But is there a good
> reason for this behavior?

Yes, sanity.

> # perl -e 'print substr("ciao",0,10);'
> ciao

Perl is extremely sloppy.

> # ruby -e 'puts "ciao"[0..10]'
> ciao

Ruby is just Perl in OOP clothing.

> # python -c 'print "ciao"[0:10];'
> ciao

Python should know better.

> # csi -e '(print (substring "ciao" 0 10))'
> Error: (substring) out of range 0 10
> 
>         Call history:
> 
>         <syntax>          (print (substring "ciao" 0 10))
>         <syntax>          (substring "ciao" 0 10)
>         <eval>    (print (substring "ciao" 0 10))
>         <eval>    (substring "ciao" 0 10)       <--
> 
> 
> I find it rather inconvenient and I can't really see any good reason
> to do that.  In fact, it makes the usage of substring "unsafe" and the
> countermeasures I can imagine (manual bounds checks, padding, writing
> my-own-substring-function, whatever) are quite unsatisfactory... to me
> at least.

If you prefer braindead bug-ridden "do what I mean"-ish behaviour of
other, more sloppy languages, use PHP.

Scheme is about correctness.  If you provide invalid indices, you get
errors.  This will help you detect bugs early on instead of just keep
going on with a bad result of an incorrect computation until some other
thing fails much farther along.  This kind of thing also tends to sneak
in vulnerabilities, as you never *really* know what your code will do
in the face of inconsistencies.  "fail early and noisily" is good design.

More seriously, if you think you know what you're doing and really
want this kind of broken behaviour, we have an egg that emulates this
kind of nonsense: http://wiki.call-cc.org/eggref/4/slice

#;1> (use slice)
#;2> (slice "foo" 0 10)
"foo"

Cheers,
Peter
-- 
http://sjamaan.ath.cx



reply via email to

[Prev in Thread] Current Thread [Next in Thread]