Skip to content

Commit

Permalink
libfetch: fix fetch_err* for negative error codes
Browse files Browse the repository at this point in the history
The earlier fix for 32-bit arches also messed up the sign
extension for error code portion. Fix this by using a
struct instead of trying to be clever with the bit shifts.

fixes 8d41ff4 fetch: fix fetch_err_make() for 32-bit arches
fixes #11082
  • Loading branch information
fabled committed Feb 12, 2025
1 parent 123fa05 commit 5fbb854
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 19 deletions.
4 changes: 2 additions & 2 deletions libfetch/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ int fetch_netrc_auth(struct url *url);
int fetch_no_proxy_match(const char *);
int fetch_urlpath_safe(char);

static inline void _fetch_seterr(unsigned char category, int code) {
fetchLastErrCode = fetch_err_make(category, code);
static inline void _fetch_seterr(unsigned int category, int code) {
fetchLastErrCode = (struct fetch_error) { .category = category, .code = code };
}
static inline void fetch_syserr(void) {
_fetch_seterr(FETCH_ERRCAT_ERRNO, errno);
Expand Down
2 changes: 1 addition & 1 deletion libfetch/fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

fetch_redirect_t fetchRedirectMethod;
auth_t fetchAuthMethod;
long fetchLastErrCode;
struct fetch_error fetchLastErrCode;
int fetchTimeout;
volatile int fetchRestartCalls = 1;
int fetchDebug;
Expand Down
14 changes: 5 additions & 9 deletions libfetch/fetch.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,10 @@ enum {
FETCH_ERR_TLS_HANDSHAKE,
};

#define fetch_err_make(category, code) ((((unsigned long)category) << 28) + (unsigned long)code)

static inline unsigned char fetch_err_category(unsigned long err) {
return (unsigned char)(err >> 28);
}
static inline int fetch_err_code(unsigned long err) {
return (int)(err & 0xfffffff);
}
struct fetch_error {
unsigned int category;
int code;
};

#if defined(__cplusplus)
extern "C" {
Expand Down Expand Up @@ -172,7 +168,7 @@ typedef int (*auth_t)(struct url *);
extern auth_t fetchAuthMethod;

/* Last error code */
extern long fetchLastErrCode;
extern struct fetch_error fetchLastErrCode;

/* I/O timeout */
extern int fetchTimeout;
Expand Down
14 changes: 7 additions & 7 deletions src/io_url_libfetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static int fetch_maperr(const struct maperr *map, size_t mapsz, int ec, int defa
return default_apkerr;
}

static int fetch_maperror(long ec)
static int fetch_maperror(struct fetch_error fe)
{
static const struct maperr fetch_err[] = {
{ FETCH_OK, 0, },
Expand Down Expand Up @@ -73,19 +73,19 @@ static int fetch_maperror(long ec)
{ 504, APKE_HTTP_504_GATEWAY_TIMEOUT },
};

switch (fetch_err_category(ec)) {
switch (fe.category) {
case FETCH_ERRCAT_FETCH:
return fetch_maperr(fetch_err, ARRAY_SIZE(fetch_err), fetch_err_code(ec), EIO);
return fetch_maperr(fetch_err, ARRAY_SIZE(fetch_err), fe.code, EIO);
case FETCH_ERRCAT_URL:
return APKE_URL_FORMAT;
case FETCH_ERRCAT_ERRNO:
return fetch_err_code(ec);
return fe.code;
case FETCH_ERRCAT_NETDB:
return fetch_maperr(netdb_err, ARRAY_SIZE(netdb_err), fetch_err_code(ec), APKE_DNS_FAIL);
return fetch_maperr(netdb_err, ARRAY_SIZE(netdb_err), fe.code, APKE_DNS_FAIL);
case FETCH_ERRCAT_HTTP:
return fetch_maperr(http_err, ARRAY_SIZE(http_err), fetch_err_code(ec), APKE_HTTP_UNKNOWN);
return fetch_maperr(http_err, ARRAY_SIZE(http_err), fe.code, APKE_HTTP_UNKNOWN);
case FETCH_ERRCAT_TLS:
return fetch_maperr(tls_err, ARRAY_SIZE(tls_err), fetch_err_code(ec), APKE_TLS_ERROR);
return fetch_maperr(tls_err, ARRAY_SIZE(tls_err), fe.code, APKE_TLS_ERROR);
default:
return EIO;
}
Expand Down

0 comments on commit 5fbb854

Please # to comment.