Skip to content

Commit 49e9758

Browse files
committed
Corrected behavior on parse an empty file
1 parent 031ba65 commit 49e9758

File tree

6 files changed

+55
-10
lines changed

6 files changed

+55
-10
lines changed

CHANGELOG.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
66

77
## [Unreleased]
88
### Fixed
9-
- Fixed syntax error with final class and use of extends and implements [#48](https://github.com/phalcon/php-zephir-parser/issues/48)
9+
- Fixed syntax error with final class and use of extends and implements
10+
[#48](https://github.com/phalcon/php-zephir-parser/issues/48)
11+
- Corrected behavior on parse an empty file. Now an empty
12+
[IR](https://en.wikipedia.org/wiki/Intermediate_representation)
13+
(as an array) will be returned
1014

1115
### Removed
1216
- PHP 5.x no longer supported. PHP 5.x users should use previous releases
@@ -17,10 +21,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1721

1822
## [1.1.3] - 2018-11-06
1923
### Changed
20-
- Extremely simplified installation of the extension using standard PHP workflow [#38](https://github.com/phalcon/php-zephir-parser/issues/38)
24+
- Extremely simplified installation of the extension using standard PHP workflow
25+
[#38](https://github.com/phalcon/php-zephir-parser/issues/38)
2126

2227
### Fixed
23-
- Improved error handling and prevent segfault on invalid syntax [#30](https://github.com/phalcon/php-zephir-parser/issues/30)
28+
- Improved error handling and prevent segfault on invalid syntax
29+
[#30](https://github.com/phalcon/php-zephir-parser/issues/30)
2430

2531
### Removed
2632
- Removed no longer needed BASH scripts to build and install extension
@@ -30,7 +36,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
3036
- Added ability to build Windows DLLs for PHP 7.2
3137

3238
### Changed
33-
- Removed ability to build Windows DLLs for PHP 5.x. Windows users with PHP 5.x should use Zephir Parser <= 1.1.1 (see latest releases).
39+
- Removed ability to build Windows DLLs for PHP 5.x.
40+
Windows users with PHP 5.x should use Zephir Parser <= 1.1.1 (see latest releases).
3441

3542
## [1.1.1] - 2017-11-09
3643
### Changed
@@ -40,14 +47,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
4047

4148
### Fixed
4249
- Fixed `mod-assign` operator recognition
43-
- Fixed issue with incorrectly used `YYMARKER` and `YYCURSOR` [#31](https://github.com/phalcon/php-zephir-parser/issues/31),
44-
[phalcon/zephir#1591](https://github.com/phalcon/zephir/issues/1591), [phalcon/cphalcon#13140](https://github.com/phalcon/cphalcon/issues/13140)
50+
- Fixed issue with incorrectly used `YYMARKER` and `YYCURSOR`
51+
[#31](https://github.com/phalcon/php-zephir-parser/issues/31),
52+
[phalcon/zephir#1591](https://github.com/phalcon/zephir/issues/1591),
53+
[phalcon/cphalcon#13140](https://github.com/phalcon/cphalcon/issues/13140)
4554
- Improved scanner by removing redundant rules
4655

4756
## [1.1.0] - 2017-10-12
4857
### Added
49-
- Added support syntax assign-bitwise operators [#14](https://github.com/phalcon/php-zephir-parser/issues/14),
50-
[phalcon/zephir#1056](https://github.com/phalcon/zephir/issues/1056)
58+
- Added support syntax assign-bitwise operators
59+
[#14](https://github.com/phalcon/php-zephir-parser/issues/14),
60+
[phalcon/zephir#1056](https://github.com/phalcon/zephir/issues/1056)
5161

5262
### Changed
5363
- Refactor tests to use more standard approach usually used for PHP extensions

parser/base.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ void xx_parse_program(zval *return_value, char *program, size_t program_length,
6868
/**
6969
* Check if the program has any length
7070
*/
71-
if (program_length < 2) {
71+
if (program_length < 2 || is_empty(program)) {
72+
array_init(return_value);
7273
return;
7374
}
7475

parser/parser.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8611,7 +8611,8 @@ void xx_parse_program(zval *return_value, char *program, size_t program_length,
86118611
/**
86128612
* Check if the program has any length
86138613
*/
8614-
if (program_length < 2) {
8614+
if (program_length < 2 || is_empty(program)) {
8615+
array_init(return_value);
86158616
return;
86168617
}
86178618

parser/parser.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
#include <php.h>
22
#include <string.h>
3+
#include <ctype.h>
34

45
#include "zephir.h"
56
#include "xx.h"
67
#include "scanner.h"
78

9+
/**
10+
* Check if all characters are whitespace.
11+
*/
12+
static int is_empty(const char *program)
13+
{
14+
while (*program != '\0') {
15+
if (!isspace((unsigned char)*program))
16+
return 0;
17+
program++;
18+
}
19+
20+
return 1;
21+
}
22+
823
static void parser_add_str(zval *arr, const char *key, const char *val) {
924
zval tmp;
1025
zend_string *tmp_str = zend_string_init(val, strlen(val), 0);

tests/base/empty01.phpt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--TEST--
2+
test me
3+
--SKIPIF--
4+
<?php include(__DIR__ . '/../skipif.inc'); ?>
5+
--FILE--
6+
<?php var_dump(zephir_parse_file(' ', '(eval code)')); ?>
7+
--EXPECT--
8+
array(0) {
9+
}

tests/base/empty02.phpt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--TEST--
2+
test me
3+
--SKIPIF--
4+
<?php include(__DIR__ . '/../skipif.inc'); ?>
5+
--FILE--
6+
<?php var_dump(zephir_parse_file("\n\n\t \n\n\t \r\n\t", '(eval code)')); ?>
7+
--EXPECT--
8+
array(0) {
9+
}

0 commit comments

Comments
 (0)