Re: use assert and defensive together
- From: Flash Gordon <smap@xxxxxxxxxxxxxxxxx>
- Date: Sun, 28 Sep 2008 11:26:13 +0100
Malcolm McLean wrote, On 26/09/08 21:20:
"Malcolm McLean" <regniztar@xxxxxxxxxxxxxx> wrote in message newDuh. Image-saving I meant. Image loading is easily defended agaisnt by returning a NULL for failure to load the image.
"Flash Gordon" <smap@xxxxxxxxxxxxxxxxx> wrote in messageMalcolm McLean wrote, On 24/09/08 23:08: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.
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.
To keep it simple, you can just defend against negative image dimensions.
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/
.
- Follow-Ups:
- Re: use assert and defensive together
- From: Malcolm McLean
- Re: use assert and defensive together
- References:
- use assert and defensive together
- From: bingfeng
- Re: use assert and defensive together
- From: Malcolm McLean
- Re: use assert and defensive together
- From: Flash Gordon
- Re: use assert and defensive together
- From: Malcolm McLean
- Re: use assert and defensive together
- From: Flash Gordon
- Re: use assert and defensive together
- From: Malcolm McLean
- Re: use assert and defensive together
- From: Flash Gordon
- Re: use assert and defensive together
- From: Malcolm McLean
- Re: use assert and defensive together
- From: Malcolm McLean
- use assert and defensive together
- Prev by Date: Re: Highly efficient string reversal code
- Next by Date: Re: "Continue" usage
- Previous by thread: Re: use assert and defensive together
- Next by thread: Re: use assert and defensive together
- Index(es):
Relevant Pages
|