From 1ee7a0f668ebc874a16ca839dfb972b778115d11 Mon Sep 17 00:00:00 2001 From: axxel Date: Sat, 12 Oct 2024 19:04:18 +0200 Subject: [PATCH] ptp2: make a failed realloc in array_extend() return GP_ERROR_NO_MEMORY To be able to use the array macros in PTP and GP error "contexts", this makes translate_ptp_result() tolerate both "types" of errors. I added a comment regarding the use of the GP_ERROR_NO_MEMORY use. This is related to #1008. --- camlibs/ptp2/library.c | 13 ++++++++++++- camlibs/ptp2/ptp.h | 8 ++++++-- libgphoto2_port/gphoto2/gphoto2-port-result.h | 7 +++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/camlibs/ptp2/library.c b/camlibs/ptp2/library.c index cf21a85f7d..29ba9811e5 100644 --- a/camlibs/ptp2/library.c +++ b/camlibs/ptp2/library.c @@ -133,7 +133,18 @@ translate_ptp_result (uint16_t result) case PTP_ERROR_IO: case PTP_ERROR_DATA_EXPECTED: case PTP_ERROR_RESP_EXPECTED: return GP_ERROR_IO; - default: return GP_ERROR; + default: { + /* If result is actually a GP_ERROR code (see gphoto2-port-result.h), then we simply pass it through as is. + * This works because those two value ranges have no overlap: + * - PTP errors are either 0x0..., 0x2.... or 0xA.... + * - GP errors (as uint16_t) are 0xF... + */ + int int_res = (int16_t)result; + if (-99 <= int_res && int_res <= 0) + return int_res; + else + return GP_ERROR; + } } } diff --git a/camlibs/ptp2/ptp.h b/camlibs/ptp2/ptp.h index 494f1da06c..bff5a2e751 100644 --- a/camlibs/ptp2/ptp.h +++ b/camlibs/ptp2/ptp.h @@ -31,6 +31,8 @@ #include #endif #include "libgphoto2/gphoto2-endian.h" +#include +#include #include "device-flags.h" #ifdef __cplusplus @@ -102,8 +104,10 @@ static inline uint32_t _post_inc(uint32_t* o, int n) #define array_extend(ARRAY, LEN) do { \ (ARRAY)->val = realloc((ARRAY)->val, ((ARRAY)->len + (LEN)) * sizeof((ARRAY)->val[0])); \ - if (!(ARRAY)->val) \ - ptp_error(params, "PANIC: realloc failed"); \ + if (!(ARRAY)->val) { \ + GP_LOG_E ("Out of memory: 'realloc' of %ld bytes failed.", ((ARRAY)->len + (LEN)) * sizeof((ARRAY)->val[0])); \ + return GP_ERROR_NO_MEMORY; \ + } \ } while(0) #define array_push_back(ARRAY, VAL) do { \ diff --git a/libgphoto2_port/gphoto2/gphoto2-port-result.h b/libgphoto2_port/gphoto2/gphoto2-port-result.h index 319a73b1e0..70c5bc8196 100644 --- a/libgphoto2_port/gphoto2/gphoto2-port-result.h +++ b/libgphoto2_port/gphoto2/gphoto2-port-result.h @@ -40,6 +40,13 @@ * \brief Out of memory */ #define GP_ERROR_NO_MEMORY -3 +/* FIXME: GP_ERROR_NO_MEMORY is used to communicate two completely differnt + * things, which have nothing to do with each other: + * - the camera went out of memory because the storage space ran out, this + * is totally "normal" + * - a malloc on the host computer failed: this will completely interrupt + * the functionality and likely crash the process soonish + */ /** * \brief Error in the camera driver */