Subject: Re: [PATCH] Make pointed-to ares_options data const

Re: [PATCH] Make pointed-to ares_options data const

From: Brad House <brad_at_mainstreetsoftworks.com>
Date: Wed, 16 May 2007 21:55:15 -0400

> It seems that all the pointed-to data in ares_options is copied when
> ares_init_options is called. So, it would seem to be best if the
> pointers in the ares_options structure pointed to constant data so I
> can write something like this without breaking any const rules (at
> least in C++; I'm not sure what the current C rules are):
>
> // Should be okay to pass type "const char *" here since
> // copied anyway.
> options.lookups = "bf";
>
> (BTW, this and my previous patch were against c-ares-1.3.1.)

Since the parameters aren't touched by ares_init_options() doesn't
mean that all the members of the struct should be const. It
means that the struct itself passed to ares_init_options() should
be const.

Think of if someone is copying args from an initialized channel that
may be closed before the options struct is used. If you didn't
options.lookups = strdup(channel->lookups), and instead did
options.lookups = channel->lookups , when the channel is
destroyed, the pointer would disappear. And if you did strdup(),
with your const settings, you wouldn't be able to free() that
memory without first casting off the 'const', which is definitely
considered a bad practice.

If you look at the patch I recently submitted, you'll see the
use argument for this (ares_save_options() along with associated
ares_destroy_options()), as well as marking the options parameter
as const for ares_init_options().

-Brad
Received on 2007-05-17