Use #defines rather than sizeof.
Use ForCES_HDRL, TLV_HDRL, and ILV_HDRL instead of various sizeof's, to
make it clearer what certain tests ensure are true (as, in other cases,
the #defines are subtracted from values that should be, at that point,
ensured not to be less than the #define in question).
diff --git a/forces.h b/forces.h
index 092a30b..b9cf072 100644
--- a/forces.h
+++ b/forces.h
@@ -216,8 +216,8 @@
#define ForCES_RS2(forcesh) ((EXTRACT_32BITS(&(forcesh)->fm_flags)&0x0007FFFF) >> 0)
};
-#define ForCES_HLN_VALID(fhl,tlen) ((tlen) >= sizeof(struct forcesh) && \
- (fhl) >= sizeof(struct forcesh) && \
+#define ForCES_HLN_VALID(fhl,tlen) ((tlen) >= ForCES_HDRL && \
+ (fhl) >= ForCES_HDRL && \
(fhl) == (tlen))
#define F_LFB_RSVD 0x0
@@ -428,14 +428,14 @@
#define F_ALN_LEN(len) ( ((len)+ForCES_ALNL-1) & ~(ForCES_ALNL-1) )
#define GET_TOP_TLV(fhdr) ((struct forces_tlv *)((fhdr) + sizeof (struct forcesh)))
-#define TLV_SET_LEN(len) (F_ALN_LEN(sizeof(struct forces_tlv)) + (len))
+#define TLV_SET_LEN(len) (F_ALN_LEN(TLV_HDRL) + (len))
#define TLV_ALN_LEN(len) F_ALN_LEN(TLV_SET_LEN(len))
#define TLV_RDAT_LEN(tlv) ((int)(EXTRACT_16BITS(&(tlv)->length) - TLV_SET_LEN(0))
#define TLV_DATA(tlvp) ((void*)(((char*)(tlvp)) + TLV_SET_LEN(0)))
#define GO_NXT_TLV(tlv,rlen) ((rlen) -= F_ALN_LEN(EXTRACT_16BITS(&(tlv)->length)), \
(struct forces_tlv*)(((char*)(tlv)) \
+ F_ALN_LEN(EXTRACT_16BITS(&(tlv)->length))))
-#define ILV_SET_LEN(len) (F_ALN_LEN(sizeof(struct forces_ilv)) + (len))
+#define ILV_SET_LEN(len) (F_ALN_LEN(ILV_HDRL) + (len))
#define ILV_ALN_LEN(len) F_ALN_LEN(ILV_SET_LEN(len))
#define ILV_RDAT_LEN(ilv) ((int)(EXTRACT_32BITS(&(ilv)->length)) - ILV_SET_LEN(0))
#define ILV_DATA(ilvp) ((void*)(((char*)(ilvp)) + ILV_SET_LEN(0)))
@@ -457,9 +457,9 @@
static inline int tlv_valid(const struct forces_tlv *tlv, u_int rlen)
{
- if (rlen < (int) sizeof(struct forces_tlv))
+ if (rlen < TLV_HDRL)
return INVALID_RLEN;
- if (EXTRACT_16BITS(&tlv->length) < sizeof(struct forces_tlv))
+ if (EXTRACT_16BITS(&tlv->length) < TLV_HDRL)
return INVALID_STLN;
if (EXTRACT_16BITS(&tlv->length) > rlen)
return INVALID_LTLN;
@@ -471,9 +471,9 @@
static inline int ilv_valid(const struct forces_ilv *ilv, u_int rlen)
{
- if (rlen < sizeof(struct forces_ilv))
+ if (rlen < ILV_HDRL)
return INVALID_RLEN;
- if (EXTRACT_32BITS(&ilv->length) < sizeof(struct forces_ilv))
+ if (EXTRACT_32BITS(&ilv->length) < ILV_HDRL)
return INVALID_STLN;
if (EXTRACT_32BITS(&ilv->length) > rlen)
return INVALID_LTLN;
diff --git a/print-forces.c b/print-forces.c
index 760d3b1..efe7b5f 100644
--- a/print-forces.c
+++ b/print-forces.c
@@ -213,7 +213,6 @@
TCHECK(*pdtlv);
type = EXTRACT_16BITS(&pdtlv->type);
- tll = EXTRACT_16BITS(&pdtlv->length) - TLV_HDRL;
aln = F_ALN_LEN(EXTRACT_16BITS(&pdtlv->length));
invtlv = tlv_valid(pdtlv, len);
if (invtlv) {
@@ -223,6 +222,7 @@
EXTRACT_16BITS(&pdtlv->length));
goto pd_err;
}
+ tll = EXTRACT_16BITS(&pdtlv->length) - TLV_HDRL;
if (aln > EXTRACT_16BITS(&pdtlv->length)) {
if (aln > len) {
printf
@@ -769,7 +769,7 @@
int rc = 0;
int ttlv = 0;
- tll = mlen - sizeof(struct forcesh);
+ tll = mlen - ForCES_HDRL;
if (tll > TLV_HLN) {
if (tops->flags & ZERO_TTLV) {
@@ -821,9 +821,8 @@
break;
}
if (tll) {
- printf("\tMess TopTLV header: min %lu, total %d advertised %d ",
- (unsigned long)sizeof(struct forces_tlv),
- tll, EXTRACT_16BITS(&tltlv->length));
+ printf("\tMess TopTLV header: min %u, total %d advertised %d ",
+ TLV_HDRL, tll, EXTRACT_16BITS(&tltlv->length));
return -1;
}
@@ -860,8 +859,8 @@
printf("\n\tForCES %s ", tops->s);
if (!ForCES_HLN_VALID(mlen, len)) {
printf
- ("Illegal ForCES pkt len - min %lu, total recvd %d, advertised %d ",
- (unsigned long)sizeof(struct forcesh), len, ForCES_BLN(fhdr));
+ ("Illegal ForCES pkt len - min %u, total recvd %d, advertised %d ",
+ ForCES_HDRL, len, ForCES_BLN(fhdr));
goto error;
}