2

This is a dereference of null pointer problem - in both the ANSI C & gSoap domains:

I am using the following public WSDL:

http://www.mobilefish.com/services/web_service/countries.php?wsdl

and have tested its behavior using soapUI.
I created client-side only ANSI C bindings using the wsdl2h and soapcpp2 utilities.

The problem:

In previous gsoap projects, results structures in the client soap_call functions (the fifth argument) required no initialization other than something like:

struct ns2__countryInfoByIanaResponse out, *pOut
pOut= &out;

this has always been sufficient until this project.
The client soap_call looks like this:

soap_call_ns2__countryInfoByIana(&soap, NULL, NULL, pIn, pOut); /* SOAP 1.2 RPC return element...*/

pIn for this project is defined as a char *, populated with a two character IANA code such as "us", or "nz". The return structure pOut for this particular call is shaped like this:

struct ns2__countryInfoByIanaResponse
{
    struct ns1__CountryData *countryinfo;
}

With ns1__CountryData shaped like this:

struct ns1__CountryData
{
    char *ianacode; /* required element of type xsd:string */
    char *countryname;  /* required element of type xsd:string */
    float latitude; /* required element of type xsd:float */
    float longitude;    /* required element of type xsd:float */
};

A call to this function from my application is therefore set up like this:

//declare response structure:
struct ns2__countryInfoByIanaResponse o, *pO;

void main(void)
{
   pO = &o;
   if(GetCountryInfo(buf, pO)==0)
   {
      pO->countryinfo->countryname; //Error Occurs Here...
   }                                
}

The error occurs at pO->countryinfo as a dereference of null pointer

GetCountryInfo is defined here:

int DLL_EXPORT GetCountryInfo(char *pIn, struct ns2__countryInfoByIanaResponse *pOut)
{

    int status = 0;
    size_t len=2048;
    char buf[2048];

    if (soap_call_ns2__countryInfoByIana(&soap, NULL, NULL, pIn, pOut)== SOAP_OK)
    {
        status = 0;
    }
    else
    {
        //soap_print_fault(&soap, stderr);
        soap_sprint_fault(&soap, buf, len);
        MessagePopup("soap error", buf);
        status = 1;
    }
    return status;
}

Other gSoap projects using similar output structure shapes (i.e. structures containing structures containing char *) returned fully populated results when initialized with nothing other than what I have shown above.

Any ideas? Please let me know if I can provide any further details. Thanks.

4

1 回答 1

1

在我看来,肥皂服务器有一个错误。countryInfoByIana 函数的示例肥皂响应如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
        xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
        SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <SOAP-ENV:Body>
    <SOAP-ENV:countryInfoByIanaResponse>
      <return>
        <ianacode xsi:type="xsd:string">nz</ianacode>
        <countryname xsi:type="xsd:string">New Zealand</countryname>
        <latitude xsi:type="xsd:float">-40.900558</latitude>
        <longitude xsi:type="xsd:float">174.885971</longitude>
      </return>
    </SOAP-ENV:countryInfoByIanaResponse>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

<SOAP-ENV:countryInfoByIanaResponse>应该有不同的命名空间。

这是 WSDL 的一部分,它包含相同的(无效的)名称空间。

<operation name="countryInfoByIana">
  <soap:operation soapAction="http://schemas.xmlsoap.org/soap/envelope/#Countries#countryInfoByIana" />
  <input>
    <soap:body use="encoded" namespace="http://schemas.xmlsoap.org/soap/envelope/" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
  </input>
  <output>
    <soap:body use="encoded" namespace="http://schemas.xmlsoap.org/soap/envelope/" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
  </output>
</operation>

编辑:

关于你为什么soapUI工作正常的问题;soapUI 可能不会像 gsoap 那样验证返回值。

我设法让程序在我的电脑上使用 gsoap 2.7 成功:

在 soapClient.c 第 56 行中,更改此行:

//soap_get_ns2__countryInfoByIanaResponse(soap, _param_1, "ns2:countryInfoByIanaResponse", "");
soap_get_ns2__countryInfoByIanaResponse(soap, _param_1, "SOAP-ENV:countryInfoByIanaResponse", "");

在 soapC.c 第 1470 行中,更改此行:

//if (soap_in_PointerTons1__CountryData(soap, "countryinfo", &a->countryinfo, "ns1:CountryData"))
if (soap_in_PointerTons1__CountryData(soap, "return", &a->countryinfo, "ns1:CountryData"))//return

但我认为你不应该以这种方式解决问题。不仅因为这两个文件都生成了,所以当你再次生成它时你会丢失你的更改。

于 2011-04-22T23:02:42.807 回答