Is there a C function like sprintf in the Linux kernel?

后端 未结 5 1849
甜味超标
甜味超标 2021-01-17 08:24

Is there function like sprintf() in Linux Kernel (like printf()->printk())?

相关标签:
5条回答
  • 2021-01-17 08:39

    yes there is check out here for example

    you can use grep to see if it is in the kernel's source code

    0 讨论(0)
  • 2021-01-17 08:43

    yes. https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/lib/vsprintf.c#n1828

    int snprintf(char *buf, size_t size, const char *fmt, ...)
    {
        va_list args;
        int i;
    
        va_start(args, fmt);
        i = vsnprintf(buf, size, fmt, args);
        va_end(args);
    
        return i;
    }
    EXPORT_SYMBOL(snprintf);
    

    sprintf() by itself is prone to buffer overflows. CERT buffer overflows, Apple, etc

    0 讨论(0)
  • 2021-01-17 08:45

    Running crash on a live 2.6 kernel confirms sprintf() is defined, and where it is defined.

    crash> sym sprintf

    ffffffff81267ba0 (T) sprintf ../debug/kernel-2.6.39/linux-2.6.39-400.210.2.el6uek/lib/vsprintf.c: 1442

    0 讨论(0)
  • 2021-01-17 08:50

    Yes, just include linux/kernel.h

    0 讨论(0)
  • 2021-01-17 08:52

    sprintf() is unsafe because of buffer overflow. If you need to pass data from user space to kernel space, use instead copy_from_user(); it acts like copy_to_user() but in reverse direction.

    0 讨论(0)
提交回复
热议问题