Skip to content

Commit 3b2b080

Browse files
committed
Fixed bug #73704 (phpdbg shows the wrong line in files with shebang)
1 parent c41826d commit 3b2b080

File tree

5 files changed

+62
-14
lines changed

5 files changed

+62
-14
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ PHP NEWS
3333
. Fixed bug #73615 (phpdbg without option never load .phpdbginit at startup).
3434
(Bob)
3535
. Fixed issue getting executable lines from custom wrappers. (Bob)
36+
. Fixed bug #73704 (phpdbg shows the wrong line in files with shebang). (Bob)
3637

3738
- Reflection:
3839
. Fixed bug #46103 (ReflectionObject memory leak). (Nikita)

sapi/phpdbg/phpdbg_list.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,6 @@ zend_op_array *phpdbg_compile_file(zend_file_handle *file, int type) {
250250
memcpy(data.buf, bufptr, data.len);
251251
}
252252
memset(data.buf + data.len, 0, ZEND_MMAP_AHEAD + 1);
253-
data.filename = filename;
254253
data.line[0] = 0;
255254

256255
memset(&fake, 0, sizeof(fake));
@@ -283,10 +282,9 @@ zend_op_array *phpdbg_compile_file(zend_file_handle *file, int type) {
283282
return NULL;
284283
}
285284

286-
dataptr->filename = estrdup(dataptr->filename);
287285
dataptr = erealloc(dataptr, sizeof(phpdbg_file_source) + sizeof(uint) * line);
288-
zend_hash_str_add_ptr(&PHPDBG_G(file_sources), filename, strlen(filename), dataptr);
289-
phpdbg_resolve_pending_file_break(filename);
286+
zend_hash_add_ptr(&PHPDBG_G(file_sources), ret->filename, dataptr);
287+
phpdbg_resolve_pending_file_break(ZSTR_VAL(ret->filename));
290288

291289
fake.opened_path = NULL;
292290
zend_file_handle_dtor(&fake);
@@ -321,9 +319,7 @@ zend_op_array *phpdbg_init_compile_file(zend_file_handle *file, int type) {
321319
return NULL;
322320
}
323321

324-
filename = (char *)(file->opened_path ? ZSTR_VAL(file->opened_path) : file->filename);
325-
326-
dataptr = zend_hash_str_find_ptr(&PHPDBG_G(file_sources), filename, strlen(filename));
322+
dataptr = zend_hash_find_ptr(&PHPDBG_G(file_sources), op_array->filename);
327323
ZEND_ASSERT(dataptr != NULL);
328324

