[問題] 請問free分配空間的概念
請教一下兩個問題,如下
為什麼path分配空間後,最後面寫kfree(path)會導致crash,這樣寫法是錯的嗎?
另外code寫free_token = token,然後最後kfree(free_token),為什麼要多寫一個指標去free?
謝謝
int test(struct device *dev, const char *p_i8_buf, size_t count)
{
int i32_ret = 0;
char *temp_buf, *token, *path;
char *free_temp_buf, *free_token;
unsigned long fun = 0;
const char *delim = " ,";
temp_buf = kzalloc(RAYDIUM_FW_BIN_PATH_LENGTH + 1, GFP_KERNEL);
if (temp_buf == NULL)
{
return -ENOMEM;
}
token = kzalloc(RAYDIUM_FW_BIN_PATH_LENGTH + 1, GFP_KERNEL);
if (token == NULL) {
kfree(temp_buf);
return -ENOMEM;
}
path = kzalloc(RAYDIUM_FW_BIN_PATH_LENGTH + 1, GFP_KERNEL);
if (path == NULL) {
kfree(temp_buf);
kfree(token);
return -ENOMEM;
}
free_token = token;
free_temp_buf = temp_buf;
strlcpy(temp_buf, p_i8_buf, count);
token = strsep(&temp_buf, delim);
if(token == NULL)
{
kfree(free_token);
kfree(free_temp_buf);
kfree(path);
return -EINVAL;
}
i32_ret = kstrtoul(token, 16, &fun);
if (i32_ret < 0) {
kfree(free_token);
kfree(free_temp_buf);
kfree(path);
return i32_ret;
}
path = strsep(&temp_buf, delim);//log path
parse(dev, fun, path);
kfree(free_token);
kfree(free_temp_buf);
//kfree(path); //will not crash <-----------------
or kfree(path); //will crash<------------------------
return count;
}
crash log:
[ 1316.983483] Unable to handle kernel paging request at virtual address 00005e00
[ 1316.990498] pgd = db59c000
[ 1316.993168] [00005e00] *pgd=00000000
[ 1316.996642] Internal error: Oops: 5 [#1] PREEMPT SMP ARM
[ 1317.001840] Modules linked in: drmboot(P) mali_kbase
[ 1317.006705] CPU: 3 PID: 1326 Comm: sh Tainted: P 3.10.0 #6612
[ 1317.013355] task: db702a00 ti: db728000 task.ti: db728000
[ 1317.018648] PC is at kmem_cache_alloc_trace+0x7c/0x1c0
[ 1317.023676] LR is at test+0x98/0x2fc
[ 1317.028707] pc : [<c0100620>] r : [<c04b39a8>] sr: 20010013
[ 1317.028707] sp : db729eb0 p : 00000000 p : 00000101
[ 1317.039960] r10: 00000f53 9 : 01328000 8 : c04b39a8
2.
通常看到都是寫法3,
我在看很多別人寫的code的寫法都是1,請問是不是改成2才對?
if(!handle)等價if(handle==NULL)嗎?
謝謝回答
struct input_handle *handle;
(1) handle = kzalloc(sizeof(*handle), GFP_KERNEL);
if (!handle)
return -ENOMEM;
(2)
handle = kzalloc(sizeof(*handle), GFP_KERNEL);
if (!handle)
{
kfree(handle);
return -ENOMEM;
}
(3)
handle = kzalloc(sizeof(*handle), GFP_KERNEL);
if (handle == NULL)
{
return -ENOMEM;
}
kfree(handle);
-----
Sent from JPTT on my Asus ASUS_Z012DA.
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.200.98.254 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1682046179.A.E43.html
推
04/21 13:24,
1年前
, 1F
04/21 13:24, 1F
→
04/21 13:25,
1年前
, 2F
04/21 13:25, 2F
推
04/21 13:25,
1年前
, 3F
04/21 13:25, 3F
→
04/21 13:25,
1年前
, 4F
04/21 13:25, 4F
→
04/21 13:25,
1年前
, 5F
04/21 13:25, 5F
→
04/21 13:25,
1年前
, 6F
04/21 13:25, 6F
→
04/21 13:25,
1年前
, 7F
04/21 13:25, 7F
→
04/21 13:25,
1年前
, 8F
04/21 13:25, 8F
推
04/21 13:28,
1年前
, 9F
04/21 13:28, 9F
→
04/21 17:48,
1年前
, 10F
04/21 17:48, 10F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章