Skip to content

Commit

Permalink
ptp2: make a failed realloc in array_extend() return GP_ERROR_NO_MEMORY
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
axxel authored and msmeissn committed Oct 14, 2024
1 parent 84e14fe commit 67de966
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
13 changes: 12 additions & 1 deletion camlibs/ptp2/library.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions camlibs/ptp2/ptp.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <iconv.h>
#endif
#include "libgphoto2/gphoto2-endian.h"
#include <gphoto2/gphoto2-port-log.h>
#include <gphoto2/gphoto2-port-result.h>
#include "device-flags.h"

#ifdef __cplusplus
Expand Down Expand Up @@ -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 { \
Expand Down
7 changes: 7 additions & 0 deletions libgphoto2_port/gphoto2/gphoto2-port-result.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 67de966

Please sign in to comment.