I’m having a hard time understanding why you would need asprintf.
Here in the manual it says
The functions
asprintf()andvasprintf()are analogs ofsprintf(3)and
vsprintf(3), except that they allocate a string large enough to hold
the output including the terminating null byte, and return a pointer
to it via the first argument. This pointer should be passed to
free(3)to release the allocated storage when it is no longer needed.
So here is the example that I’m trying to understand:
asprintf(&buffer, "/bin/echo %s is cool", getenv("USER"));
What’s the difference if the buffer allocates a string large enough vs saying char* = (string)
If you use
sprintf()orvsprintf(), you need to allocate a buffer first, and you need to be sure that the buffer is large enough to contain what sprintf writes. Otherwisesprintf()will happily overwrite whatever memory lies beyond the end of the buffer.… writes the ‘6’ and the terminating
nullbeyond the end of the space allocated tox, either corrupting some other variable, or causing a segmentation fault.If you’re lucky, it will trample on memory in-between allocated blocks, and will do no harm — this time. This leads to intermittent bugs — the hardest kind to diagnose. It’s good to use a tool like ElectricFence that causes overruns to fail-fast.
A non-malicious user who provides an overlong input, could cause the program to behave in unexpected ways. A malicious user could exploit this as a way to get their own executable code into the system.
One guard against this is to use
snprintf(), which truncates the string to the maximum length you supply.The return value
sizeis the length that would have been written if space was available — not including the terminating null.In this case, if
sizeis greater than or equal to 5 then you know that truncation occurred – and if you didn’t want truncation, you could allocate a new string and trysnprintf()again.(That’s a pretty naive algorithm, but it illustrates the point. There may yet be bugs in it, which further illustrates the point — this stuff is easy to screw up.)
asprintf()does this in one step for you – calculates the length of the string, allocates that amount of memory, and writes the string into it.In all cases, once you’ve finished with
xyou need to release it, or you leak memory:asprintf()is an implicitmalloc(), so you have to check it worked, just as you would withmalloc()or any other system call.Note that
asprintf()is part of the GNU and BSD extensions to libc – you can’t be sure it will be available in every C environment.sprintf()andsnprintf()are part of the POSIX and C99 standards.