[xdebug-dev] xdebug xdebug/usefulstuff.c xdebug/xdebug.c xdebug/xdebug_handler_dbgp.c xdebug/xdebug_handler_gdb.c xdebug/xdebug_private.c xdebug/xdebug_private.h - Fixed problems with opening files - the filename could cause double free

From: Derick Rethans <derick[@]derickrethans.nl>
Date: Sun, 14 Jan 2007 18:00:59 +0100

Date: Sun Jan 14 18:00:59 CET 2007
User: Derick Rethans
Directory: xdebug

Log Message:
[4.00]
- Fixed problems with opening files - the filename could cause double free
  issues.
- Removed the bogus "xdebug.allowed_clients" setting - it was not implemented.
- Optimized used variable collection by switching to a linked list instead
  of a hash. This is about 30% faster, but it needed a quick conversion to
  hash in the case the information had to be shown to remove duplicate
  variable names.

Modified files:
           xdebug/usefulstuff.c (version: 1.38)
           xdebug/xdebug.c (version: 1.369)
           xdebug/xdebug_handler_dbgp.c (version: 1.116)
           xdebug/xdebug_handler_gdb.c (version: 1.85)
           xdebug/xdebug_private.c (version: 1.6)
           xdebug/xdebug_private.h (version: 1.23)

[FILE: /xdebug/usefulstuff.c]

===================================================================
RCS file: cvstemp,v
retrieving revision 1.37
retrieving revision 1.38
diff -u -r1.37 -r1.38
--- xdebug/usefulstuff.c:1.37 Tue Jan 02 15:02:36 2007 GMT
+++ xdebug/usefulstuff.c Sun Jan 14 16:00:58 2007 GMT
@@ -386,7 +386,7 @@
                 tmp_fname = xdstrdup(fname);
         }
         fh = fopen(tmp_fname, mode);
