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

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

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

Yes, just include linux/kernel.h

yes there is check out here for example
you can use grep to see if it is in the kernel's source code

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

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.

Related

How to make a wrapper for sprintf that returns the string result?

I would like to be able to pass formatted strings to other functions, like my error handler:
error_handler(str_format("error code %d in graphics function: %s", code, error));
My first idea was to use sprintf, but it does not return the result, so you have to add extra lines:
char msg = [100];
sprintf(msg, "error code %d in graphics function: %s", code, error)
error_handler(msg);
And if the error message is bigger than the size of msg then there could be problems.
So I thought I could make a wrapping function like this:
char* str_format (char* format, ...) {
char* output = malloc(100);
va_list args;
va_start(args, format);
vsnprintf(output, 100, format, args);
va_end(args);
return output;
}
But I still have the problem of not knowing the needed size for output. So I thought I could use sizeof to get the size of format and all the args, but there's no way to detect how many args there are unless you manually pass a number of args every time. I could read the format string and look for '%' symbols and detect each placeholder and type, but this seems to be too complicated, just to get the return value of sprintf.
I know there is a function called snprintf that will be safe and limit the number of chars but there is no vsnprintf in c
EDIT: #1 changed output to malloc #2 there is a vsnprintf function after all, so I changed to that. So then the question is down to if there is an easier way than this type of wrapper function. Thank you
Is there an easy way to get sprintf to return the formatted string?
Typically, you use two calls to vsnprintf for this -- the first call with no buffer to get the size needed and a second call to fill the buffer
char* str_format (char* format, ...) {
va_list args;
va_start(args, format);
int len = vsnprintf(0, 0, format, args);
va_end(args);
char *output = malloc(len+1);
va_start(args, format);
vsnprintf(output, len+1, format, args);
va_end(args);
return output;
}
Note that the caller will need to free the allocated pointer (or it will leak)
So I thought I could make a wrapping function like this:
/* !!! DON'T DO THIS !!! */
char* str_format (char* format, ...) {
char output[100];
va_list args;
va_start(args, format);
vsprintf(output, format, args);
va_end(args);
return output;
}
This is bad for multiple reasons: first, as you say you don't know the size of the output. Most importantly however, you cannot possibly return a variable defined locally (like output).
To know the size of the output, you can first make a "dummy" call to vsnprintf with a size of 0, which according to the manual should simply calculate and return the needed size for the output buffer.
As per returning a valid pointer, you could make this work in two different ways:
Allocate a buffer with malloc, then return a pointer to the allocated buffer. This could be annoying since you would need to always free() the allocated buffer, and since you want to use your function like error_handler(str_format(...)) you lose reference to the returned buffer, so you either free() it in error_handler() and always assume that error_handler() will take malloc'd objects, or you need a temporary variable.
Use a static buffer. This however has the problem of needing to be pre-allocated with a constant size at compile time. You could choose this option and limit your error message size. One other limitation of this approach is that only one error at a time can be formatted (i.e. this is not thread safe), as you would be overwriting the same static buffer each time. This is a pretty common among C libraries as it is painless and easy to implement, but only really applicable if you have reasonably-sized error messages.
Option 1 (error checking omitted):
char* str_format(char* format, ...) {
char *buf;
int size;
va_list args;
va_start(args, format);
size = vsnprintf(NULL, 0, format, args) + 1;
va_end(args);
buf = malloc(size);
va_start(args, format);
vsnprintf(NULL, size, format, args);
va_end(args);
return buf;
}
Option 2:
char* str_format(char* format, ...) {
static char buf[1024]; // fixed at compile time
va_list args;
va_start(args, format);
vsnprintf(NULL, 1024, format, args);
va_end(args);
return buf;
}
Note that for vsnprintf you need to define at least one of the following feature test macros, as specified by the manual:
#define _XOPEN_SOURCE 500
// or
#define _ISOC99_SOURCE
// or
#define _BSD_SOURCE // old glibc <= 2.19

vsnwprintf alternative on linux

