[libvirt] [PATCH] esx: Fix and improve the libcurl debug callback

Matthias Bolte matthias.bolte at googlemail.com
Sun Jan 3 19:40:15 UTC 2010


The data passed to the callback is not guaranteed to be zero terminated,
take care of that by coping the data and adding a zero terminator.

Also dump the data for other types than CURLINFO_TEXT.

Set CURLOPT_VERBOSE to 1 so the debug callback is called when enabled.
---
 src/esx/esx_vi.c |   43 ++++++++++++++++++++++++++++++++++---------
 1 files changed, 34 insertions(+), 9 deletions(-)

diff --git a/src/esx/esx_vi.c b/src/esx/esx_vi.c
index a7498f0..bad987c 100644
--- a/src/esx/esx_vi.c
+++ b/src/esx/esx_vi.c
@@ -181,27 +181,50 @@ static int
 esxVI_CURL_Debug(CURL *curl ATTRIBUTE_UNUSED, curl_infotype type,
                  char *info, size_t size, void *data ATTRIBUTE_UNUSED)
 {
+    char *buffer = NULL;
+
+    /*
+     * The libcurl documentation says:
+     *
+     *    The data pointed to by the char * passed to this function WILL NOT
+     *    be zero terminated, but will be exactly of the size as told by the
+     *    size_t argument.
+     *
+     * To handle this properly in order to pass the info string to VIR_DEBUG
+     * a zero terminated copy of the info string has to be allocated.
+     */
+    if (VIR_ALLOC_N(buffer, size + 1) < 0) {
+        return 0;
+    }
+
+    if (virStrncpy(buffer, info, size, size + 1) == NULL) {
+        VIR_FREE(buffer);
+        return 0;
+    }
+
     switch (type) {
       case CURLINFO_TEXT:
-        VIR_DEBUG0("CURLINFO_TEXT");
-        fwrite(info, 1, size, stderr);
-        printf("\n\n");
+        if (size > 0 && buffer[size - 1] == '\n') {
+            buffer[size - 1] = '\0';
+        }
+
+        VIR_DEBUG("CURLINFO_TEXT [[[[%s]]]]", buffer);
         break;
 
       case CURLINFO_HEADER_IN:
-        VIR_DEBUG0("CURLINFO_HEADER_IN");
+        VIR_DEBUG("CURLINFO_HEADER_IN [[[[%s]]]]", buffer);
         break;
 
       case CURLINFO_HEADER_OUT:
-        VIR_DEBUG0("CURLINFO_HEADER_OUT");
+        VIR_DEBUG("CURLINFO_HEADER_OUT [[[[%s]]]]", buffer);
         break;
 
       case CURLINFO_DATA_IN:
-        VIR_DEBUG0("CURLINFO_DATA_IN");
+        VIR_DEBUG("CURLINFO_DATA_IN [[[[%s]]]]", buffer);
         break;
 
       case CURLINFO_DATA_OUT:
-        VIR_DEBUG0("CURLINFO_DATA_OUT");
+        VIR_DEBUG("CURLINFO_DATA_OUT [[[[%s]]]]", buffer);
         break;
 
       default:
@@ -209,6 +232,8 @@ esxVI_CURL_Debug(CURL *curl ATTRIBUTE_UNUSED, curl_infotype type,
         break;
     }
 
+    VIR_FREE(buffer);
+
     return 0;
 }
 #endif
@@ -338,8 +363,8 @@ esxVI_Context_Connect(virConnectPtr conn, esxVI_Context *ctx, const char *url,
     curl_easy_setopt(ctx->curl_handle, CURLOPT_WRITEFUNCTION,
                      esxVI_CURL_WriteBuffer);
 #if ESX_VI__CURL__ENABLE_DEBUG_OUTPUT
-    curl_easy_setopt(ctx->curl_handle, CURLOPT_DEBUGFUNCTION,
-                     esxVI_CURL_Debug);
+    curl_easy_setopt(ctx->curl_handle, CURLOPT_DEBUGFUNCTION, esxVI_CURL_Debug);
+    curl_easy_setopt(ctx->curl_handle, CURLOPT_VERBOSE, 1);
 #endif
 
     if (virMutexInit(&ctx->curl_lock) < 0) {
-- 
1.6.0.4




More information about the libvir-list mailing list