329325
dataptr->op_array = *op_array;
@@ -370,7 +366,6 @@ zend_op_array *phpdbg_compile_string(zval *source_string, char *filename) {
370366
dataptr = erealloc(dataptr, sizeof(phpdbg_file_source) + sizeof(uint) * line);
371367
zend_hash_add_ptr(&PHPDBG_G(file_sources), fake_name, dataptr);
372368

373-
dataptr->filename = estrndup(ZSTR_VAL(fake_name), ZSTR_LEN(fake_name));
374369
zend_string_release(fake_name);
375370

376371
dataptr->op_array = *op_array;
@@ -387,7 +382,6 @@ void phpdbg_free_file_source(zval *zv) {
387382
if (data->buf) {
388383
efree(data->buf);
389384
}
390-
efree(data->filename);
391385

392386
destroy_op_array(&data->op_array);
393387

sapi/phpdbg/phpdbg_list.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ void phpdbg_init_list(void);
4242
void phpdbg_list_update(void);
4343

4444
typedef struct {
45-
char *filename;
4645
char *buf;
4746
size_t len;
4847
#if HAVE_MMAP

sapi/phpdbg/phpdbg_prompt.c

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ int phpdbg_compile_stdin(zend_string *code) {
544544
PHPDBG_G(exec_len) = 1;
545545
{ /* remove leading ?> from source */
546546
int i;
547+
/* remove trailing data after zero byte, used for avoiding conflicts in eval()'ed code snippets */
547548
zend_string *source_path = strpprintf(0, "-%c%p", 0, PHPDBG_G(ops)->opcodes);
548549
phpdbg_file_source *data = zend_hash_find_ptr(&PHPDBG_G(file_sources), source_path);
549550
dtor_func_t dtor = PHPDBG_G(file_sources).pDestructor;
@@ -553,9 +554,6 @@ int phpdbg_compile_stdin(zend_string *code) {
553554
zend_hash_str_update_ptr(&PHPDBG_G(file_sources), "-", 1, data);
554555
zend_string_release(source_path);
555556

556-
efree(data->filename);
557-
data->filename = estrdup("-");
558-
559557
for (i = 1; i <= data->lines; i++) {
560558
data->line[i] -= 2;
561559
}
@@ -572,7 +570,10 @@ int phpdbg_compile(void) /* {{{ */
572570
{
573571
zend_file_handle fh;
574572
char *buf;
573+
char *start_line = NULL;
575574
size_t len;
575+
size_t start_line_len;
576+
int i;
576577

577578
if (!PHPDBG_G(exec)) {
578579
phpdbg_error("inactive", "type=\"nocontext\"", "No execution context");
@@ -591,14 +592,40 @@ int phpdbg_compile(void) /* {{{ */
591592
}
592593
case '\n':
593594
CG(start_lineno) = 2;
594-
fh.handle.stream.mmap.len -= fh.handle.stream.mmap.buf - buf;
595+
start_line_len = fh.handle.stream.mmap.buf - buf;
596+
start_line = emalloc(start_line_len);
597+
memcpy(start_line, buf, start_line_len);
598+
fh.handle.stream.mmap.len -= start_line_len;
595599
end = fh.handle.stream.mmap.buf;
596600
}
597601
} while (fh.handle.stream.mmap.buf + 1 < end);
598602
}
599603

600604
PHPDBG_G(ops) = zend_compile_file(&fh, ZEND_INCLUDE);
601605

606+
/* prepend shebang line to file_source */
607+
if (start_line) {
608+
phpdbg_file_source *data = zend_hash_find_ptr(&PHPDBG_G(file_sources), PHPDBG_G(ops)->filename);
609+
610+
dtor_func_t dtor = PHPDBG_G(file_sources).pDestructor;
611+
PHPDBG_G(file_sources).pDestructor = NULL;
612+
zend_hash_del(&PHPDBG_G(file_sources), PHPDBG_G(ops)->filename);
613+
PHPDBG_G(file_sources).pDestructor = dtor;
614+
615+
data = erealloc(data, sizeof(phpdbg_file_source) + sizeof(uint) * ++data->lines);
616+
memmove(data->line + 1, data->line, sizeof(uint) * data->lines);
617+
data->line[0] = 0;
618+
data->buf = erealloc(data->buf, data->len + start_line_len);
619+
memmove(data->buf + start_line_len, data->buf, data->len * sizeof(uint));
620+
memcpy(data->buf, start_line, start_line_len);
621+
efree(start_line);
622+
data->len += start_line_len;
623+
for (i = 1; i <= data->lines; i++) {
624+
data->line[i] += start_line_len;
625+
}
626+
zend_hash_update_ptr(&PHPDBG_G(file_sources), PHPDBG_G(ops)->filename, data);
627+
}
628+
602629
fh.handle.stream.mmap.buf = buf;
603630
fh.handle.stream.mmap.len = len;
604631
zend_destroy_file_handle(&fh);

sapi/phpdbg/tests/bug73704.phpt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
Bug #73704 (phpdbg shows the wrong line in files with shebang)
3+
--PHPDBG--
4+
list 6
5+
b 4
6+
r
7+
c
8+
q
9+
--EXPECTF--
10+
[Successful compilation of %s]
11+
prompt> 00001: #!/usr/bin/env php
12+
00002: <?php
13+
00003:
14+
00004: echo 1;
15+
00005:
16+
prompt> [Breakpoint #0 added at %s:4]
17+
prompt> [Breakpoint #0 at %s:4, hits: 1]
18+
>00004: echo 1;
19+
00005:
20+
prompt> 1
21+
[Script ended normally]
22+
prompt>
23+
--FILE--
24+
#!/usr/bin/env php
25+
<?php
26+
27+
echo 1;

0 commit comments

Comments
 (0)