What is the direct alternative for the vsnwprintf function on linux machines and what headers should be included to use it?
Thanks for any advice
It depends on what vsnwprintf() (sic) does and what is its parameter list.
The C99 Standard describes vswprintf() (in section 7.24.2.7). To use it you need to #include <stdarg.h> and <wchar.h>. The implementation should be in the Standard library.
Linux variant of this function is snprintf and vsnprintf which are kindly provided by stdio.h:
int snprintf(char *str, size_t size, const char *format, ...);
int vsnprintf(char *str, size_t size, const char *format, va_list args);
and Unicode versions provided by wchar.h:
int swprintf(wchar_t *wcs, size_t maxlen, const wchar_t *format, ...);
int vswprintf(wchar_t *wcs, size_t maxlen, const wchar_t *format, va_list args);
http://linux.die.net/man/3/snprintf
http://linux.die.net/man/3/swprintf
There’s no alternative for that.
vswprintf suggested in other answers doesn’t tell required buffer length. When you’ll pass nullptr and 0 to the first two arguments, it will returns -1, not the required buffer length. This is both how it's documented, and how it works in practice in clang 4.
There’s a workaround in that answer
https://stackoverflow.com/a/16352146/126995
Scroll to the end of that for vscwprintf source code. The workaround works for small strings, but relatively slow for larger strings, and can even exhaust stack space for a very large string.
Seems that u_vsnprintf() or u_vsnprintf_u() from libicu is what you need
The truncating version of vswprintf() is unsafe.
You really want the C11 vswprintf_s(), which ensures a null delimited wide string, and errors when the destination buffer is too small.
As you know, the wide variants have no API to calculate the size with a NULL buffer. You need to extend the buffer on errors, if the buffer is too small.
Anyway, safeclib-3.0 will provide all of them.
safe and unsafe with --enable-unsafe, wide or non-wide, truncating ('n') or not-truncating.
https://github.com/rurban/safeclib/blob/wchar/src/wchar/vsnwprintf_s.c
vsnwprintf is in C99, MSVC, and a few others. Not in musl.
-stc=c99 should be enough for glibc.
Here an example of workaround:
int vsnwprintf(wchar_t* buffer, size_t size, const wchar_t* format, va_list& params)
{
if(buffer || size)
return vswprintf(buffer, size, format, params);
int result = -1;
for(size = 100; result < 0; size += 100)
{
unique_ptr<wchar_t[]> guard(buffer = new wchar_t[size]);
va_list copy;
va_copy(copy, params);
result = vswprintf(buffer, size, format, copy);
va_end(copy);
}
return result;
};
Probably, it's needed to limit a number of iterations in some way.
For C++, you can use fmt::sprintf of libfmt from <fmt/printf.h>

Can you write to a byte array using a FILE*

C# has a neat feature of being able to write to a memory stream using the MemoryStream object.
I'm looking for similar functionality from C using a FILE* pointer.
I want to be able to sprintf() but with the added functionality of having C remember "where I was" in the buffer I'm writing to.
If you are using the GNU C Library, you can use fmemopen(). There may be other non-portable extensions for other environments, but there's no portable way using FILE*s.
You could also, however, wrap snprintf, if you don't insist on actually using FILE*s. For example, glib (note: not the same as the GNU C Library, and portable) has a g_string_append_printf that does what you want.
There's also an ugly hack which works with plain ISO-C: You can use fopen() to open a null file (/dev/null on *nix, NUL on Windows) and set the array as the file's buffer via
setvbuf(file, buffer, _IOFBF, buffer_size)
This should work fine as long as fflush() isn't called anywhere in the code. Also, the programmer has to explicitly take care of the string delimiter.
I don't really see a need to do this, though: As snprintf() returns the number of characters written, it's trivial to keep track of a buffer's position.
One can even write a function to automagically resize the buffer on overflow: bufprintf.c
The function's prototype is
int bufprintf(char **buffer, size_t *size, size_t *offset,
const char *format, ...);
An example program could look like this:
#include <stdio.h>
extern int bufprintf(char **buffer, size_t *size, size_t *offset,
const char *format, ...);
int main(void)
{
size_t size = 0; // must be set!
size_t offset;
char * buffer;
for(int i = 0; i < 100; ++i)
bufprintf(&buffer, &size, &offset, "we rock %i\n", i);
puts(buffer);
printf("size:\t%u\noffset:\t%u\n", (unsigned)size, (unsigned)offset);
}
sprintf returns the number of characters that were printed into the string. You can use that value to increment the pointer of your buffer.
buffer += sprintf(buffer, "%d", i);
Make sure that you keep around a copy of the original pointer, as that is what you will be using when passing the buffer somewhere else.
Why not use mmap? You can map a file to memory and use a FILE* pointer.
Update: It does not work. Sorry.
MemoryStream is not a feature of C#, the language. It's simply a class in the BCL. You could write it yourself in C#.
And so it is in C - you'd write some functions that work similarly in use to fopen, fprintf, fwrite, fclose, etc., but give them names like mopen, mwrite, etc. (presumably) and write them so they operate on a memory buffer.
How about just manually managing your pointers. I don't have a compiler in front me but hopefully the code below gets the idea across:
char huge_buffer[REALLY_BIG_SIZE];
char *write_pos = huge_buffer;
//...
void fprintf_to_mem(char **mem_ptr, const char *fmt, ...)
{
va_list args;
int num_written;
va_start(args, mem_ptr);
num_written = vsprintf(*write_pos, fmt, args);
*write_pos += num_written;
va_end(args);
}
//...
fprintf_to_mem(&write_pos, "Testing %d %d %d", 1, 2, 3);
fprintf_to_mem(&write_pos, "Hello world!\r\n");
I would expect that to output "Testing 1 2 3Hello world!\r\n" to huge_buffer.

