Re: use assert and defensive together



Malcolm McLean wrote, On 26/09/08 21:20:

"Malcolm McLean" <regniztar@xxxxxxxxxxxxxx> wrote in message new

"Flash Gordon" <smap@xxxxxxxxxxxxxxxxx> wrote in message
Malcolm McLean wrote, On 24/09/08 23:08:

It make the code dependent on the logging system. None of the code on my website could be used if I employed such a system, for instance.

No it doesn't, and I provided part of a sample interface for you which keeps them nice and separate and allows you to use the code *without* the logging system if that is your choice.

OK. There are several image-loading functions on my website (in the Basic Algorithms sourcecode). See if you can add defensive programming to any one of them without destroying modularity or the cleanness of the interface.
To keep it simple, you can just defend against negative image dimensions.

Duh. Image-saving I meant. Image loading is easily defended agaisnt by returning a NULL for failure to load the image.

Well, on your savejpeg.c and bmp.c files you can use the following in your save routines:
if (height<0 || width<0 || path || *path || rgb)
return -1;

You might want to also deal with fputc failing. After all, even on modern systems disks fill up occasionally!

After all, the caller must be dealing with the -1 error code generated if the function fails to open the file. It would be better if you used different return codes for each type of failure, but I'm not going to fix al of your code. You could also do what I suggested and provide a mechanism to register a log function and call that if it is passed invalid parameters.

Oh, and you should use a compiler with a decent warning level on it. You maketables function does not use any of the parameters it is passed. The function is therefore incorrect because it should use them or incorrect because it takes them.
--
Flash Gordon
If spamming me sent it to smap@xxxxxxxxxxxxxxxxx
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
.



Relevant Pages

  • Re: In the News: Intelligent Design Group Identifies Failures of
    ... An intelligent design think tank has launched a new website recounting the ... evolution by natural selection." ... molecular biology, developmental biology, genetics, biochemistry, and ... The failure of evolutionary biology to provide detailed evolutionary ...
    (talk.origins)
  • Re: Tektronix 7000 series power supply, sluggish start
    ... It is a combination switcher and linear system. ... usually the backup battery in there croaks and the thing quits. ... Tektronix has a forum on their website where there is plenty of room ... Failure does not prove something is impossible, ...
    (sci.electronics.design)
  • Re: scrape, scrape.
    ... we're trying replace what was our gas cooker. ... Looking for fairly basic stuff like the ability to cook on gas during an electric power failure and being able to have the grill flap fold right down for use as an external shelf whilst serving and still get the oven door open conveniently. ... Our criteria seem to bear odds-all relationship to those on the Which website. ... And as for the squeezing nominal sixty-pz cookers in and out of the nogap where the fitted kitchen seems to have shrunk round it... ...
    (uk.rec.sheds)
  • Re: Scottish verdict on accusation of a "bait and switch scam"
    ... vitriol and bitterness. ... Is this statement directed at the contents of the website for which he ... the ID perps have admitted that space aliens are their most scientific ... There is also 100% failure to explain how, say, the bacterial ...
    (talk.origins)
  • Re: Help with MVP spellcheck protected form code
    ... and I got an error that the password was incorrect so I tried to follow the ... directions from the website but I am still unsure where to insert ...
    (microsoft.public.word.vba.general)