Re: NULL passed as an argument to memcmp() in parse_func.c

From: Piotr Stefaniak <postgres(at)piotr-stefaniak(dot)me>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Glen Knowles <gknowles(at)ieee(dot)org>, Robert Haas <robertmhaas(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: NULL passed as an argument to memcmp() in parse_func.c
Date: 2015-06-23 17:54:53
Message-ID: BLU436-SMTP51B0275132FFB7104E5DCCF2A00@phx.gbl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 06/23/2015 06:42 PM, Tom Lane wrote:
> Glen Knowles <gknowles(at)ieee(dot)org> writes:
>> It appears that, according to the standard, passing NULL to memcmp is
>> undefined behavior, even if the count is 0. See
>> http://stackoverflow.com/questions/16362925/can-i-pass-a-null-pointer-to-memcmp
>> for C99 and C++ standard references.
>
> Hmm ... looks like that's correct. I had not noticed the introductory
> paragraphs. For those following along at home, the relevant text in
> C99 is in "7.21.1 String function conventions":
>
> [#2] Where an argument declared as size_t n specifies the
> length of the array for a function, n can have the value
> zero on a call to that function. Unless explicitly stated
> otherwise in the description of a particular function in
> this subclause, pointer arguments on such a call shall still
> have valid values, as described in 7.1.4. On such a call, a
> function that locates a character finds no occurrence, a
> function that compares two character sequences returns zero,
> and a function that copies characters copies zero
> characters.
>
> and the relevant text from 7.1.4 is
>
> [#1] Each of the following statements applies unless
> explicitly stated otherwise in the detailed descriptions |
> that follow: If an argument to a function has an invalid
> value (such as a value outside the domain of the function,
> or a pointer outside the address space of the program, or a
> null pointer) or a type (after promotion) not expected by a
> function with variable number of arguments, the behavior is
> undefined.

For what it's worth, in C89 and C90 the wording of the latter paragraph
(respectively 4.1.6 and 7.1.7) is:

> Use of library functions
> Each of the following statements applies unless explicitly stated
> otherwise in the detailed descriptions that follow. If an argument to
> a function has an invalid value (such as a value outside the domain
> of the function, or a pointer outside the address space of the
> program, or a null pointer), the behavior is undefined. [...]

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Magnus Hagander 2015-06-23 18:14:42 Re: pg_stat_*_columns?
Previous Message Alvaro Herrera 2015-06-23 17:33:22 Re: less log level for success dynamic background workers for 9.5