@@ -487,7 +487,8 @@ static void php_jwt_encode(INTERNAL_FUNCTION_PARAMETERS) {
487
487
smart_str_free (& json_header );
488
488
smart_str_free (& json_payload );
489
489
490
- buf = (char * )emalloc (strlen (header_b64 ) + strlen (payload_b64 ) + 1 );
490
+ int buflen = strlen (header_b64 ) + strlen (payload_b64 ) + 2 ;
491
+ buf = (char * )ecalloc (buflen , 1 );
491
492
strcpy (buf , header_b64 );
492
493
strcat (buf , "." );
493
494
strcat (buf , payload_b64 );
@@ -497,9 +498,11 @@ static void php_jwt_encode(INTERNAL_FUNCTION_PARAMETERS) {
497
498
498
499
/* sign */
499
500
if (jwt -> alg == JWT_ALG_NONE ) {
501
+ buflen += 1 ;
500
502
/* alg none */
501
- buf = (char * )erealloc (buf , strlen ( buf ) + 1 );
503
+ buf = (char * )erealloc (buf , buflen );
502
504
strcat (buf , "." );
505
+ buf [buflen ] = '\0' ;
503
506
} else {
504
507
/* set jwt struct */
505
508
jwt -> key = key ;
@@ -516,7 +519,8 @@ static void php_jwt_encode(INTERNAL_FUNCTION_PARAMETERS) {
516
519
zend_string * sig_str = zend_string_init (sig , sig_len , 0 );
517
520
char * sig_b64 = jwt_b64_url_encode (sig_str );
518
521
519
- char * tmp = (char * )emalloc (strlen (sig_b64 ) + strlen (buf ) + 1 );
522
+ buflen = strlen (sig_b64 ) + strlen (buf ) + 2 ;
523
+ char * tmp = (char * )ecalloc (buflen , 1 );
520
524
sprintf (tmp , "%s.%s" , buf , sig_b64 );
521
525
522
526
efree (buf );
@@ -534,11 +538,8 @@ static void php_jwt_encode(INTERNAL_FUNCTION_PARAMETERS) {
534
538
535
539
jwt_free (jwt );
536
540
537
- char * ret = alloca (strlen (buf ));
538
- strcpy (ret , buf );
541
+ RETVAL_STRINGL (buf , strlen (buf ));
539
542
efree (buf );
540
-
541
- RETURN_STRING (ret );
542
543
}
543
544
544
545
/* Jwt decode */
0 commit comments