-1

I try to get strings from userspace within the kernel module. Till I set my char size manually it seems working properly. However, I need to make it dynamic so if I use len parameter it shows weird symbols on the end of char.

static ssize_t msecurity_write(struct file *filep, const char *buffer, size_t len, loff_t *offset){
  
    char chars[12];
    if(copy_from_user(chars,buffer,len)){
        return -EFAULT;
    }

    printk(KERN_ALERT "Output> %s", chars);
    printk(KERN_ALERT "lengh> %i", len);

    return len;
 
}

First output is for char[len] secound has been set manualy char[12]. Even if you print len it shows value of 12.

console output for debug

1

1 Answer 1

0

C strings are terminated by a null character. This character is not included in any string length calculation but must be included.

Thus a string of length 12 will need 13 bytes with the last byte equal to 0.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.