[committed] Make main more readable

Tom de Vries tdevries@suse.de
Thu Mar 18 10:30:00 GMT 2021


On 3/17/21 6:28 PM, Jakub Jelinek wrote:
> On Wed, Mar 17, 2021 at 02:37:57PM +0100, Tom de Vries wrote:
>> On 3/17/21 2:18 PM, Jakub Jelinek wrote:
>>> On Wed, Mar 17, 2021 at 02:14:45PM +0100, Tom de Vries wrote:
>>>> @@ -17048,10 +17050,12 @@ main (int argc, char *argv[])
>>>>    outfile = NULL;
>>>>    hardlink = false;
>>>>    parse_args (argc, argv, &hardlink, &outfile);
>>>> +  nr_files = argc - optind;
>>>> +  files = (const char **)&argv[optind];
>>>>  
>>>> -  if (optind == argc || optind + 1 == argc)
>>>> +  if (nr_files <= 1)
>>>>      {
>>>> -      const char *file = optind == argc ? "a.out" : argv[optind];
>>>> +      const char *file = nr_files == 0 ? "a.out" : files[0];
>>>
>>> Isn't that aliasing violation?
>>
>> Sorry, I don't see it, can you be specific about which entity is
>> accessed with an incompatible type?
> 
> char * and const char * are different types from C aliasing POV I think.
> Now, as these are arguments of main and argv can be const char **
> or char ** interchangeably, what is the dynamic type is a little bit fuzzy,
> but I'd say mixing accesses to argv array elements, accessing some of them
> through char * effective type (e.g. in getopt_long) and others through
> const char * effective type is problematic.
> Making files char ** and then casting if needed (say (const char *)(files[0]))
> is fine of course.

I've prepared a patch implementing that suggestion.  Does that address
your concerns?

[ FWIW, I've read through a C99 draft pdf to refresh my memory on this
topic.  Looking at the effective type of **argv, AFAIU it falls into the
catagory "For all other accesses to an object having no declared type,
the effective type of the object is simply the type of the lvalue used
for the access".  In other words, the effective type is char.

Then I read:
...
An object shall have its stored value accessed only by an lvalue
expression that has one of the following types:
— a qualified version of a type compatible with the effective type of
  the object,
...

So, const char is a qualified version of a type char compatible with the
effective type of the object (char).

So I still don't see the problem. ]

Thanks,
- Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-Change-files-var-in-main-to-char.patch
Type: text/x-patch
Size: 1770 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/dwz/attachments/20210318/5b49524e/attachment.bin>


More information about the Dwz mailing list