@@ -684,21 +684,35 @@ PHP_JSON_API void php_json_decode_ex(zval *return_value, char *str, int str_len,
684
684
double d ;
685
685
int type , overflow_info ;
686
686
long p ;
687
+ char * trim = str ;
688
+ int trim_len = str_len ;
689
+
690
+ /* Increment trimmed string pointer to strip leading whitespace */
691
+ /* JSON RFC says to consider as whitespace: space, tab, LF or CR */
692
+ while (trim_len && (* trim == ' ' || * trim == '\t' || * trim == '\n' || * trim == '\r' )) {
693
+ trim ++ ;
694
+ trim_len -- ;
695
+ }
696
+
697
+ /* Decrement trimmed string length to strip trailing whitespace */
698
+ while (trim_len && (trim [trim_len - 1 ] == ' ' || trim [trim_len - 1 ] == '\t' || trim [trim_len - 1 ] == '\n' || trim [trim_len - 1 ] == '\r' )) {
699
+ trim_len -- ;
700
+ }
687
701
688
702
RETVAL_NULL ();
689
- if (str_len == 4 ) {
690
- if (!strcasecmp ( str , "null" )) {
703
+ if (trim_len == 4 ) {
704
+ if (!strncasecmp ( trim , "null" , trim_len )) {
691
705
/* We need to explicitly clear the error because its an actual NULL and not an error */
692
706
jp -> error_code = PHP_JSON_ERROR_NONE ;
693
707
RETVAL_NULL ();
694
- } else if (!strcasecmp ( str , "true" )) {
708
+ } else if (!strncasecmp ( trim , "true" , trim_len )) {
695
709
RETVAL_BOOL (1 );
696
710
}
697
- } else if (str_len == 5 && !strcasecmp ( str , "false" )) {
711
+ } else if (trim_len == 5 && !strncasecmp ( trim , "false" , trim_len )) {
698
712
RETVAL_BOOL (0 );
699
713
}
700
714
701
- if ((type = is_numeric_string_ex (str , str_len , & p , & d , 0 , & overflow_info )) != 0 ) {
715
+ if ((type = is_numeric_string_ex (trim , trim_len , & p , & d , 0 , & overflow_info )) != 0 ) {
702
716
if (type == IS_LONG ) {
703
717
RETVAL_LONG (p );
704
718
} else if (type == IS_DOUBLE ) {
@@ -711,10 +725,10 @@ PHP_JSON_API void php_json_decode_ex(zval *return_value, char *str, int str_len,
711
725
int i ;
712
726
zend_bool is_float = 0 ;
713
727
714
- for (i = (str [0 ] == '-' ? 1 : 0 ); i < str_len ; i ++ ) {
728
+ for (i = (trim [0 ] == '-' ? 1 : 0 ); i < trim_len ; i ++ ) {
715
729
/* Not using isdigit() because it's locale specific,
716
730
* but we expect JSON input to always be UTF-8. */
717
- if (str [i ] < '0' || str [i ] > '9' ) {
731
+ if (trim [i ] < '0' || trim [i ] > '9' ) {
718
732
is_float = 1 ;
719
733
break ;
720
734
}
@@ -723,7 +737,7 @@ PHP_JSON_API void php_json_decode_ex(zval *return_value, char *str, int str_len,
723
737
if (is_float ) {
724
738
RETVAL_DOUBLE (d );
725
739
} else {
726
- RETVAL_STRINGL (str , str_len , 1 );
740
+ RETVAL_STRINGL (trim , trim_len , 1 );
727
741
}
728
742
} else {
729
743
RETVAL_DOUBLE (d );
0 commit comments