]> granicus.if.org Git - php/commitdiff
Switch asprintf to spprintf in phpdbg opcode dump
authorNikita Popov <nikic@php.net>
Fri, 17 Jul 2015 14:49:34 +0000 (16:49 +0200)
committerNikita Popov <nikic@php.net>
Fri, 17 Jul 2015 14:53:07 +0000 (16:53 +0200)
Also use %td where appropriate, a lot of the values are ptrdiff
based.

Fix a leak in phpdbg_frame.c.

sapi/phpdbg/phpdbg_frame.c
sapi/phpdbg/phpdbg_opcode.c
sapi/phpdbg/phpdbg_print.c
sapi/phpdbg/phpdbg_utils.c

index 189b3b20faac97f835bf6c2b3f74cf0e4a0fec92..4f0e5e88b0901b50fcff98b0f21a73f644dbe5a4 100644 (file)
@@ -164,7 +164,11 @@ static void phpdbg_dump_prototype(zval *tmp) /* {{{ */
                        }
                        ++j;
 
-                       php_printf("%s", phpdbg_short_zval_print(argstmp, 40));
+                       {
+                               char *arg_print = phpdbg_short_zval_print(argstmp, 40);
+                               php_printf("%s", arg_print);
+                               efree(arg_print);
+                       }
 
                        phpdbg_xml("</arg>");
                } ZEND_HASH_FOREACH_END();
