[gnutls-dev] RE: constification patch

ZIGLIO, Frediano, VF-IT Frediano.Ziglio at vodafone.com
Wed Dec 28 14:00:03 CET 2005


> 
> "ZIGLIO, Frediano, VF-IT" <Frediano.Ziglio at vodafone.com> writes:
> 
> > This simple patch add some const definition. It also assure 
> that global
> > variables are really const.
> 
> I installed most of the patch.
> 
> The lib/minitasn1/ changes didn't look right, so I didn't install
> them.  First, 'inline' can't be used because libtasn1 is supposed to
> be C89 portable.  Also, isn't it possible to solve this without
> creating new *_const functions?  Moving typecasts into a special
> function just hide the problems.  I think warnings are better here
> because then we know that there is a problem.
> 
> Thanks,
> Simon
> 

Hi Simon,
  I would never expected that you ever apply the patch so fast!

Well, I came from "C++ world" were const is very important but easier to
use than in C however after some year developing OpenSource C
applications I have to say that const is important even on C... The
*_const functions reflect how C++ work... but in C. In C++ you can
declare two functions like

  char *strchr(char *, int);
  const char *strchr(const char *, int);

without problems, in C this give error... so I added _const. The
advantage is that compiler help you. Usually many people do not pay many
attention to warning but some time ignoring warnings can lead to
disaster... so usually I remove any type of warning I found. In this
case I call a function for no-const object knowing that this function do
not change the data.

Well, after explaining my reasons let's came to a possible (and
portable) fix. A way to fix this issue is to use static inline for
compilers that support this or define for others. As I think that many
people will use gcc so they will see the error I would convert 

static inline const node_asn *
_asn1_find_up_const(const node_asn *node)
{
	return _asn1_find_up((node_asn*) node);
}

to 

#if defined(__GNUC__) && __GNUC__ >= 3
static inline const node_asn *
_asn1_find_up_const(const node_asn *node)
{
	return _asn1_find_up((node_asn*) node);
}
#else
#define _asn1_find_up_const(node) _asn1_find_up((node_asn*) (node))
#endif

freddy77




More information about the Gnutls-devel mailing list