8000 Add try_into conversion helper and drop gcc 4.8 support by sreimers · Pull Request #286 · baresip/re · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add try_into conversion helper and drop gcc 4.8 support #286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/centos7.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:

strategy:
matrix:
compiler: [gcc, clang]
compiler: [clang]
os: [ubuntu-latest]

env:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ legend:

### Supported compilers:

* gcc 4.x or later
* ms vc2003 compiler
* gcc 4.9 or later
* MSVC 2019
* clang 3.x or later


Expand Down
1 change: 1 addition & 0 deletions include/re.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extern "C" {
#include "re_bfcp.h"
#include "re_btrace.h"
#include "re_conf.h"
#include "re_convert.h"
#include "re_crc32.h"
#include "re_dns.h"
#include "re_hash.h"
Expand Down
79 changes: 79 additions & 0 deletions include/re_convert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* @file re_convert.h Conversion helpers
*
* Copyright (C) 2022 Sebastian Reimers
*/
#include <limits.h>

static inline int try_into_u16_from_size(uint16_t *dest, const size_t src)
{
*dest = 0;

if (src > UINT16_MAX)
return ERANGE;

*dest = (uint16_t)src;

return 0;
}


static inline int try_into_u16_from_int(uint16_t *dest, const int src)
{
*dest = 0;

if (src > UINT16_MAX)
return ERANGE;

if (src < 0)
return ERANGE;

*dest = (uint16_t)src;

return 0;
}


static inline int try_into_int_from_size(int *dest, const size_t src)
{
*dest = 0;

if (src > INT_MAX)
return ERANGE;

*dest = (int)src;

return 0;
}


static inline int try_into_err(void *dest, ...)
{
(void)dest;

return ENOTSUP;
}


/**
* Try to convert safely from one type (src) into another (dest).
* Types are auto detected.
*
* @param dest Destination
* @param src Source value
*
* @return 0 if success, ERANGE if value overflow and ENOTSUP if not supported
*/
#define try_into(dest, src) \
_Generic((dest), \
uint16_t: _Generic((src), \
size_t: try_into_u16_from_size, \
int: try_into_u16_from_int, \
default: try_into_err \
), \
int: _Generic((src), \
size_t: try_into_int_from_size, \
default: try_into_err \
) \
) \
(&(dest), (src))
11 changes: 9 additions & 2 deletions src/shim/shim.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <re_tcp.h>
#include <re_net.h>
#include <re_shim.h>
#include <re_convert.h>


#define DEBUG_MODULE "shim"
Expand All @@ -36,7 +37,8 @@ struct shim {
static bool shim_send_handler(int *err, struct mbuf *mb, void *arg)
{
struct shim *shim = arg;
size_t len;
int err_len;
uint16_t len;
(void)shim;

if (mb->pos < SHIM_HDR_SIZE) {
Expand All @@ -45,7 +47,12 @@ static bool shim_send_handler(int *err, struct mbuf *mb, void *arg)
return true;
}

len = mbuf_get_left(mb);
err_len = try_into(len, mbuf_get_left(mb));
if (err_len) {
DEBUG_WARNING("send: mbuf to big\n");
*err = err_len;
return true;
}

mb->pos -= SHIM_HDR_SIZE;
*err = mbuf_write_u16(mb, htons(len));
Expand Down
0