[Tutor] Safely buffering user input

Bill Mill bill.mill at gmail.com
Thu Jan 27 19:17:20 CET 2005


Miles,


On Thu, 27 Jan 2005 13:08:05 -0500, Miles Stevenson
<miles at mstevenson.org> wrote:
> Newbie question.
> 
> I'm trying to practice safe coding techniques. I just want to make sure that a
> user can't supply a massive argument to my script and cause trouble. I'm just
> trying only accept about 256 bytes:
> 
> buffer(sys.argv[1], 0, 256)
> searchpath = sys.argv[1]

I've never used buffer(); in fact, I didn't even know it existed, and
I've been using python for a while now.

Instead of using buffer, just do:

sys.argv[1] = sys.argv[1][:255]

This says "Set the second element of sys.argv equal to its first 256
characters".

Peace
Bill Mill
bill.mill at gmail.com


More information about the Tutor mailing list