Re: Testing on NULL an unitialized values

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

 



On 04/21/2010 11:58 AM, Oliver Bandel wrote:
> Zitat von "Tor Lillqvist"<tml@xxxxxx>:
>
>>> The test
>>>    if( template )
>>> makes only sense, if you can be sure that uninitialzed values
>>> will definitelky be NULL.
>>
>> You must have missed the g_return_val_if_fail (! template ||
>> GIMP_IS_CONTEXT (template), NULL) .
>>
>> It checks if template is NULL or a pointer to a valid GimpContext. If
>> template is some random non-NULL value, the test will fail and a
>> warning message will be printed. Such warning messages indicate a
>> programmer error and should be dealt with during development.
> [...]
>
> Nice to know, but I was talking on things like the *context
> in that funcion.
>
> Even only temporarily valies, if set to a certain value,
> like 0 or NULL, will help in finding problems.
>
> The mentioned function just was an example.
>
> Uninitialzed values I see nearly everywhere in the code.
>
> Dereferencing NULL is easy to find, because it crashes early.

Hi, Oliver

Have you programmed with glib before?  A lot of defensive programming 
techniques differ between straight C and C-with-glib.  For instance, the 
guards at the top are common, and (I imagine) 
gimp_context_copy_properties has similar guards.  As such, it's the job 
of the called function, not the caller, to check if a pointer they want 
to dereference is NULL.

This has the advantage that you don't check a pointer for NULL 10 times 
across 10 different function calls when you only use it once, all the 
way at the bottom.  Of course, if you actually dereference a value (like 
the template pointer in the snippet you posted), you should test it 
before you dereference it.

In short, you might want to see what sort of defensive techniques are 
customary or appropriate for a given context before concluding that 
we're programming blind.

--xsdg
_______________________________________________
Gimp-developer mailing list
Gimp-developer@xxxxxxxxxxxxxxxxxxxxxx
https://lists.XCF.Berkeley.EDU/mailman/listinfo/gimp-developer

[Index of Archives]     [Video For Linux]     [Photo]     [Yosemite News]     [gtk]     [GIMP for Windows]     [KDE]     [GEGL]     [Gimp's Home]     [Gimp on GUI]     [Gimp on Windows]     [Steve's Art]

  Powered by Linux