index b64fa5c491596bbc27416756e0ce500902295bf1..8a58a44f0bbb67e930001b00d7bb4bdb876fd2b4 100644 (file)
@@ -34,12 +34,14 @@ static inline char *phpdbg_decode_op(zend_op_array *ops, znode_op *op, uint32_t
        switch (type) {
                case IS_CV: {
                        zend_string *var = ops->vars[EX_VAR_TO_NUM(op->var)];
-                       asprintf(&decode, "$%.*s%c", ZSTR_LEN(var) <= 19 ? (int) ZSTR_LEN(var) : 18, ZSTR_VAL(var), ZSTR_LEN(var) <= 19 ? 0 : '+');
+                       spprintf(&decode, 0, "$%.*s%c",
+                               ZSTR_LEN(var) <= 19 ? (int) ZSTR_LEN(var) : 18,
+                               ZSTR_VAL(var), ZSTR_LEN(var) <= 19 ? 0 : '+');
                } break;
 
                case IS_VAR:
                case IS_TMP_VAR: {
-                       asprintf(&decode, "@%" PRIu32, EX_VAR_TO_NUM(op->var) - ops->last_var);
+                       spprintf(&decode, 0, "@%td", EX_VAR_TO_NUM(op->var) - ops->last_var);
                } break;
                case IS_CONST: {
                        zval *literal = RT_CONSTANT(ops, *op);
@@ -58,13 +60,13 @@ char *phpdbg_decode_opline(zend_op_array *ops, zend_op *op) /*{{{ */
        switch (op->opcode) {
        case ZEND_FAST_CALL:
                if (op->extended_value != 0) {
-                       asprintf(&decode[0], "FAST_CALL<%s>",
+                       spprintf(&decode[0], 0, "FAST_CALL<%s>",
                                op->extended_value == ZEND_FAST_CALL_FROM_CATCH ? "FROM_CATCH" : "FROM_FINALLY");
                }
                break;
        case ZEND_FAST_RET:
                if (op->extended_value != 0) {
-                       asprintf(&decode[0], "FAST_RET<%s>",
+                       spprintf(&decode[0], 0, "FAST_RET<%s>",
                                op->extended_value == ZEND_FAST_RET_TO_CATCH ? "TO_CATCH" : "TO_FINALLY");
                }
                break;
@@ -74,14 +76,14 @@ char *phpdbg_decode_opline(zend_op_array *ops, zend_op *op) /*{{{ */
        switch (op->opcode) {
        case ZEND_JMP:
        case ZEND_FAST_CALL:
-               asprintf(&decode[1], "J%ld", OP_JMP_ADDR(op, op->op1) - ops->opcodes);
+               spprintf(&decode[1], 0, "J%td", OP_JMP_ADDR(op, op->op1) - ops->opcodes);
                break;
 
        case ZEND_INIT_FCALL:
        case ZEND_RECV:
        case ZEND_RECV_INIT:
        case ZEND_RECV_VARIADIC:
-               asprintf(&decode[1], "%" PRIu32, op->op1.num);
+               spprintf(&decode[1], 0, "%" PRIu32, op->op1.num);
                break;
 
        default:
@@ -92,7 +94,9 @@ char *phpdbg_decode_opline(zend_op_array *ops, zend_op *op) /*{{{ */
        /* OP2 */
        switch (op->opcode) {
        case ZEND_JMPZNZ:
-               asprintf(&decode[2], "J%ld or J%ld", OP_JMP_ADDR(op, op->op2) - ops->opcodes, ZEND_OFFSET_TO_OPLINE(op, op->extended_value) - ops->opcodes);
+               spprintf(&decode[2], 0, "J%td or J%td",
+                       OP_JMP_ADDR(op, op->op2) - ops->opcodes,
+                       ZEND_OFFSET_TO_OPLINE(op, op->extended_value) - ops->opcodes);
                break;
 
        case ZEND_JMPZ:
@@ -101,13 +105,13 @@ char *phpdbg_decode_opline(zend_op_array *ops, zend_op *op) /*{{{ */
        case ZEND_JMPNZ_EX:
        case ZEND_JMP_SET:
        case ZEND_ASSERT_CHECK:
-               asprintf(&decode[2], "J%ld", OP_JMP_ADDR(op, op->op2) - ops->opcodes);
+               spprintf(&decode[2], 0, "J%td", OP_JMP_ADDR(op, op->op2) - ops->opcodes);
                break;
 
        case ZEND_FAST_CALL:
        case ZEND_FAST_RET:
                if (op->extended_value != 0) {
-                       asprintf(&decode[2], "J%" PRIu32, op->op2.opline_num);
+                       spprintf(&decode[2], 0, "J%" PRIu32, op->op2.opline_num);
                }
                break;
 
@@ -118,7 +122,7 @@ char *phpdbg_decode_opline(zend_op_array *ops, zend_op *op) /*{{{ */
        case ZEND_SEND_REF:
        case ZEND_SEND_VAR_EX:
        case ZEND_SEND_USER:
-               asprintf(&decode[2], "%" PRIu32, op->op2.num);
+               spprintf(&decode[2], 0, "%" PRIu32, op->op2.num);
                break;
 
        default:
@@ -129,14 +133,14 @@ char *phpdbg_decode_opline(zend_op_array *ops, zend_op *op) /*{{{ */
        /* RESULT */
        switch (op->opcode) {
        case ZEND_CATCH:
-               asprintf(&decode[2], "%" PRIu32, op->result.num);
+               spprintf(&decode[2], 0, "%" PRIu32, op->result.num);
                break;
        default:
                decode[3] = phpdbg_decode_op(ops, &op->result, op->result_type);
                break;
        }
 
-       asprintf(&result,
+       spprintf(&result, 0,
                "%-23s %-20s %-20s %-20s",
                decode[0] ? decode[0] : opcode_name,
                decode[1] ? decode[1] : "",
@@ -144,13 +148,13 @@ char *phpdbg_decode_opline(zend_op_array *ops, zend_op *op) /*{{{ */
                decode[3] ? decode[3] : "");
 
        if (decode[0])
-               free(decode[0]);
+               efree(decode[0]);
        if (decode[1])
-               free(decode[1]);
+               efree(decode[1]);
        if (decode[2])
-               free(decode[2]);
+               efree(decode[2]);
        if (decode[3])
-               free(decode[3]);
+               efree(decode[3]);
 
        return result;
 } /* }}} */
@@ -183,9 +187,7 @@ void phpdbg_print_opline_ex(zend_execute_data *execute_data, zend_bool ignore_fl
                                execute_data->func->op_array.filename ? ZSTR_VAL(execute_data->func->op_array.filename) : "unknown");
                }
 
-               if (decode) {
-                       free(decode);
-               }
+               efree(decode);
        }
 
        if (PHPDBG_G(oplog_list)) {
index b7bb9e688abd968e94d69da0734bf86e4bda65e3..4a00189b46184f3d9b2afa0f580782395f51ab8b 100644 (file)
@@ -82,15 +82,11 @@ static inline void phpdbg_print_function_helper(zend_function *method) /* {{{ */
 
                                do {
                                        char *decode = phpdbg_decode_opline(op_array, opline);
-                                       if (decode != NULL) {
-                                               phpdbg_writeln("print", "line=\"%u\" opnum=\"%u\" op=\"%s\"", " L%-4u #%-5u %s",
-                                                       opline->lineno,
-                                                       opcode,
-                                                       decode);
-                                               free(decode);
-                                       } else {
-                                               phpdbg_error("print", "type=\"decodefailure\" opline=\"%16p\"", "Failed to decode opline %16p", opline);
-                                       }
+                                       phpdbg_writeln("print", "line=\"%u\" opnum=\"%u\" op=\"%s\"", " L%-4u #%-5u %s",
+                                               opline->lineno,
+                                               opcode,
+                                               decode);
+                                       efree(decode);
                                        opline++;
                                } while (opcode++ < end);
                        }
index 112f340d6b2705d3188b7b11a18ac6afef448ddc..fc34c094b63bd4637adfc490b0761d727393fe85 100644 (file)
@@ -764,22 +764,22 @@ char *phpdbg_short_zval_print(zval *zv, int maxlen) /* {{{ */
 
        switch (Z_TYPE_P(zv)) {
                case IS_UNDEF:
-                       decode = zend_strndup("", 0);
+                       decode = estrdup("");
                        break;
                case IS_NULL:
-                       decode = zend_strndup(ZEND_STRL("null"));
+                       decode = estrdup("null");
                        break;
                case IS_FALSE:
-                       decode = zend_strndup(ZEND_STRL("false"));
+                       decode = estrdup("false");
                        break;
                case IS_TRUE:
-                       decode = zend_strndup(ZEND_STRL("true"));
+                       decode = estrdup("true");
                        break;
                case IS_LONG:
-                       asprintf(&decode, ZEND_ULONG_FMT, Z_LVAL_P(zv));
+                       spprintf(&decode, 0, ZEND_LONG_FMT, Z_LVAL_P(zv));
                        break;
                case IS_DOUBLE:
-                       asprintf(&decode, "%.*G", 14, Z_DVAL_P(zv));
+                       spprintf(&decode, 0, "%.*G", 14, Z_DVAL_P(zv));
                        break;
                case IS_STRING: {
                        int i;
@@ -789,28 +789,32 @@ char *phpdbg_short_zval_print(zval *zv, int maxlen) /* {{{ */
                                        ZSTR_VAL(str)[i] = ' ';
                                }
                        }
-                       asprintf(&decode, "\"%.*s\"%c", ZSTR_LEN(str) <= maxlen - 2 ? (int) ZSTR_LEN(str) : (maxlen - 3), ZSTR_VAL(str), ZSTR_LEN(str) <= maxlen - 2 ? 0 : '+');
+                       spprintf(&decode, 0, "\"%.*s\"%c",
+                               ZSTR_LEN(str) <= maxlen - 2 ? (int) ZSTR_LEN(str) : (maxlen - 3),
+                               ZSTR_VAL(str), ZSTR_LEN(str) <= maxlen - 2 ? 0 : '+');
                        zend_string_release(str);
                        } break;
                case IS_RESOURCE:
-                       asprintf(&decode, "Rsrc #%d", Z_RES_HANDLE_P(zv));
+                       spprintf(&decode, 0, "Rsrc #%d", Z_RES_HANDLE_P(zv));
                        break;
                case IS_ARRAY:
-                       asprintf(&decode, "array(%d)", zend_hash_num_elements(Z_ARR_P(zv)));
+                       spprintf(&decode, 0, "array(%d)", zend_hash_num_elements(Z_ARR_P(zv)));
                        break;
                case IS_OBJECT: {
                        zend_string *str = Z_OBJCE_P(zv)->name;
-                       asprintf(&decode, "%.*s%c", ZSTR_LEN(str) <= maxlen ? (int) ZSTR_LEN(str) : maxlen - 1, ZSTR_VAL(str), ZSTR_LEN(str) <= maxlen ? 0 : '+');
+                       spprintf(&decode, 0, "%.*s%c",
+                               ZSTR_LEN(str) <= maxlen ? (int) ZSTR_LEN(str) : maxlen - 1,
+                               ZSTR_VAL(str), ZSTR_LEN(str) <= maxlen ? 0 : '+');
                        break;
                }
                case IS_CONSTANT:
-                       decode = zend_strndup(ZEND_STRL("<constant>"));
+                       decode = estrdup("<constant>");
                        break;
                case IS_CONSTANT_AST:
-                       decode = zend_strndup(ZEND_STRL("<ast>"));
+                       decode = estrdup("<ast>");
                        break;
                default:
-                       asprintf(&decode, "unknown type: %d", Z_TYPE_P(zv));
+                       spprintf(&decode, 0, "unknown type: %d", Z_TYPE_P(zv));
                        break;
        }