lib/string_helpers.c: change semantics of string_escape_mem
The current semantics of string_escape_mem are inadequate for one of its current users, vsnprintf(). If that is to honour its contract, it must know how much space would be needed for the entire escaped buffer, and string_escape_mem provides no way of obtaining that (short of allocating a large enough buffer (~4 times input string) to let it play with, and that's definitely a big no-no inside vsnprintf). So change the semantics for string_escape_mem to be more snprintf-like: Return the size of the output that would be generated if the destination buffer was big enough, but of course still only write to the part of dst it is allowed to, and (contrary to snprintf) don't do '\0'-termination. It is then up to the caller to detect whether output was truncated and to append a '\0' if desired. Also, we must output partial escape sequences, otherwise a call such as snprintf(buf, 3, "%1pE", "\123") would cause printf to write a \0 to buf[2] but leaving...
Showing
- fs/proc/array.c 2 additions, 2 deletionsfs/proc/array.c
- include/linux/string_helpers.h 4 additions, 4 deletionsinclude/linux/string_helpers.h
- lib/string_helpers.c 7 additions, 42 deletionslib/string_helpers.c
- lib/test-string_helpers.c 20 additions, 20 deletionslib/test-string_helpers.c
- lib/vsprintf.c 6 additions, 2 deletionslib/vsprintf.c
- net/sunrpc/cache.c 5 additions, 3 deletionsnet/sunrpc/cache.c
Please register or sign in to comment