首先,我已經用C修改指針值
unsigned char * text ="Test program";
int _size = strlen(text);
unsigned char * str = malloc(sizeof(text));
memcpy(str, text, _size);
printf("Before(%d): %s\n", _size, str);
for(i=0;i < _size; i++) {
str[i] -= 13; //rot13
}
printf("After: (%d): %s\n", strlen(str), str);
創建一個簡單的程序,它運行正常。然而,當我提出這個代碼的Linux內核,它似乎無法工作
unsigned char * str;
len = min(count, log->size - read->off);
/* Allocate the memory for storing plain text */
str = kmalloc(len, GFP_KERNEL);
if(str == NULL) {
printk(KERN_ERR "logger: failed to allocate buffer\n");
return -ENOMEM;
}
memcpy(str, log->buf + read->off, len);
/* Start: Add a simple rot13 encryption here */
for(i=0;i < strlen(str); i++)
str[i] -= 13; //rot13
/* End: Add a simple rot13 encryption here */
if (copy_to_user(buf, str, len))
return -EFAULT;
if(str != NULL) {
kfree(str);
}
的問題來自於因爲如果它刪除,程序運行的情況下,原來下面的代碼
for(i=0;i < strlen(str); i++)
str[i] -= 13; //rot13
。我在這裏錯過了什麼嗎?
當你說,它不工作 - 你是什麼意思?它是否會崩潰內核?或給一些意想不到的文字或其他東西? – joe