Skip to content

Commit 20e5027

Browse files
committed
Switch asprintf to spprintf in phpdbg opcode dump
Also use %td where appropriate, a lot of the values are ptrdiff based. Fix a leak in phpdbg_frame.c.
1 parent 75dc448 commit 20e5027

File tree

4 files changed

+48
-42
lines changed

4 files changed

+48
-42
lines changed

sapi/phpdbg/phpdbg_frame.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,11 @@ static void phpdbg_dump_prototype(zval *tmp) /* {{{ */
164164
}
165165
++j;
166166

167-
php_printf("%s", phpdbg_short_zval_print(argstmp, 40));
167+
{
168+
char *arg_print = phpdbg_short_zval_print(argstmp, 40);
169+
php_printf("%s", arg_print);
170+
efree(arg_print);
171+
}
168172

169173
phpdbg_xml("</arg>");
170174
} ZEND_HASH_FOREACH_END();

sapi/phpdbg/phpdbg_opcode.c

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ static inline char *phpdbg_decode_op(zend_op_array *ops, znode_op *op, uint32_t
3434
switch (type) {
3535
case IS_CV: {
3636
zend_string *var = ops->vars[EX_VAR_TO_NUM(op->var)];
37-
asprintf(&decode, "$%.*s%c", ZSTR_LEN(var) <= 19 ? (int) ZSTR_LEN(var) : 18, ZSTR_VAL(var), ZSTR_LEN(var) <= 19 ? 0 : '+');
37+
spprintf(&decode, 0, "$%.*s%c",
38+
ZSTR_LEN(var) <= 19 ? (int) ZSTR_LEN(var) : 18,
39+
ZSTR_VAL(var), ZSTR_LEN(var) <= 19 ? 0 : '+');
3840
} break;
3941

4042
case IS_VAR:
4143
case IS_TMP_VAR: {
42-
asprintf(&decode, "@%" PRIu32, EX_VAR_TO_NUM(op->var) - ops->last_var);
44+
spprintf(&decode, 0, "@%td", EX_VAR_TO_NUM(op->var) - ops->last_var);
4345
} break;
4446
case IS_CONST: {
4547
zval *literal = RT_CONSTANT(ops, *op);
@@ -58,13 +60,13 @@ char *phpdbg_decode_opline(zend_op_array *ops, zend_op *op) /*{{{ */
5860
switch (op->opcode) {
5961
case ZEND_FAST_CALL:
6062
if (op->extended_value != 0) {
61-
asprintf(&decode[0], "FAST_CALL<%s>",
63+
spprintf(&decode[0], 0, "FAST_CALL<%s>",
6264
op->extended_value == ZEND_FAST_CALL_FROM_CATCH ? "FROM_CATCH" : "FROM_FINALLY");
6365
}
6466
break;
6567
case ZEND_FAST_RET:
6668
if (op->extended_value != 0) {
67-
asprintf(&decode[0], "FAST_RET<%s>",
69+
spprintf(&decode[0], 0, "FAST_RET<%s>",
6870
op->extended_value == ZEND_FAST_RET_TO_CATCH ? "TO_CATCH" : "TO_FINALLY");
6971
}
7072
break;
@@ -74,14 +76,14 @@ char *phpdbg_decode_opline(zend_op_array *ops, zend_op *op) /*{{{ */
7476
switch (op->opcode) {
7577
case ZEND_JMP:
7678
case ZEND_FAST_CALL:
77-
asprintf(&decode[1], "J%ld", OP_JMP_ADDR(op, op->op1) - ops->opcodes);
79+
spprintf(&decode[1], 0, "J%td", OP_JMP_ADDR(op, op->op1) - ops->opcodes);
7880
break;
7981

8082
case ZEND_INIT_FCALL:
8183
case ZEND_RECV:
8284
case ZEND_RECV_INIT:
8385
case ZEND_RECV_VARIADIC:
84-
asprintf(&decode[1], "%" PRIu32, op->op1.num);
86+
spprintf(&decode[1], 0, "%" PRIu32, op->op1.num);
8587
break;
8688

8789
default:
@@ -92,7 +94,9 @@ char *phpdbg_decode_opline(zend_op_array *ops, zend_op *op) /*{{{ */
9294
/* OP2 */
9395
switch (op->opcode) {
9496
case ZEND_JMPZNZ:
95-
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);
97+
spprintf(&decode[2], 0, "J%td or J%td",
98+
OP_JMP_ADDR(op, op->op2) - ops->opcodes,
99+
ZEND_OFFSET_TO_OPLINE(op, op->extended_value) - ops->opcodes);
96100
break;
97101

98102
case ZEND_JMPZ:
@@ -101,13 +105,13 @@ char *phpdbg_decode_opline(zend_op_array *ops, zend_op *op) /*{{{ */
101105
case ZEND_JMPNZ_EX:
102106
case ZEND_JMP_SET:
103107
case ZEND_ASSERT_CHECK:
104-
asprintf(&decode[2], "J%ld", OP_JMP_ADDR(op, op->op2) - ops->opcodes);
108+
spprintf(&decode[2], 0, "J%td", OP_JMP_ADDR(op, op->op2) - ops->opcodes);
105109
break;
106110

107111
case ZEND_FAST_CALL:
108112
case ZEND_FAST_RET:
109113
if (op->extended_value != 0) {
110-
asprintf(&decode[2], "J%" PRIu32, op->op2.opline_num);
114+
spprintf(&decode[2], 0, "J%" PRIu32, op->op2.opline_num);
111115
}
112116
break;
113117

@@ -118,7 +122,7 @@ char *phpdbg_decode_opline(zend_op_array *ops, zend_op *op) /*{{{ */
118122
case ZEND_SEND_REF:
119123
case ZEND_SEND_VAR_EX:
120124
case ZEND_SEND_USER:
121-
asprintf(&decode[2], "%" PRIu32, op->op2.num);
125+
spprintf(&decode[2], 0, "%" PRIu32, op->op2.num);
122126
break;
123127

124128
default:
@@ -129,28 +133,28 @@ char *phpdbg_decode_opline(zend_op_array *ops, zend_op *op) /*{{{ */
129133
/* RESULT */
130134
switch (op->opcode) {
131135
case ZEND_CATCH:
132-
asprintf(&decode[2], "%" PRIu32, op->result.num);
136+
spprintf(&decode[2], 0, "%" PRIu32, op->result.num);
133137
break;
134138
default:
135139
decode[3] = phpdbg_decode_op(ops, &op->result, op->result_type);
136140
break;
137141
}
138142

139-
asprintf(&result,
143+
spprintf(&result, 0,
140144
"%-23s %-20s %-20s %-20s",
141145
decode[0] ? decode[0] : opcode_name,
142146
decode[1] ? decode[1] : "",
143147
decode[2] ? decode[2] : "",
144148
decode[3] ? decode[3] : "");
145149

146150
if (decode[0])
147-
free(decode[0]);
151+
efree(decode[0]);
148152
if (decode[1])
149-
free(decode[1]);
153+
efree(decode[1]);
150154
if (decode[2])
151-
free(decode[2]);
155+
efree(decode[2]);
152156
if (decode[3])
153-
free(decode[3]);
157+
efree(decode[3]);
154158

155159
return result;
156160
} /* }}} */
@@ -183,9 +187,7 @@ void phpdbg_print_opline_ex(zend_execute_data *execute_data, zend_bool ignore_fl
183187
execute_data->func->op_array.filename ? ZSTR_VAL(execute_data->func->op_array.filename) : "unknown");
184188
}
185189

186-
if (decode) {
187-
free(decode);
188-
}
190+
efree(decode);
189191
}
190192

191193
if (PHPDBG_G(oplog_list)) {

sapi/phpdbg/phpdbg_print.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,11 @@ static inline void phpdbg_print_function_helper(zend_function *method) /* {{{ */
8282

8383
do {
8484
char *decode = phpdbg_decode_opline(op_array, opline);
85-
if (decode != NULL) {
86-
phpdbg_writeln("print", "line=\"%u\" opnum=\"%u\" op=\"%s\"", " L%-4u #%-5u %s",
87-
opline->lineno,
88-
opcode,
89-
decode);
90-
free(decode);
91-
} else {
92-
phpdbg_error("print", "type=\"decodefailure\" opline=\"%16p\"", "Failed to decode opline %16p", opline);
93-
}
85+
phpdbg_writeln("print", "line=\"%u\" opnum=\"%u\" op=\"%s\"", " L%-4u #%-5u %s",
86+
opline->lineno,
87+
opcode,
88+
decode);
89+
efree(decode);
9490
opline++;
9591
} while (opcode++ < end);
9692
}

sapi/phpdbg/phpdbg_utils.c

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -764,22 +764,22 @@ char *phpdbg_short_zval_print(zval *zv, int maxlen) /* {{{ */
764764

765765
switch (Z_TYPE_P(zv)) {
766766
case IS_UNDEF:
767-
decode = zend_strndup("", 0);
767+
decode = estrdup("");
768768
break;
769769
case IS_NULL:
770-
decode = zend_strndup(ZEND_STRL("null"));
770+
decode = estrdup("null");
771771
break;
772772
case IS_FALSE:
773-
decode = zend_strndup(ZEND_STRL("false"));
773+
decode = estrdup("false");
774774
break;
775775
case IS_TRUE:
776-
decode = zend_strndup(ZEND_STRL("true"));
776+
decode = estrdup("true");
777777
break;
778778
case IS_LONG:
779-
asprintf(&decode, ZEND_ULONG_FMT, Z_LVAL_P(zv));
779+
spprintf(&decode, 0, ZEND_LONG_FMT, Z_LVAL_P(zv));
780780
break;
781781
case IS_DOUBLE:
782-
asprintf(&decode, "%.*G", 14, Z_DVAL_P(zv));
782+
spprintf(&decode, 0, "%.*G", 14, Z_DVAL_P(zv));
783783
break;
784784
case IS_STRING: {
785785
int i;
@@ -789,28 +789,32 @@ char *phpdbg_short_zval_print(zval *zv, int maxlen) /* {{{ */
789789
ZSTR_VAL(str)[i] = ' ';
790790
}
791791
}
792-
asprintf(&decode, "\"%.*s\"%c", ZSTR_LEN(str) <= maxlen - 2 ? (int) ZSTR_LEN(str) : (maxlen - 3), ZSTR_VAL(str), ZSTR_LEN(str) <= maxlen - 2 ? 0 : '+');
792+
spprintf(&decode, 0, "\"%.*s\"%c",
793+
ZSTR_LEN(str) <= maxlen - 2 ? (int) ZSTR_LEN(str) : (maxlen - 3),
794+
ZSTR_VAL(str), ZSTR_LEN(str) <= maxlen - 2 ? 0 : '+');
793795
zend_string_release(str);
794796
} break;
795797
case IS_RESOURCE:
796-
asprintf(&decode, "Rsrc #%d", Z_RES_HANDLE_P(zv));
798+
spprintf(&decode, 0, "Rsrc #%d", Z_RES_HANDLE_P(zv));
797799
break;
798800
case IS_ARRAY:
799-
asprintf(&decode, "array(%d)", zend_hash_num_elements(Z_ARR_P(zv)));
801+
spprintf(&decode, 0, "array(%d)", zend_hash_num_elements(Z_ARR_P(zv)));
800802
break;
801803
case IS_OBJECT: {
802804
zend_string *str = Z_OBJCE_P(zv)->name;
803-
asprintf(&decode, "%.*s%c", ZSTR_LEN(str) <= maxlen ? (int) ZSTR_LEN(str) : maxlen - 1, ZSTR_VAL(str), ZSTR_LEN(str) <= maxlen ? 0 : '+');
805+
spprintf(&decode, 0, "%.*s%c",
806+
ZSTR_LEN(str) <= maxlen ? (int) ZSTR_LEN(str) : maxlen - 1,
807+
ZSTR_VAL(str), ZSTR_LEN(str) <= maxlen ? 0 : '+');
804808
break;
805809
}
806810
case IS_CONSTANT:
807-
decode = zend_strndup(ZEND_STRL("<constant>"));
811+
decode = estrdup("<constant>");
808812
break;
809813
case IS_CONSTANT_AST:
810-
decode = zend_strndup(ZEND_STRL("<ast>"));
814+
decode = estrdup("<ast>");
811815
break;
812816
default:
813-
asprintf(&decode, "unknown type: %d", Z_TYPE_P(zv));
817+
spprintf(&decode, 0, "unknown type: %d", Z_TYPE_P(zv));
814818
break;
815819
}
816820

0 commit comments

Comments
 (0)