Re: bgpd, use correct size for ASPATH_HEADER_SIZE

2018-10-25 Thread Denis Fondras
On Thu, Oct 25, 2018 at 10:57:58AM +0200, Claudio Jeker wrote:
> Currently struct aspath is defined with a placeholder for the dynamic data
> part.
> struct aspath {
> LIST_ENTRY(aspath)  entry;
> int refcnt; /* reference count */
> u_int16_t   len;/* total length of aspath in octets */
> u_int16_t   ascnt;  /* number of AS hops in data */
> u_char  data[1]; /* placeholder for actual data */
> };
> 
> The size of the struct - this placeholder was calculated as
> ASPATH_HEADER_SIZE using (sizeof(struct aspath) - sizeof(u_char)).
> Now that does not consider any padding bytes added. Instead this should
> use offsetof(struct aspath, data) so that the malloc does not allocate too
> much memory.
> 

OK denis@

> -- 
> :wq Claudio
> 
> Index: rde.h
> ===
> RCS file: /cvs/src/usr.sbin/bgpd/rde.h,v
> retrieving revision 1.198
> diff -u -p -r1.198 rde.h
> --- rde.h 24 Oct 2018 08:26:37 -  1.198
> +++ rde.h 25 Oct 2018 08:48:38 -
> @@ -23,6 +23,7 @@
>  #include 
>  #include 
>  #include 
> +#include 
>  
>  #include "bgpd.h"
>  #include "log.h"
> @@ -125,7 +126,7 @@ struct rde_peer {
>  #define AS_SEQUENCE  2
>  #define AS_CONFED_SEQUENCE   3
>  #define AS_CONFED_SET4
> -#define ASPATH_HEADER_SIZE   (sizeof(struct aspath) - sizeof(u_char))
> +#define ASPATH_HEADER_SIZE   (offsetof(struct aspath, data))
>  
>  struct aspath {
>   LIST_ENTRY(aspath)  entry;
> 



bgpd, use correct size for ASPATH_HEADER_SIZE

2018-10-25 Thread Claudio Jeker
Currently struct aspath is defined with a placeholder for the dynamic data
part.
struct aspath {
LIST_ENTRY(aspath)  entry;
int refcnt; /* reference count */
u_int16_t   len;/* total length of aspath in octets */
u_int16_t   ascnt;  /* number of AS hops in data */
u_char  data[1]; /* placeholder for actual data */
};

The size of the struct - this placeholder was calculated as
ASPATH_HEADER_SIZE using (sizeof(struct aspath) - sizeof(u_char)).
Now that does not consider any padding bytes added. Instead this should
use offsetof(struct aspath, data) so that the malloc does not allocate too
much memory.

-- 
:wq Claudio

Index: rde.h
===
RCS file: /cvs/src/usr.sbin/bgpd/rde.h,v
retrieving revision 1.198
diff -u -p -r1.198 rde.h
--- rde.h   24 Oct 2018 08:26:37 -  1.198
+++ rde.h   25 Oct 2018 08:48:38 -
@@ -23,6 +23,7 @@
 #include 
 #include 
 #include 
+#include 
 
 #include "bgpd.h"
 #include "log.h"
@@ -125,7 +126,7 @@ struct rde_peer {
 #define AS_SEQUENCE2
 #define AS_CONFED_SEQUENCE 3
 #define AS_CONFED_SET  4
-#define ASPATH_HEADER_SIZE (sizeof(struct aspath) - sizeof(u_char))
+#define ASPATH_HEADER_SIZE (offsetof(struct aspath, data))
 
 struct aspath {
LIST_ENTRY(aspath)  entry;