Add dbPvt2Rec() cast

Reverse of dbRec2Pvt()

pacify -D_FORTIFY_SOURCE=3 and __builtin_object_size() as
"&precord->common" does not know than common as actually
the prefix of a variable sized struct.
This commit is contained in:
Michael Davidsaver
2024-06-14 14:49:30 -07:00
committed by Andrew Johnson
parent f9e53dded6
commit 3d70e70640
2 changed files with 23 additions and 5 deletions

View File

@ -15,13 +15,21 @@ typedef struct dbCommonPvt {
/* Thread which is currently processing this record */
struct epicsThreadOSD* procThread;
struct dbCommon common;
/* actually followed by:
* struct dbCommon common;
*/
} dbCommonPvt;
static EPICS_ALWAYS_INLINE
dbCommonPvt* dbRec2Pvt(struct dbCommon *prec)
{
return CONTAINER(prec, dbCommonPvt, common);
return (dbCommonPvt*)((char*)prec - sizeof(dbCommonPvt));
}
static EPICS_ALWAYS_INLINE
dbCommon* dbPvt2Rec(struct dbCommonPvt *pvt)
{
return (dbCommon*)&pvt[1];
}
#endif // DBCOMMONPVT_H

View File

@ -66,7 +66,17 @@ void devExtend(dsxt *pdsxt)
pthisDevSup->pdsxt = pdsxt;
}
}
/* Ensure that: sizeof(dbCommonPvt) + pdbRecordType->rec_size
* accounts for necessary alignment of record type struct
*/
typedef struct {
dbCommonPvt prefix;
/* ensure no padding needed... */
dbCommon suffix;
} dbCommonPvtAlignmentTest;
STATIC_ASSERT(offsetof(dbCommonPvtAlignmentTest, suffix)==sizeof(dbCommonPvt));
long dbAllocRecord(DBENTRY *pdbentry,const char *precordName)
{
dbRecordType *pdbRecordType = pdbentry->precordType;
@ -90,8 +100,8 @@ long dbAllocRecord(DBENTRY *pdbentry,const char *precordName)
precordName, pdbRecordType->name, pdbRecordType->rec_size);
return(S_dbLib_noRecSup);
}
ppvt = dbCalloc(1, offsetof(dbCommonPvt, common) + pdbRecordType->rec_size);
precord = &ppvt->common;
ppvt = dbCalloc(1, sizeof(dbCommonPvt) + pdbRecordType->rec_size);
precord = dbPvt2Rec(ppvt);
ppvt->recnode = precnode;
precord->rdes = pdbRecordType;
precnode->precord = precord;