[libvirt] [libvirt-php] Fix get_xml when xpath is null

Hi all: To my surprise, it seems that passing null to php as a string parameter, will set the pointer which retrive it to an empty string "" , but not NULL. get_xml_from_xpath() doesn't work correctly since an empty string is passed as the xpath argument, and libxml will complain "Invalid expression" I know seldom about xpath, I'm not sure if it is the correct way to fix it. BTW, what xpath shall I pass to get the entire xml? --- src/libvirt-php.c | 25 +++++++++++++++++++++++++ 1 files changed, 25 insertions(+), 0 deletions(-) diff --git a/src/libvirt-php.c b/src/libvirt-php.c index ce9d0b9..2a77423 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -1205,6 +1205,11 @@ char *get_string_from_xpath(char *xml, char *xpath, zval **val, int *retVal) int ret = 0, i; char *value, key[8] = { 0 }; + if ((xpath == NULL) || (xml == NULL)) + { + return NULL; + } + xp = xmlCreateDocParserCtxt( (xmlChar *)xml ); if (!xp) { if (retVal) @@ -1691,6 +1696,10 @@ PHP_FUNCTION(libvirt_domain_get_xml_desc) int retval = -1; GET_DOMAIN_FROM_ARGS("rs|l",&zdomain,&xpath,&xpath_len,&flags); + if (xpath_len < 1) + { + xpath = NULL; + } xml=virDomainGetXMLDesc(domain->domain,flags); if (xml==NULL) { @@ -3123,6 +3132,10 @@ PHP_FUNCTION(libvirt_storagevolume_get_xml_desc) int retval = -1; GET_VOLUME_FROM_ARGS("rs|l",&zvolume,&xpath,&xpath_len,&flags); + if (xpath_len < 1) + { + xpath = NULL; + } xml=virStorageVolGetXMLDesc(volume->volume,flags); if (xml==NULL) { @@ -3338,6 +3351,10 @@ PHP_FUNCTION(libvirt_storagepool_get_xml_desc) int retval = -1; GET_STORAGEPOOL_FROM_ARGS("rs|l", &zpool, &xpath, &xpath_len, &flags); + if (xpath_len < 1) + { + xpath = NULL; + } xml = virStoragePoolGetXMLDesc (pool->pool, flags); if (xml == NULL) @@ -4136,6 +4153,10 @@ PHP_FUNCTION(libvirt_nodedev_get_xml_desc) int retval = -1; GET_NODEDEV_FROM_ARGS("r|s",&znodedev,&xpath,&xpath_len); + if (xpath_len < 1) + { + xpath = NULL; + } xml=virNodeDeviceGetXMLDesc(nodedev->device, 0); if ( xml == NULL ) { @@ -4576,6 +4597,10 @@ PHP_FUNCTION(libvirt_network_get_xml_desc) int retval = -1; GET_NETWORK_FROM_ARGS("r|s",&znetwork,&xpath,&xpath_len); + if (xpath_len < 1) + { + xpath = NULL; + } xml=virNetworkGetXMLDesc(network->network, 0); -- 1.7.3.4

On 04/13/2011 06:10 AM, Lyre wrote:
Hi all:
To my surprise, it seems that passing null to php as a string parameter, will set the pointer which retrive it to an empty string "" , but not NULL. get_xml_from_xpath() doesn't work correctly since an empty string is passed as the xpath argument, and libxml will complain "Invalid expression"
I know seldom about xpath, I'm not sure if it is the correct way to fix it.
BTW, what xpath shall I pass to get the entire xml?
Hi Lyre, thanks for the information however I was always using the NULL value as second argument for the get_xml_desc(), like e.g.: $entire_xml = libvirt_domain_get_xml_desc($dom, NULL); My development and testing is done on Fedora 14 and php-5.3.6-1.fc14.i686 version of the package and it's working fine for me. However the patch you sent to the list seems better and it's working fine so I've pushed it to the repository right now. Just for the record, what did you mean by passing NULL to php as a string parameter? To pass as 'null' ? I'm using it the way described above and 'null' is required only for libvirt_connect() since there are some issues with passing NULL to libvirt_connect(). We still may need to fix the libvirt_connect() function, that's true. Michal -- Michal Novotny <minovotn@redhat.com>, RHCE Virtualization Team (xen userspace), Red Hat

于 2011年04月13日 15:32, Michal Novotny 写道:
On 04/13/2011 06:10 AM, Lyre wrote:
Hi all:
To my surprise, it seems that passing null to php as a string parameter, will set the pointer which retrive it to an empty string "" , but not NULL. get_xml_from_xpath() doesn't work correctly since an empty string is passed as the xpath argument, and libxml will complain "Invalid expression"
I know seldom about xpath, I'm not sure if it is the correct way to fix it.
BTW, what xpath shall I pass to get the entire xml?
Hi Lyre, thanks for the information however I was always using the NULL value as second argument for the get_xml_desc(), like e.g.:
$entire_xml = libvirt_domain_get_xml_desc($dom, NULL);
My development and testing is done on Fedora 14 and php-5.3.6-1.fc14.i686 version of the package and it's working fine for me. However the patch you sent to the list seems better and it's working fine so I've pushed it to the repository right now.
Just for the record, what did you mean by passing NULL to php as a string parameter? To pass as 'null' ? I'm using it the way described above and 'null' is required only for libvirt_connect() since there are some issues with passing NULL to libvirt_connect(). We still may need to fix the libvirt_connect() function, that's true.
Michal
Hi Michal: I'm testing it on sles 11 sp1 with php 5.2.6. Let's take libvirt_domains_get_xml_desc as an example: when I called $entire_xml = libvirt_domain_get_xml_desc($dom, NULL); In the C code: PHP_FUNCTION(libvirt_domain_get_xml_desc) { char *xpath = NULL; int xpath_len; /* ... */ GET_DOMAIN_FROM_ARGS("rs|l",&zdomain,&xpath,&xpath_len,&flags); /* other stuff */ } After fetching arguments, xpath point to an empty string "". So that it was not NULL anymore. That's strange, or maybe I have misunderstood the argument passing mechanism.

On 04/13/2011 09:50 AM, Lyre wrote:
于 2011年04月13日 15:32, Michal Novotny 写道:
On 04/13/2011 06:10 AM, Lyre wrote:
Hi all:
To my surprise, it seems that passing null to php as a string parameter, will set the pointer which retrive it to an empty string "" , but not NULL. get_xml_from_xpath() doesn't work correctly since an empty string is passed as the xpath argument, and libxml will complain "Invalid expression"
I know seldom about xpath, I'm not sure if it is the correct way to fix it.
BTW, what xpath shall I pass to get the entire xml?
Hi Lyre, thanks for the information however I was always using the NULL value as second argument for the get_xml_desc(), like e.g.:
$entire_xml = libvirt_domain_get_xml_desc($dom, NULL);
My development and testing is done on Fedora 14 and php-5.3.6-1.fc14.i686 version of the package and it's working fine for me. However the patch you sent to the list seems better and it's working fine so I've pushed it to the repository right now.
Just for the record, what did you mean by passing NULL to php as a string parameter? To pass as 'null' ? I'm using it the way described above and 'null' is required only for libvirt_connect() since there are some issues with passing NULL to libvirt_connect(). We still may need to fix the libvirt_connect() function, that's true.
Michal
Hi Michal:
I'm testing it on sles 11 sp1 with php 5.2.6.
Let's take libvirt_domains_get_xml_desc as an example:
when I called $entire_xml = libvirt_domain_get_xml_desc($dom, NULL);
In the C code:
PHP_FUNCTION(libvirt_domain_get_xml_desc) { char *xpath = NULL; int xpath_len; /* ... */
GET_DOMAIN_FROM_ARGS("rs|l",&zdomain,&xpath,&xpath_len,&flags);
/* other stuff */ }
After fetching arguments, xpath point to an empty string "". So that it was not NULL anymore.
That's strange, or maybe I have misunderstood the argument passing mechanism.
Hi Lyre, I can see your point and since you're having different version of php it's possible the version I'm having had a bug that xpath was not empty string. I guess passing mechanism could change in next versions of PHP so I'm not saying that it's bad or anything. I'm just saying I didn't run into those issues myself but since your patch fixes the issue for you and it didn't break anything on my setup, then I guess it's good and I pushed it to the repository already. Michal -- Michal Novotny <minovotn@redhat.com>, RHCE Virtualization Team (xen userspace), Red Hat
participants (2)
-
Lyre
-
Michal Novotny