From 9ffbb05b445496c63f69e85db489d839dce3e68f Mon Sep 17 00:00:00 2001 From: Viktor Szakats Date: Sun, 16 Apr 2023 15:48:59 +0000 Subject: [PATCH] src: add and use `LIBSSH2_MIN/MAX` macros Also for #797 Closes #974 --- src/bcrypt_pbkdf.c | 4 +--- src/libssh2_priv.h | 3 +++ src/sftp.c | 12 +++++++----- src/sftp.h | 4 ---- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/bcrypt_pbkdf.c b/src/bcrypt_pbkdf.c index c9e6acb0..5382c0bd 100644 --- a/src/bcrypt_pbkdf.c +++ b/src/bcrypt_pbkdf.c @@ -28,8 +28,6 @@ #define LIBSSH2_BCRYPT_PBKDF_C #include "blowfish.c" -#define MINIMUM(a,b) (((a) < (b)) ? (a) : (b)) - /* * pkcs #5 pbkdf2 implementation using the "bcrypt" hash * @@ -164,7 +162,7 @@ bcrypt_pbkdf(const char *pass, size_t passlen, const uint8_t *salt, /* * pbkdf2 deviation: output the key material non-linearly. */ - amt = MINIMUM(amt, keylen); + amt = LIBSSH2_MIN(amt, keylen); for(i = 0; i < amt; i++) { size_t dest = i * stride + (count - 1); if(dest >= origkeylen) { diff --git a/src/libssh2_priv.h b/src/libssh2_priv.h index 428dad1c..58e1ba96 100644 --- a/src/libssh2_priv.h +++ b/src/libssh2_priv.h @@ -156,6 +156,9 @@ struct iovec { #define UINT_MAX 0xFFFFFFFF #endif +#define LIBSSH2_MAX(x, y) ((x) > (y) ? (x) : (y)) +#define LIBSSH2_MIN(x, y) ((x) < (y) ? (x) : (y)) + /* RFC4253 section 6.1 Maximum Packet Length says: * * "All implementations MUST be able to process packets with diff --git a/src/sftp.c b/src/sftp.c index a7c25cc8..f8d811ae 100644 --- a/src/sftp.c +++ b/src/sftp.c @@ -1405,7 +1405,7 @@ static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE * handle, char *buffer, return that now as we can't risk being interrupted later with data partially written to the buffer. */ if(filep->data_left) { - size_t copy = MIN(buffer_size, filep->data_left); + size_t copy = LIBSSH2_MIN(buffer_size, filep->data_left); memcpy(buffer, &filep->data[ filep->data_len - filep->data_left], copy); @@ -1519,8 +1519,9 @@ static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE * handle, char *buffer, /* add this new entry LAST in the list */ _libssh2_list_add(&handle->packet_list, &chunk->node); - count -= MIN(size, count); /* deduct the size we used, as we might - * have to create more packets */ + /* deduct the size we used, as we might have to create + more packets */ + count -= LIBSSH2_MIN(size, count); _libssh2_debug((session, LIBSSH2_TRACE_SFTP, "read request id %d sent (offset: %d, size: %d)", request_id, (int)chunk->offset, (int)chunk->len)); @@ -2076,7 +2077,8 @@ static ssize_t sftp_write(LIBSSH2_SFTP_HANDLE *handle, const char *buffer, while(count) { /* TODO: Possibly this should have some logic to prevent a very very small fraction to be left but lets ignore that for now */ - uint32_t size = (uint32_t)(MIN(MAX_SFTP_OUTGOING_SIZE, count)); + uint32_t size = + (uint32_t)(LIBSSH2_MIN(MAX_SFTP_OUTGOING_SIZE, count)); uint32_t request_id; /* 25 = packet_len(4) + packet_type(1) + request_id(4) + @@ -2226,7 +2228,7 @@ static ssize_t sftp_write(LIBSSH2_SFTP_HANDLE *handle, const char *buffer, acked += handle->u.file.acked; if(acked) { - ssize_t ret = MIN(acked, org_count); + ssize_t ret = LIBSSH2_MIN(acked, org_count); /* we got data acked so return that amount, but no more than what was asked to get sent! */ diff --git a/src/sftp.h b/src/sftp.h index b0019b88..891a89db 100644 --- a/src/sftp.h +++ b/src/sftp.h @@ -67,10 +67,6 @@ struct sftp_zombie_requests { uint32_t request_id; }; -#ifndef MIN -#define MIN(x,y) ((x) < (y) ? (x) : (y)) -#endif - struct _LIBSSH2_SFTP_PACKET { struct list_node node; /* linked list header */