About some bugs

Vladimir Panteleev vladimir at thecybershadow.net
Tue Jan 4 05:00:41 PST 2011


On Tue, 04 Jan 2011 14:34:15 +0200, bearophile <bearophileHUGS at lycos.com>  
wrote:

> void zeroit(T)(T* ptr) if (!IsPointer!T) {
>     memset(ptr, 0, (*ptr).sizeof);
> }
>
> Standard safer wrappers for some C functions may help low-level D coding.
>
> If you don't want to use a zeroit() then a type system able to catch  
> such bugs needs some nice annotations...

Doesn't D already solve this?
For value types: obj = typeof(obj).init;
For arrays: arr[] = typeof(arr[0]).init; // or just 0 or null or whatever  
.init is

> If the first line is correct, then "new" can't be NULL, so there's no  
> need to test "|| !new".

I think this is something that should be done by the optimizer.

> In 7 cases the result of malloc-like function was not tested for NULL:

This is rather specific. Application programmers would usually want an  
exception to be thrown on a failed allocation.

> A very common case (20 cases in few years) are like this, where a  
> pointer is deferenced before the NULL test:
>
>         block = bdev->bd_disk->private_data;
> -       base = block->base;
>         if (!block)
>                 return -ENODEV;
> +       base = block->base;

Delphi compilers warn in cases when a condition is always true/false,  
because Delphi lacks metaprogramming so such cases are usually due to a  
bug.

-- 
Best regards,
  Vladimir                            mailto:vladimir at thecybershadow.net


More information about the Digitalmars-d mailing list