Click here to Skip to main content
15,922,584 members

Survey Results

Who should take responsibility for library function calls?   [Edit]

Survey period: 17 Feb 2003 to 23 Feb 2003

When a library function is called, who is responsible for ensuring that the input data will not cause a program to crash, or worse - be susceptible to a security hole?

OptionVotes% 
The programmer calling the function should ensure the supplied input data is valid.11210.82
The programmer who wrote the library function should ensure that all input data is valid.43341.84
Both library function writer and caller should validate all input data independently.44042.51
Neither - just use try...catch blocks to handle any problems.504.83



 
GeneralProgramming by Contract Pin
rruge26-Feb-03 4:31
rruge26-Feb-03 4:31 
GeneralDebug vs. Release Libraries Pin
Henry P.22-Feb-03 13:26
Henry P.22-Feb-03 13:26 
GeneralSemaphores and Mutexes Pin
almc22-Feb-03 1:06
almc22-Feb-03 1:06 
GeneralA real world example: Win32 API Pin
Rodrigo Strauss21-Feb-03 13:34
Rodrigo Strauss21-Feb-03 13:34 
GeneralRe: A real world example: Win32 API Pin
Daniel Lohmann22-Feb-03 0:11
Daniel Lohmann22-Feb-03 0:11 
GeneralInternal or External Libraries Pin
fantastic_mr_fox20-Feb-03 22:25
fantastic_mr_fox20-Feb-03 22:25 
General2 sides of same coin Pin
John R. Shaw20-Feb-03 9:56
John R. Shaw20-Feb-03 9:56 
GeneralRe: 2 sides of same coin Pin
Chris Maunder21-Feb-03 16:17
cofounderChris Maunder21-Feb-03 16:17 
GeneralSurprising results, IMHO Pin
iGN19-Feb-03 9:24
iGN19-Feb-03 9:24 
I found the results of this poll rather puzzling.

I voted for the first option; the person who uses the library should use it correctly.

It seems like a very odd thing to do to do the error checking in two places, which is what a whole lot of you guys voted for. Why would you waste time, implementation (your own time! You should be at the beach) and CPU-time, doing the same thing twice? Especially when you know that if it's done "right", it simply executes the exact same code twice.

I could understand the argument that "it's better to be safe than sorry", but this sounds kludgy; programming by approximation ("oh, so that's where it crashes. I guess _I_ should have taken extra precautions, even though I'm used to the library catching these things for me.")

There are a number of reasons why I'd rather have the user of the library make sure he feeds correct data than having the library check it every time:

- Efficiency. If the input value needs to be between 0 and 255, the library could check it, but if the user wants to iterate from 0 to 255, that's 256 if-tests out the window CPU-wise, but more importantly:
- Allocation of responsibility. A good coder should know his libraries well enough to know what their valid input parameters are. A good library should make clear for which input values it's supposed to work.

It's important that you, the library user, knows where the check is being done, and I for one, would be much more comfortable checking my own input than having that being left to the library writer. Not that I don't necessarily trust him/them... But why should I? The excuse "I picked a bad library, so it isn't really my fault" might fly with your geekiest of friends, but the boss needs a working product.

It's been pointed out that some things are downright infeasible for the library writer to check himself, especially if it's a library for å low-level language like C or C++; how do you determine if a handle is valid? For scripting languages like PHP, I like to make a level of indirection from the handle to the user, and make sure the "Resource ID" is valid. Why? Because you can't trust a PHP-programmer to do what he should, and he'll take down the webserver given the chance. Doing this with a C/C++-library (and you could, of course), you'd be walking around in quicksand.

Obviously, C and C++ themselves advocates correct usage of libraries. You should not free a pointer not returned by malloc. You should not delete [] something not returned by new []. The STL expects you to make sure you're iterating from .begin() to .end() on the same container. Or else... all hell breaks loose.

To sum up my point of view:

- Consider your users' needs.
- For a low-level library, it's more important that the correct usage of a library is documented than enforced by code. That way, everybody knows who's to blame when things go wrong.

--
O< O- O< *quack* O- O< O- O< O- O< *kvekk* O-
Lars Thomas Denstad <larsde@redloop.com>
http://www.redloop.com/larsde/
O< *pip* *pip* *pip* O- O< O- O< O- O< *quack* O- O< O-
GeneralRe: Surprising results, IMHO Pin
Member 3174719-Feb-03 10:02
Member 3174719-Feb-03 10:02 
GeneralRe: Surprising results, IMHO Pin
Brandon Haase19-Feb-03 10:41
Brandon Haase19-Feb-03 10:41 
GeneralRe: Surprising results, IMHO Pin
compiler21-Feb-03 6:48
compiler21-Feb-03 6:48 
GeneralRe: Surprising results, IMHO Pin
Andreas Saurwein20-Feb-03 1:15
Andreas Saurwein20-Feb-03 1:15 
GeneralRe: Surprising results, IMHO Pin
Baris Kurtlutepe21-Feb-03 7:49
Baris Kurtlutepe21-Feb-03 7:49 
GeneralError locus Pin
Gary Wheeler18-Feb-03 6:20
Gary Wheeler18-Feb-03 6:20 
GeneralHmmm Pin
Jörgen Sigvardsson17-Feb-03 11:24
Jörgen Sigvardsson17-Feb-03 11:24 
GeneralRe: Hmmm Pin
Andreas Saurwein20-Feb-03 7:49
Andreas Saurwein20-Feb-03 7:49 
GeneralNot exactly a library but Pin
Michael P Butler17-Feb-03 10:01
Michael P Butler17-Feb-03 10:01 
GeneralRe: Not exactly a library but Pin
Mike Nordell17-Feb-03 18:13
Mike Nordell17-Feb-03 18:13 
GeneralIf there were a strict rule set in stone... Pin
Paul M Watt17-Feb-03 9:57
mentorPaul M Watt17-Feb-03 9:57 
GeneralWhere are all the C devs? Pin
Shog917-Feb-03 6:56
sitebuilderShog917-Feb-03 6:56 
GeneralRe: Where are all the C devs? Pin
Tim Smith17-Feb-03 8:13
Tim Smith17-Feb-03 8:13 
GeneralRe: Where are all the C devs? Pin
Jeremy Falcon18-Feb-03 12:00
professionalJeremy Falcon18-Feb-03 12:00 
General*I* want to be responsible Pin
Nemanja Trifunovic17-Feb-03 6:24
Nemanja Trifunovic17-Feb-03 6:24 
GeneralWhere are all the C++ devs? Pin
Chris Maunder17-Feb-03 5:59
cofounderChris Maunder17-Feb-03 5:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.