diff options
author | Shelley Vohr <shelley.vohr@gmail.com> | 2018-06-20 10:01:57 -0700 |
---|---|---|
committer | Santiago Gimeno <santiago.gimeno@gmail.com> | 2018-07-06 19:35:21 +0200 |
commit | 5124b27d355a868a07c51ca694c45b32b10f89b5 (patch) | |
tree | ffcd0664ea156ad315b775f6f60ab2a35e4a9efb /src | |
parent | b16d10a0177c32d6a9442d0758031cac8a5db36e (diff) | |
download | libuv-5124b27d355a868a07c51ca694c45b32b10f89b5.tar.gz libuv-5124b27d355a868a07c51ca694c45b32b10f89b5.zip |
src: add new error apis to prevent memory leaks
This PR creates two new externally-facing APIs, uv_err_name_r() and
uv_strerror_r().
In keeping with the precedent set by POSIX, the *_r() suffix of these
two new methods indicate that the caller does the memory management and
passes in the memory that the output will be stored in, which provides
an alternative for the two existent methods (uv_err_name() and
uv_strerror()), which, when called with an unknown error code, leak a
few bytes of memory.
PR-URL: https://github.com/libuv/libuv/pull/1898
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Santiago Gimeno santiago.gimeno@gmail.com
Diffstat (limited to 'src')
-rw-r--r-- | src/uv-common.c | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/uv-common.c b/src/uv-common.c index 1146cfa3..f0aec452 100644 --- a/src/uv-common.c +++ b/src/uv-common.c @@ -155,6 +155,18 @@ static const char* uv__unknown_err_code(int err) { return copy != NULL ? copy : "Unknown system error"; } +#define UV_ERR_NAME_GEN_R(name, _) \ +case UV_## name: \ + snprintf(buf, buflen, "%s", #name); break; +char* uv_err_name_r(int err, char* buf, size_t buflen) { + switch (err) { + UV_ERRNO_MAP(UV_ERR_NAME_GEN_R) + default: snprintf(buf, buflen, "Unknown system error %d", err); + } + return buf; +} +#undef UV_ERR_NAME_GEN_R + #define UV_ERR_NAME_GEN(name, _) case UV_ ## name: return #name; const char* uv_err_name(int err) { @@ -166,6 +178,19 @@ const char* uv_err_name(int err) { #undef UV_ERR_NAME_GEN +#define UV_STRERROR_GEN_R(name, msg) \ +case UV_ ## name: \ + snprintf(buf, buflen, "%s", msg); break; +char* uv_strerror_r(int err, char* buf, size_t buflen) { + switch (err) { + UV_ERRNO_MAP(UV_STRERROR_GEN_R) + default: snprintf(buf, buflen, "Unknown system error %d", err); + } + return buf; +} +#undef UV_STRERROR_GEN_R + + #define UV_STRERROR_GEN(name, msg) case UV_ ## name: return msg; const char* uv_strerror(int err) { switch (err) { |