Author: chromatic
Date: Sat Nov  8 23:41:49 2008
New Revision: 32463

Modified:
   trunk/src/pmc/nci.pmc

Log:
[PMC] Fixed a read of unitialized memory in NCI PMC reported by Valgrind.
Tidied some of the code while I was at it.

Modified: trunk/src/pmc/nci.pmc
==============================================================================
--- trunk/src/pmc/nci.pmc       (original)
+++ trunk/src/pmc/nci.pmc       Sat Nov  8 23:41:49 2008
@@ -22,14 +22,13 @@
 
 void pcc_params(PARROT_INTERP, STRING *sig, Parrot_NCI_attributes * const 
nci_info);
 void pcc_params(PARROT_INTERP, STRING *sig, Parrot_NCI_attributes * const 
nci_info) {
-        size_t i;
-        size_t j = 0;
-        char * sig_c = string_to_cstring(interp, sig);
-        size_t sig_length = strlen(sig_c);
-        char * param_sig = (char *) mem_sys_allocate(sig_length);
-        STRING *params;
+        char   *sig_c      = string_to_cstring(interp, sig);
+        size_t  sig_length = strlen(sig_c);
+        char   *param_sig  = mem_allocate_n_zeroed_typed(sig_length, char);
+        size_t  j          = 0;
+        size_t  i;
 
-        for (i=1; i < sig_length; i++) {
+        for (i = 1; i < sig_length; i++) {
             switch (sig_c[i]) {
                 case '0':    /* null ptr or such - doesn't consume a reg */
                     break;
@@ -72,18 +71,24 @@
                     param_sig[j++] = 'S';
                     break;
                 default:
-                    Parrot_ex_throw_from_c_args(interp, NULL, 
EXCEPTION_JIT_ERROR,
+                    Parrot_ex_throw_from_c_args(interp, NULL,
+                        EXCEPTION_JIT_ERROR,
                         "Unknown param Signature %c\n", sig_c[i]);
                     break;
             }
         }
-        PARROT_ASSERT(j <= sig_length);
 
-        params = string_make(interp, param_sig, strlen(param_sig), NULL, 
PObj_constant_FLAG);
+        PARROT_ASSERT(j <= sig_length);
 
-        nci_info->pcc_params_signature = params;
+        /* use only the signature-significant part of the string buffer */
+        if (j) {
+            nci_info->pcc_params_signature = string_make(interp, param_sig, j,
+                NULL, PObj_constant_FLAG);
+            mem_sys_free(param_sig);
+        }
+        else
+            nci_info->pcc_params_signature = CONST_STRING(interp, "");
 
-        mem_sys_free(param_sig);
         string_cstring_free(sig_c);
     }
 
@@ -162,13 +167,14 @@
 
     VTABLE void set_pointer_keyed_str(STRING *key, void *func) {
         Parrot_NCI_attributes * const nci_info = PARROT_NCI(SELF);
-        int jitted = 0;
-        char *key_c;
+        int                           jitted   = 0;
+        char                         *key_c;
 
         /* Store the original function and signature. */
         PMC_struct_val(SELF) = func;
-        key_c = string_to_cstring(INTERP, key);
-        nci_info->signature  = string_make(interp, key_c, strlen(key_c), NULL, 
PObj_constant_FLAG);
+        key_c                = string_to_cstring(INTERP, key);
+        nci_info->signature  = string_make(interp, key_c, strlen(key_c),
+                                    NULL, PObj_constant_FLAG);
         string_cstring_free(key_c);
         pcc_params(INTERP, key, nci_info);
 
@@ -176,9 +182,11 @@
         nci_info->arity      = string_length(INTERP, key) - 1;
 
         /* Build call function. */
-        nci_info->func       = (PMC *)(build_call_func(INTERP, SELF, key, 
&jitted));
+        nci_info->func       = (PMC *)(build_call_func(INTERP, SELF,
+                                            key, &jitted));
         nci_info->jitted = jitted;
     }
+
 /*
 
 =item C<void mark()>

Reply via email to