From e7e1312b0cbfa643e2f8bf5f2036ce5147ed797d Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 21 Mar 2022 09:31:39 +0100 Subject: [PATCH] misc/libssh2_copy_string: avoid malloc zero bytes Avoids the inconsistent malloc return code for malloc(0) Closes #686 --- src/misc.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/misc.c b/src/misc.c index 5e4c4335..4f1e0543 100644 --- a/src/misc.c +++ b/src/misc.c @@ -817,12 +817,18 @@ int _libssh2_copy_string(LIBSSH2_SESSION *session, struct string_buf *buf, return -1; } - *outbuf = LIBSSH2_ALLOC(session, str_len); - if(*outbuf) { - memcpy(*outbuf, str, str_len); + if(str_len) { + *outbuf = LIBSSH2_ALLOC(session, str_len); + if(*outbuf) { + memcpy(*outbuf, str, str_len); + } + else { + return -1; + } } else { - return -1; + *outlen = 0; + *outbuf = NULL; } if(outlen)