Skip to content

Commit 2a65e4f

Browse files
authored
Merge pull request #32 from dreamsxin/patch-1
Update jwt.c
2 parents 9800dfe + d19cf43 commit 2a65e4f

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

jwt.c

+8-7
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,8 @@ static void php_jwt_encode(INTERNAL_FUNCTION_PARAMETERS) {
487487
smart_str_free(&json_header);
488488
smart_str_free(&json_payload);
489489

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);
491492
strcpy(buf, header_b64);
492493
strcat(buf, ".");
493494
strcat(buf, payload_b64);
@@ -497,9 +498,11 @@ static void php_jwt_encode(INTERNAL_FUNCTION_PARAMETERS) {
497498

498499
/* sign */
499500
if (jwt->alg == JWT_ALG_NONE) {
501+
buflen += 1;
500502
/* alg none */
501-
buf = (char *)erealloc(buf, strlen(buf) + 1);
503+
buf = (char *)erealloc(buf, buflen);
502504
strcat(buf, ".");
505+
buf[buflen] = '\0';
503506
} else {
504507
/* set jwt struct */
505508
jwt->key = key;
@@ -516,7 +519,8 @@ static void php_jwt_encode(INTERNAL_FUNCTION_PARAMETERS) {
516519
zend_string *sig_str = zend_string_init(sig, sig_len, 0);
517520
char *sig_b64 = jwt_b64_url_encode(sig_str);
518521

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);
520524
sprintf(tmp, "%s.%s", buf, sig_b64);
521525

522526
efree(buf);
@@ -534,11 +538,8 @@ static void php_jwt_encode(INTERNAL_FUNCTION_PARAMETERS) {
534538

535539
jwt_free(jwt);
536540

537-
char *ret = alloca(strlen(buf));
538-
strcpy(ret, buf);
541+
RETVAL_STRINGL(buf, strlen(buf));
539542
efree(buf);
540-
541-
RETURN_STRING(ret);
542543
}
543544

544545
/* Jwt decode */

0 commit comments

Comments
 (0)