Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5954997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:03:04+00:00 2026-05-22T18:03:04+00:00

ssize_t probchar_write(struct file *filp, const char __user *data, size_t s, loff_t *off) { printk(KERN_DEBUG

  • 0
ssize_t probchar_write(struct file *filp, 
    const char __user *data, size_t s, loff_t *off) {

    printk(KERN_DEBUG "Data> |%s|\n", data); // only for debug
    char chars[MAX_LENGHT];
    if(s > MAX_LENGHT)
        s = MAX_LENGHT;
    if (copy_from_user(chars, data, s)) {
        return -EFAULT;
    }
    printk(KERN_DEBUG "Chars> |%s|\n", chars);
    return s;
}

This is dmesg output

[66777.956582] Data> |45
[66777.956596] |
[66777.956634] Chars> |45
[66777.956636] �    Ҩ�H��   H�� |

Why the copied chain has more characters in the end?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:03:04+00:00Added an answer on May 22, 2026 at 6:03 pm

    This seems to be a write function from a device driver.

    First

    printk(KERN_DEBUG "Data> |%s|\n", data);
    

    Do not do this! Do not ever directly access user data ever!

    Second

    char chars[s];
    

    I doubt this is legal C. You need to either specify a size at compile time, or use kmalloc.

    The copy_from_user usage is good. You should check for error and return -EFAULT. This is ok.

    So just try and allocating the chars and it should work. You might also want to look at the offset, though for academic purposes that can be initially skipped.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to allocate some memory for a char* as follows. static ssize_t memo_write(struct
I have two functions : void foo(const char * p) and template<size_t T_Size> void
template<typename T, size_t n> size_t array_size(const T (&)[n]) { return n; } The part
In the man page for the system call write(2) - ssize_t write(int fd, const
Like this function in C: size_t fwrite ( const void * ptr, size_t size,
how does realloc know the size of original data? void *realloc(void *ptr, size_t size);
I notice that modern C and C++ code seems to use size_t instead of
In some code I've inherited, I see frequent use of size_t with the std
#include <vector> std::vector<long int> as; long int a(size_t n){ if(n==1) return 1; if(n==2) return
I'm using the following code to write data through a named pipe from one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.