- if (fh) {
+ if (fh && new_fname) {
                 if (new_fname) {
                         *new_fname = tmp_fname;
                 } else {
@@ -421,7 +421,7 @@
         int r;
         FILE *fh;
         struct stat buf;
- char *tmp_fname;
+ char *tmp_fname = NULL;
 
         /* We're not doing any tricks for append mode... as that has atomic writes
          * anyway. And we ignore read mode as well. */
@@ -441,8 +441,9 @@
 
         if (r == -1) {
                 xdfree(tmp_fname);
+ tmp_fname = NULL;
                 /* 2. Cool, the file doesn't exist so we can open it without probs now. */
- fh = xdebug_open_file(fname, "w", extension, (char**) &new_fname);
+ fh = xdebug_open_file(fname, "w", extension, new_fname);
                 goto lock;
         }
 
@@ -450,8 +451,9 @@
         fh = xdebug_open_file(fname, "r+", extension, (char**) &tmp_fname);
         if (!fh) {
                 xdfree(tmp_fname);
+ tmp_fname = NULL;
                 /* 4. If fh == null we couldn't even open the file, so open a new one with a new name */
- fh = xdebug_open_file_with_random_ext(fname, "w", extension, (char**) &new_fname);
+ fh = xdebug_open_file_with_random_ext(fname, "w", extension, new_fname);
                 goto lock;
         }
         /* 5. It exists and we can open it, check if we can exclusively lock it. */
@@ -460,8 +462,9 @@
                 if (errno == EWOULDBLOCK) {
                         fclose(fh);
                         xdfree(tmp_fname);
+ tmp_fname = NULL;
                         /* 6. The file is in use, so we open one with a new name. */
- fh = xdebug_open_file_with_random_ext(fname, "w", extension, (char**) &new_fname);
+ fh = xdebug_open_file_with_random_ext(fname, "w", extension, new_fname);
                         goto lock;
                 }
         }
@@ -474,7 +477,7 @@
                  * the file and opens it again. There is a small race condition here...
                  */
                 flock(fileno(fh), LOCK_EX | LOCK_NB);
- if (new_fname) {
+ if (new_fname && tmp_fname) {
                         *new_fname = tmp_fname;
                         return fh;
                 }

[FILE: /xdebug/xdebug.c]

===================================================================
RCS file: cvstemp,v
retrieving revision 1.368
retrieving revision 1.369
diff -u -r1.368 -r1.369
--- xdebug/xdebug.c:1.368 Thu Jan 04 15:15:11 2007 GMT
+++ xdebug/xdebug.c Sun Jan 14 16:00:58 2007 GMT
@@ -219,11 +219,6 @@
         DUMP_TOK(session);
 }
 
-static PHP_INI_MH(OnUpdateAllowedClients)
-{
- return SUCCESS;
-}
-
 static PHP_INI_MH(OnUpdateIDEKey)
 {
         if (XG(ide_key)) {
@@ -315,7 +310,6 @@
 #endif
         STD_PHP_INI_BOOLEAN("xdebug.remote_autostart","0", PHP_INI_ALL, OnUpdateBool, remote_autostart, zend_xdebug_globals, xdebug_globals)
         STD_PHP_INI_ENTRY("xdebug.remote_log", "", PHP_INI_ALL, OnUpdateString, remote_log, zend_xdebug_globals, xdebug_globals)
- PHP_INI_ENTRY("xdebug.allowed_clients", "", PHP_INI_SYSTEM, OnUpdateAllowedClients)
         PHP_INI_ENTRY("xdebug.idekey", "", PHP_INI_ALL, OnUpdateIDEKey)
 
         /* Variable display settings */
@@ -636,7 +630,7 @@
         return SUCCESS;
 }
 
-static void xdebug_used_var_dtor(void *elem)
+static void xdebug_used_var_dtor(void *dummy, void *elem)
 {
         char *s = elem;
 
@@ -677,7 +671,7 @@
                 }
 
                 if (e->used_vars) {
- xdebug_hash_destroy(e->used_vars);
+ xdebug_llist_destroy(e->used_vars, NULL);
                 }
 
                 if (e->profile.call_list) {
@@ -1187,14 +1181,14 @@
         int j = op_array->size;
 
         if (!fse->used_vars) {
- fse->used_vars = xdebug_hash_alloc(64, xdebug_used_var_dtor);
+ fse->used_vars = xdebug_llist_alloc(xdebug_used_var_dtor);
         }
 
         /* Check parameters */
 # if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 1) || PHP_MAJOR_VERSION >= 6
         for (i = 0; i < fse->varc; i++) {
                 if (fse->var[i].name) {
- xdebug_hash_update(fse->used_vars, fse->var[i].name, strlen(fse->var[i].name), xdstrdup(fse->var[i].name));
+ xdebug_llist_insert_next(fse->used_vars, XDEBUG_LLIST_TAIL(fse->used_vars), xdstrdup(fse->var[i].name));
                 }
         }
 # endif
@@ -1207,29 +1201,24 @@
 
                 if (op_array->opcodes[i].op1.op_type == IS_CV) {
                         cv = zend_get_compiled_variable_name(op_array, op_array->opcodes[i].op1.u.var, &cv_len);
- xdebug_hash_update(fse->used_vars, cv, cv_len, xdstrdup(cv));
+ xdebug_llist_insert_next(fse->used_vars, XDEBUG_LLIST_TAIL(fse->used_vars), xdstrdup(cv));
                 }
                 if (op_array->opcodes[i].op2.op_type == IS_CV) {
                         cv = zend_get_compiled_variable_name(op_array, op_array->opcodes[i].op2.u.var, &cv_len);
- xdebug_hash_update(fse->used_vars, cv, cv_len, xdstrdup(cv));
+ xdebug_llist_insert_next(fse->used_vars, XDEBUG_LLIST_TAIL(fse->used_vars), xdstrdup(cv));
                 }
 #else
                 if (op_array->opcodes[i].opcode == ZEND_FETCH_R || op_array->opcodes[i].opcode == ZEND_FETCH_W) {
                         if (op_array->opcodes[i].op1.op_type == IS_CONST) {
                                 if (Z_TYPE(op_array->opcodes[i].op1.u.constant) == IS_STRING) {
- xdebug_hash_update(
- fse->used_vars,
- op_array->opcodes[i].op1.u.constant.value.str.val,
- op_array->opcodes[i].op1.u.constant.value.str.len,
- xdstrdup(op_array->opcodes[i].op1.u.constant.value.str.val)
- );
+ xdebug_llist_insert_next(fse->used_vars, XDEBUG_LLIST_TAIL(fse->used_vars), xdstrdup(op_array->opcodes[i].op1.u.constant.value.str.val));
                                 } else { /* unusual but not impossible situation */
                                         int use_copy;
                                         zval tmp_zval;
 
                                         zend_make_printable_zval(&(op_array->opcodes[i].op1.u.constant), &tmp_zval, &use_copy);
 
- xdebug_hash_update(fse->used_vars, tmp_zval.value.str.val, tmp_zval.value.str.len, xdstrdup(tmp_zval.value.str.val));
+ xdebug_llist_insert_next(fse->used_vars, XDEBUG_LLIST_TAIL(fse->used_vars), xdstrdup(tmp_zval.value.str.val));
 
                                         zval_dtor(&tmp_zval);
                                 }
@@ -1837,8 +1826,12 @@
                                 scope_nr--;
                         }
                         if (i->used_vars && i->used_vars->size) {
+ xdebug_hash *tmp_hash;
+
                                 xdebug_str_add(&str, xdebug_sprintf(formats[6], scope_nr), 1);
- xdebug_hash_apply_with_argument(i->used_vars, (void*) &html, dump_used_var_with_contents, (void *) &str);
+ tmp_hash = xdebug_used_var_hash_from_llist(i->used_vars);
+ xdebug_hash_apply_with_argument(tmp_hash, (void*) &html, dump_used_var_with_contents, (void *) &str);
+ xdebug_hash_destroy(tmp_hash);
                         }
                 }
 
@@ -2336,6 +2329,7 @@
 {
         xdebug_llist_element *le;
         function_stack_entry *i;
+ xdebug_hash *tmp_hash;
 
         array_init(return_value);
         le = XDEBUG_LLIST_TAIL(XG(stack));
@@ -2344,7 +2338,9 @@
         
         /* Add declared vars */
         if (i->used_vars) {
- xdebug_hash_apply(i->used_vars, (void *) return_value, attach_used_var_names);
+ tmp_hash = xdebug_used_var_hash_from_llist(i->used_vars);
+ xdebug_hash_apply(tmp_hash, (void *) return_value, attach_used_var_names);
+ xdebug_hash_destroy(tmp_hash);
         }
 }
 /* }}} */
@@ -2622,7 +2618,7 @@
         char *str_time;
         char *filename;
         char cwd[128];
- char *tmp_fname;
+ char *tmp_fname = NULL;
 
         if (fname && strlen(fname)) {
                 filename = xdstrdup(fname);

[FILE: /xdebug/xdebug_handler_dbgp.c]

===================================================================
RCS file: cvstemp,v
retrieving revision 1.115
retrieving revision 1.116
diff -u -r1.115 -r1.116
--- xdebug/xdebug_handler_dbgp.c:1.115 Tue Jan 02 15:02:37 2007 GMT
+++ xdebug/xdebug_handler_dbgp.c Sun Jan 14 16:00:58 2007 GMT
@@ -1816,7 +1816,6 @@
 static int attach_context_vars(xdebug_xml_node *node, xdebug_var_export_options *options, long context_id, long depth, void (*func)(void *, xdebug_hash_element*, void*) TSRMLS_DC)
 {
         function_stack_entry *fse;
- xdebug_hash *ht;
 
         /* right now, we only have zero or one, one being globals, which is
          * always the head of the stack */
@@ -1837,12 +1836,14 @@
 
         /* Here the context_id is 0 */
         if ((fse = xdebug_get_stack_frame(depth TSRMLS_CC))) {
- ht = fse->used_vars;
                 XG(active_symbol_table) = fse->symbol_table;
 
                 /* Only show vars when they are scanned */
- if (ht) {
- xdebug_hash_apply_with_argument(ht, (void *) node, func, (void *) options);
+ if (fse->used_vars) {
+ xdebug_hash *tmp_hash;
+ tmp_hash = xdebug_used_var_hash_from_llist(fse->used_vars);
+ xdebug_hash_apply_with_argument(tmp_hash, (void *) node, func, (void *) options);
+ xdebug_hash_destroy(tmp_hash);
                 }
 
 #ifdef ZEND_ENGINE_2
@@ -2177,7 +2178,7 @@
 
 char *xdebug_dbgp_get_revision(void)
 {
- return "$Revision: 1.115 $";
+ return "$Revision: 1.116 $";
 }
 
 static int xdebug_dbgp_cmdloop(xdebug_con *context TSRMLS_DC)

[FILE: /xdebug/xdebug_handler_gdb.c]

===================================================================
RCS file: cvstemp,v
retrieving revision 1.84
retrieving revision 1.85
diff -u -r1.84 -r1.85
--- xdebug/xdebug_handler_gdb.c:1.84 Tue Jan 02 15:02:37 2007 GMT
+++ xdebug/xdebug_handler_gdb.c Sun Jan 14 16:00:59 2007 GMT
@@ -544,6 +544,7 @@
         char *tmp;
         char *tmp_value;
         int len;
+ xdebug_hash *tmp_hash;
         TSRMLS_FETCH();
         
 /*
@@ -591,14 +592,18 @@
                 SENDMSG(h->socket, xdebug_sprintf("</params></function><file>%s</file><line>%d</line>", i->filename, i->lineno));
                 if (flags & XDEBUG_FRAME_FULL && i->used_vars) {
                         SSEND(h->socket, "<locals>");
- xdebug_hash_apply(i->used_vars, (void *) h, dump_used_var_with_contents);
+ tmp_hash = xdebug_used_var_hash_from_llist(i->used_vars);
+ xdebug_hash_apply(tmp_hash, (void *) h, dump_used_var_with_contents);
+ xdebug_hash_destroy(tmp_hash);
                         SSEND(h->socket, "</locals>");
                 }
                 SSEND(h->socket, "</stackframe>");
         } else {
                 SENDMSG(h->socket, xdebug_sprintf(")\n at %s:%d\n", i->filename, i->lineno));
                 if (flags & XDEBUG_FRAME_FULL && i->used_vars) {
- xdebug_hash_apply(i->used_vars, (void *) h, dump_used_var_with_contents);
+ tmp_hash = xdebug_used_var_hash_from_llist(i->used_vars);
+ xdebug_hash_apply(tmp_hash, (void *) h, dump_used_var_with_contents);
+ xdebug_hash_destroy(tmp_hash);
                         SSEND(h->socket, "\n");
                 }
         }
@@ -1161,22 +1166,23 @@
 static char* show_local_vars(xdebug_con *context, xdebug_arg *args, void (*func)(void *, xdebug_hash_element*))
 {
         function_stack_entry *i;
- xdebug_hash *ht;
+ xdebug_hash *tmp_hash;
         xdebug_gdb_options *options = (xdebug_gdb_options*) context->options;
         TSRMLS_FETCH();
 
         
         if (XDEBUG_LLIST_TAIL(XG(stack))) {
                 i = XDEBUG_LLIST_VALP(XDEBUG_LLIST_TAIL(XG(stack)));
- ht = i->used_vars;
 
                 /* Only show vars when they are scanned */
- if (ht) {
+ if (i->used_vars) {
                         if (options->response_format == XDEBUG_RESPONSE_XML) {
                                 SSEND(context->socket, "<xdebug><show>");
                         }
 
- xdebug_hash_apply(ht, (void *) context, func);
+ tmp_hash = xdebug_used_var_hash_from_llist(i->used_vars);
+ xdebug_hash_apply(tmp_hash, (void *) context, func);
+ xdebug_hash_destroy(tmp_hash);
 
                         if (options->response_format == XDEBUG_RESPONSE_XML) {
                                 SSEND(context->socket, "</show></xdebug>\n");
@@ -1335,7 +1341,7 @@
 
 char *xdebug_gdb_get_revision(void)
 {
- return "$Revision: 1.84 $";
+ return "$Revision: 1.85 $";
 }
 
 int xdebug_gdb_init(xdebug_con *context, int mode)

[FILE: /xdebug/xdebug_private.c]

===================================================================
RCS file: cvstemp,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- xdebug/xdebug_private.c:1.5 Thu Dec 28 21:58:37 2006 GMT
+++ xdebug/xdebug_private.c Sun Jan 14 16:00:59 2007 GMT
@@ -72,3 +72,23 @@
                 return NULL;
         }
 }
+
+static void xdebug_used_var_hash_from_llist_dtor(void *data)
+{
+ /* We are not freeing anything as the list creating didn't copy the data */
+}
+
+xdebug_hash* xdebug_used_var_hash_from_llist(xdebug_llist *list)
+{
+ xdebug_hash *tmp;
+ xdebug_llist_element *le;
+ char *var_name;
+
+ tmp = xdebug_hash_alloc(32, xdebug_used_var_hash_from_llist_dtor);
+ for (le = XDEBUG_LLIST_HEAD(list); le != NULL; le = XDEBUG_LLIST_NEXT(le)) {
+ var_name = (char*) XDEBUG_LLIST_VALP(le);
+ xdebug_hash_add(tmp, var_name, strlen(var_name), var_name);
+ }
+
+ return tmp;
+}

[FILE: /xdebug/xdebug_private.h]

===================================================================
RCS file: cvstemp,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -r1.22 -r1.23
--- xdebug/xdebug_private.h:1.22 Thu Dec 28 21:58:37 2006 GMT
+++ xdebug/xdebug_private.h Sun Jan 14 16:00:59 2007 GMT
@@ -163,7 +163,7 @@
         int varc;
         xdebug_var *var;
         zval *return_value;
- xdebug_hash *used_vars;
+ xdebug_llist *used_vars;
         HashTable *symbol_table;
 
         /* tracing properties */
@@ -189,6 +189,8 @@
 function_stack_entry *xdebug_get_stack_frame(int nr TSRMLS_DC);
 function_stack_entry *xdebug_get_stack_tail(TSRMLS_D);
 
+xdebug_hash* xdebug_used_var_hash_from_llist(xdebug_llist *list);
+
 #endif
 
 
Received on Sun Jan 14 2007 - 18:01:56 GMT

This archive was generated by hypermail 2.2.0 : Sun Jun 24 2018 - 04:00:03 BST