Creating C formatted strings (not printing them)

I have a function that accepts a string, that is:
void log_out(char *);
In calling it, I need to create a formatted string on the fly like:
int i = 1;
log_out("some text %d", i);
How do I do this in ANSI C?
Only, since sprintf() returns a int, this means that I have to write at least 3 commands, like:
char *s;
sprintf(s, "%d\t%d", ix, iy);
log_out(s);
Any way to shorten this?
Use sprintf. (This is NOT safe, but OP asked for an ANSI C answer. See the comments for a safe version.)
int sprintf ( char * str, const char * format, ... );
Write formatted data to string Composes a string with the same text
that would be printed if format was used on printf, but instead of
being printed, the content is stored as a C string in the buffer
pointed by str.
The size of the buffer should be large enough to contain the entire
resulting string (see snprintf for a safer version).
A terminating null character is automatically appended after the
content.
After the format parameter, the function expects at least as many
additional arguments as needed for format.
Parameters:
str
Pointer to a buffer where the resulting C-string is stored. The buffer
should be large enough to contain the resulting string.
format
C string that contains a format string that follows the same
specifications as format in printf (see printf for details).
... (additional arguments)
Depending on the format string, the function may expect a sequence of
additional arguments, each containing a value to be used to replace a
format specifier in the format string (or a pointer to a storage
location, for n). There should be at least as many of these arguments
as the number of values specified in the format specifiers. Additional
arguments are ignored by the function.
Example:
// Allocates storage
char *hello_world = (char*)malloc(13 * sizeof(char));
// Prints "Hello world!" on hello_world
sprintf(hello_world, "%s %s!", "Hello", "world");
If you have a POSIX-2008 compliant system (any modern Linux), you can use the safe and convenient asprintf() function: It will malloc() enough memory for you, you don't need to worry about the maximum string size. Use it like this:
char* string;
if(0 > asprintf(&string, "Formatting a number: %d\n", 42)) return error;
log_out(string);
free(string);
This is the minimum effort you can get to construct the string in a secure fashion. The sprintf() code you gave in the question is deeply flawed:
There is no allocated memory behind the pointer. You are writing the string to a random location in memory!
Even if you had written
char s[42];
you would be in deep trouble, because you can't know what number to put into the brackets.
Even if you had used the "safe" variant snprintf(), you would still run the danger that your strings gets truncated. When writing to a log file, that is a relatively minor concern, but it has the potential to cut off precisely the information that would have been useful. Also, it'll cut off the trailing endline character, gluing the next log line to the end of your unsuccessfully written line.
If you try to use a combination of malloc() and snprintf() to produce correct behavior in all cases, you end up with roughly twice as much code than I have given for asprintf(), and basically reprogram the functionality of asprintf().
If you are looking at providing a wrapper of log_out() that can take a printf() style parameter list itself, you can use the variant vasprintf() which takes a va_list as an argument. Here is a perfectly safe implementation of such a wrapper:
//Tell gcc that we are defining a printf-style function so that it can do type checking.
//Obviously, this should go into a header.
void log_out_wrapper(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
void log_out_wrapper(const char *format, ...) {
char* string;
va_list args;
va_start(args, format);
if(0 > vasprintf(&string, format, args)) string = NULL; //this is for logging, so failed allocation is not fatal
va_end(args);
if(string) {
log_out(string);
free(string);
} else {
log_out("Error while logging a message: Memory allocation failed.\n");
}
}
It sounds to me like you want to be able to easily pass a string created using printf-style formatting to the function you already have that takes a simple string. You can create a wrapper function using stdarg.h facilities and vsnprintf() (which may not be readily available, depending on your compiler/platform):
#include <stdarg.h>
#include <stdio.h>
// a function that accepts a string:
void foo( char* s);
// You'd like to call a function that takes a format string
// and then calls foo():
void foofmt( char* fmt, ...)
{
char buf[100]; // this should really be sized appropriately
// possibly in response to a call to vsnprintf()
va_list vl;
va_start(vl, fmt);
vsnprintf( buf, sizeof( buf), fmt, vl);
va_end( vl);
foo( buf);
}
int main()
{
int val = 42;
foofmt( "Some value: %d\n", val);
return 0;
}
For platforms that don't provide a good implementation (or any implementation) of the snprintf() family of routines, I've successfully used a nearly public domain snprintf() from Holger Weiss.
Don't use sprintf.
It will overflow your String-Buffer and crash your Program.
Always use snprintf
If you have the code to log_out(), rewrite it. Most likely, you can do:
static FILE *logfp = ...;
void log_out(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vfprintf(logfp, fmt, args);
va_end(args);
}
If there is extra logging information needed, that can be printed before or after the message shown. This saves memory allocation and dubious buffer sizes and so on and so forth. You probably need to initialize logfp to zero (null pointer) and check whether it is null and open the log file as appropriate - but the code in the existing log_out() should be dealing with that anyway.
The advantage to this solution is that you can simply call it as if it was a variant of printf(); indeed, it is a minor variant on printf().
If you don't have the code to log_out(), consider whether you can replace it with a variant such as the one outlined above. Whether you can use the same name will depend on your application framework and the ultimate source of the current log_out() function. If it is in the same object file as another indispensable function, you would have to use a new name. If you cannot work out how to replicate it exactly, you will have to use some variant like those given in other answers that allocates an appropriate amount of memory.
void log_out_wrapper(const char *fmt, ...)
{
va_list args;
size_t len;
char *space;
va_start(args, fmt);
len = vsnprintf(0, 0, fmt, args);
va_end(args);
if ((space = malloc(len + 1)) != 0)
{
va_start(args, fmt);
vsnprintf(space, len+1, fmt, args);
va_end(args);
log_out(space);
free(space);
}
/* else - what to do if memory allocation fails? */
}
Obviously, you now call the log_out_wrapper() instead of log_out() - but the memory allocation and so on is done once. I reserve the right to be over-allocating space by one unnecessary byte - I've not double-checked whether the length returned by vsnprintf() includes the terminating null or not.
Verified and Summary:
sprintf vs asprintf
asprintf = malloc + sprintf
sample code
sprintf
int largeEnoughBufferLen = 20;
char *someStr = (char*)malloc(largeEnoughBufferLen * sizeof(char));
sprintf(someStr, "formatted string: %s %s!", "Hello", "world");
// do what you want for formatted string: someStr
free(someStr);
asprintf
char *someStr;
int formattedStrResult = asprintf(&someStr, "formatted string: %s %s!", "Hello", "world");
if(formattedStrResult > 0){
// do what you want for formatted string: someStr
free(someStr);
} else {
// some error
}
I haven't done this, so I'm just going to point at the right answer.
C has provisions for functions that take unspecified numbers of operands, using the <stdarg.h> header. You can define your function as void log_out(const char *fmt, ...);, and get the va_list inside the function. Then you can allocate memory and call vsprintf() with the allocated memory, format, and va_list.
Alternately, you could use this to write a function analogous to sprintf() that would allocate memory and return the formatted string, generating it more or less as above. It would be a memory leak, but if you're just logging out it may not matter.
http://www.gnu.org/software/hello/manual/libc/Variable-Arguments-Output.html gives the following example to print to stderr. You can modify it to use your log function instead:
#include <stdio.h>
#include <stdarg.h>
void
eprintf (const char *template, ...)
{
va_list ap;
extern char *program_invocation_short_name;
fprintf (stderr, "%s: ", program_invocation_short_name);
va_start (ap, template);
vfprintf (stderr, template, ap);
va_end (ap);
}
Instead of vfprintf you will need to use vsprintf where you need to provide an adequate buffer to print into.

create a my_printf that sends data to both a sprintf and the normal printf?

I am playing with the printf and the idea to
write a my_printf(...) that calls the normal printf
and a sprintf that sends the result to a special function.
(I was thinking about sprintf since that behaves just like printf on most platforms).
My idea was to write a small macro that did this:
#define my_printf(X, Y...) do{ printf(X, ## Y); \
char* data = malloc(strlen(X)*sizeof(char)); \
sprintf(data, X, ## Y); \
other_print(data);\
free(data);}while(0)
But since sprintf can expand the string to a much bigger size than X,
this method breaks almost directly.
And just to add a number do the malloc seems to be the wrong way to attack the problem,
since then I would just move the problem into the future and a day when I want print a big expression...
Does anyone has a better idea on how to attack this problem?
Or how do I know how big the sprintf result will be?
Thanks
Johan
Update: I forgot that printf returns how many chars it prints,
and since I already is calling printf in the macro it was a very easy thing to add
a int that saves the number.
#define buf_printf(X, Y...) do{ int len = printf(X, ## Y); \
char* data = malloc((len+1)*sizeof(char)); \
sprintf(data, X, ## Y); \
other_print(data);\
free(data);}while(0)
Update: I was thinking about this and maybe to use a normal function
that looks a lot like what ephemient has suggested is a good idea.
The key there seems to be the v-version of the different printf functions
(vprintf, vsprintf and vsnprintf). Thanks for pointing that out.
Thanks again
Johan
Use snprintf to calculate the size. From the man page:
" If the output was truncated due to this limit then the return value is the number of characters (not including the trailing '\0') which would have been written to the final string if enough space had been available "
snprintf is standard from C99. If you only have a C89 compiler then check the documentation: pre-standard versions might not return the value you want. Again according to the man page, glibc prior to version 2.1 used to return -1 if the output was truncated, not the required size.
By the way, sizeof(char) is defined to be 1, always, in every C implementation ever :-)
The best way to do this is with varargs. Create a function with the same prototype as printf() and use the varargs functions to pass data to sprintf to populate the desired buffer, the also pass that buffer to printf("%s") before returning.
A lot of the early implementations had a 4K limit on the lowest level printf() call but I would opt for more than that. You probably need to just set an upper limit and stick to it.
One trick we used in a logging system was to write the data using printf() to a /dev/null handle. Since printf() returns the number of characters written, we then used that to allocate a buffer. But that wasn't very efficient since it involved calling a printf()-type function twice.
Since you are on Linux I'd suggest to use asprintf() - it it GNU extension that allocates string for you.
And thanks to C99 variadic macros you don't need to mess with varagrs.
So your macro would look like:
#define MY_PRINT(...) do { \
char *data; \
asprintf(&data, __VA_ARGS__); \
printf("%s", data); \
other_print(data); \
free(data); \
} while (0)
NB! This is C99 and GNU-only code
Edit: Now it will evaluate macro arguments just once so calling macro with something like ("%d", i++) will work correctly.
If you're always running on a system with glibc (i.e. Linux and any other OS with GNU userland), asprintf acts just like sprintf but can automatically handle allocation itself.
int my_printf(const char *fmt, ...) {
char *buf = NULL;
int len;
va_list ap;
va_start(ap, &fmt);
len = vasprintf(&buf, fmt, ap);
va_end(ap);
if (len < 0) {
/* error: allocation failed */
return len;
}
puts(buf);
other_print(buf);
free(buf);
return len;
}
Pax's answer is more portable, but instead of printing to /dev/null, here's a better trick: by POSIX, snprintf can be given a NULL buffer and 0 size, and it'll return how much it would have written -- but obviously it won't actually write anything.
int my_printf(const char *fmt, ...) {
char *buf;
int len, len2;
va_list ap;
va_start(ap, &fmt);
len = vsnprintf(NULL, 0, fmt, ap);
va_end(ap);
buf = malloc(len + 1);
if (!buf) {
/* error: allocation failed */
return -1;
}
va_start(ap, &fmt);
len2 = snprintf(buf, len + 1, fmt, ap);
buf[len] = '\0';
va_end(ap);
/* has another thread been messing with our arguments?
oh well, nothing we can do about it */
assert(len == len2);
puts(buf);
other_printf(buf);
free(buf);
return len;
}
Well, as onebyone says, some old systems don't have a compliant snprintf. Can't win them all...

Resources