lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Aurora once stated:
> Dear,
> 
> there is a strange problem in my C code, as below:
> 
> program:
> 
> 	printf("---%p\n", soap->header);
> 	soap_header(soap);
> 	printf("---%p\n", soap->header);
> 
> soap_header function:
> 
> void soap_header(struct soap *soap)
> {
> 	printf("soap_header enter %p\n", soap->header);
> 	if(!soap->header)
> 	{
> 		printf("soap_header in IF %p\n", soap->header);
> 		if((soap->header = (struct SOAP_ENV__Header*)soap_malloc(soap,
> 		        sizeof(struct SOAP_ENV__Header))))

  The if statement is assigning soap->header to the result of soap_malloc(),
and testing that assignment against NULL.  Since a pointer is being passed in to
soap_header(), that function can change the underlying structure being
pointed to by the passed in pointer.

The code fragment could be rewritten as:

  soap->header = (struct SOAP_ENV__Header *)soap_malloc(soap,sizeof(struct SOAP_ENV_Header));
  if (soap->header != NULL)
    soap_default_SOAP_ENV__Header(soap,soap->header);

Further more, soap_malloc() may end up changing the structure pointed to by
soap, but you would have to check the source code to make sure.

  -spc (Sorry for the off-topic reply ... )