[C-safe-secure-studygroup] null check

Robert Seacord rcseacord at gmail.com
Fri Apr 6 08:55:55 BST 2018


Thanks, Professor!

On Fri, Apr 6, 2018, 1:52 AM Roberto Bagnara <bagnara at cs.unipr.it> wrote:

> On 05/04/2018 21:28, Robert Seacord wrote:
> > Is a null pointer guaranteed (by the C and/or C++ specification) to
> evaluate to false?  For example, does the following C++ statement
> >
> >
> >
> >                 Std::string tmp(tmpPtrVal ? tmpPtrVal : “”);
> >
> >
> >
> > Need to be rewritten as:
> >
> >
> >
> >                 Std::string tmp((tmpPtrVal != NULL) ? tmpPtrVal : “”);
>
> In C++ the situation is simple thanks to [conv.bool], i.e.:
>
>   [conv.bool]
>   A prvalue of arithmetic, unscoped enumeration, pointer, or pointer
>   to member type can be converted to a prvalue of type bool. A zero
>   value, null pointer value, or null member pointer value is converted
>   to false; any other value is converted to true. For
>   direct-initialization (11.6), a prvalue of type std::nullptr_t can
>   be converted to a prvalue of type bool; the resulting value is
>   false.
>
> In C11, for the conditional operator you have to combine 6.3.2.3p3
> with 6.5.15p4, namely:
>
>   6.3.2.3p3:
>
>   An integer constant expression with the value 0, or such an
>   expression cast to type void *, is called a null pointer
>   constant. 66) If a null pointer constant is converted to a pointer
>   type, the resulting pointer, called a null pointer, is guaranteed to
>   compare unequal to a pointer to any object or function.
>
>   6.5.15p4
>
>   The first operand is evaluated; there is a sequence point between
>   its evaluation and the evaluation of the second or third operand
>   (whichever is evaluated). The second operand is evaluated only if
>   the first compares unequal to 0; the third operand is evaluated only
>   if the first compares equal to 0; the result is the value of the
>   second or third operand (whichever is evaluated), converted to the
>   type described below. 110)
>
> The same expressions "compares equal/unequal to 0" are used to
> define the semantics of all other constructs where a pointer type
> can occur in a context expecting (de facto) a Boolean value.
>
> --
>      Prof. Roberto Bagnara
>
> Applied Formal Methods Laboratory - University of Parma, Italy
> mailto:bagnara at cs.unipr.it
>                               BUGSENG srl - http://bugseng.com
>                               mailto:roberto.bagnara at bugseng.com
>
> _______________________________________________
> C-safe-secure-studygroup mailing list
> C-safe-secure-studygroup at lists.trustable.io
>
> https://lists.trustable.io/cgi-bin/mailman/listinfo/c-safe-secure-studygroup
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.trustable.io/pipermail/c-safe-secure-studygroup/attachments/20180406/da63022f/attachment.html>


More information about the C-safe-secure-studygroup mailing list