当我从常量int或size_t变量中分配长度的字符串时,它会让我感到困惑。它会造成内存泄漏。
另一方面,当我用数字分配时,它运行良好。发生了什么事?
PHP_FUNCTION(aa_encrypt64) {
const char *key = ""; // base64
char *str = NULL;
char *datetime = NULL;
char *private_key = NULL;
const size_t rtn_len = 32;
char rtn[rtn_len]; // char rtn[rtn_len] leaves memory leak
int argc = ZEND_NUM_ARGS();
size_t str_len;
size_t datetime_len;
size_t private_key_len;
if (zend_parse_parameters(argc, "sss", &str, &str_len, &datetime, &datetime_len, &private_key, &private_key_len) == FAILURE)
return;
md5(str, rtn);
RETURN_STRINGL(rtn, rtn_len);
}
PHP_FUNCTION(aa_decrypt64)
{
PHP_FUNCTION(aa_encrypt64) {
const char *key = ""; // base64
char *str = NULL;
char *datetime = NULL;
char *private_key = NULL;
const size_t rtn_len = 32;
char rtn[32]; // char rtn[rtn_len] leaves memory leak
int argc = ZEND_NUM_ARGS();
size_t str_len;
size_t datetime_len;
size_t private_key_len;
if (zend_parse_parameters(argc, "sss", &str, &str_len, &datetime, &datetime_len, &private_key, &private_key_len) == FAILURE)
return;
md5(str, rtn);
RETURN_STRINGL(rtn, rtn_len);
}