From 81550ac4d3b532ca78824ae48059e7ef4e3d6f01 Mon Sep 17 00:00:00 2001 From: Martin Konrad Date: Thu, 14 Nov 2019 10:11:16 -0500 Subject: [PATCH] Fix segfault when calling dbLoadRecords after iocInit This fixes lp:1829919. --- src/ioc/db/dbAccess.c | 15 ++++++++++++--- src/ioc/dbStatic/dbLexRoutines.c | 10 ++++++++++ src/ioc/dbStatic/dbStaticIocRegister.c | 8 ++++++++ src/ioc/dbStatic/dbStaticPvt.h | 1 + 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/ioc/db/dbAccess.c b/src/ioc/db/dbAccess.c index 4ee2d677d..0b893eeb9 100644 --- a/src/ioc/db/dbAccess.c +++ b/src/ioc/db/dbAccess.c @@ -730,9 +730,18 @@ int dbLoadDatabase(const char *file, const char *path, const char *subs) int dbLoadRecords(const char* file, const char* subs) { int status = dbReadDatabase(&pdbbase, file, 0, subs); - - if (!status && dbLoadRecordsHook) - dbLoadRecordsHook(file, subs); + switch(status) + { + case 0: + if(dbLoadRecordsHook) + dbLoadRecordsHook(file, subs); + break; + case -2: + errlogPrintf("dbLoadRecords: failed to load %s - cannot load records after running iocBuild!\n", file); + break; + default: + errlogPrintf("dbLoadRecords: failed to load %s\n", file); + } return status; } diff --git a/src/ioc/dbStatic/dbLexRoutines.c b/src/ioc/dbStatic/dbLexRoutines.c index 3df3c7f3a..9bc0f8722 100644 --- a/src/ioc/dbStatic/dbLexRoutines.c +++ b/src/ioc/dbStatic/dbLexRoutines.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "dbDefs.h" #include "dbmf.h" @@ -115,6 +116,12 @@ typedef struct tempListNode { static ELLLIST tempList = ELLLIST_INIT; static void *freeListPvt = NULL; static int duplicate = FALSE; +static bool dbLoadRecordsAllowed = true; + +void disableDbLoadRecords() +{ + dbLoadRecordsAllowed = false; +} static void yyerrorAbort(char *str) { @@ -215,6 +222,9 @@ static long dbReadCOM(DBBASE **ppdbbase,const char *filename, FILE *fp, char *penv; char **macPairs; + if(!dbLoadRecordsAllowed) + return -2; + if(*ppdbbase == 0) *ppdbbase = dbAllocBase(); pdbbase = *ppdbbase; if(path && strlen(path)>0) { diff --git a/src/ioc/dbStatic/dbStaticIocRegister.c b/src/ioc/dbStatic/dbStaticIocRegister.c index 18d346c70..7156691ac 100644 --- a/src/ioc/dbStatic/dbStaticIocRegister.c +++ b/src/ioc/dbStatic/dbStaticIocRegister.c @@ -8,12 +8,19 @@ \*************************************************************************/ #include "iocsh.h" +#include "initHooks.h" #define epicsExportSharedSymbols #include "dbStaticIocRegister.h" #include "dbStaticLib.h" #include "dbStaticPvt.h" +static void dbStaticIocRegisterInitHook(initHookState state) +{ + if(state == initHookAtIocBuild) + disableDbLoadRecords(); +} + /* common arguments */ static const iocshArg argPdbbase = { "pdbbase", iocshArgPdbbase}; @@ -153,6 +160,7 @@ static void dbReportDeviceConfigCallFunc(const iocshArgBuf *args) void dbStaticIocRegister(void) { + initHookRegister(dbStaticIocRegisterInitHook); iocshRegister(&dbDumpPathFuncDef, dbDumpPathCallFunc); iocshRegister(&dbDumpRecordFuncDef, dbDumpRecordCallFunc); iocshRegister(&dbDumpMenuFuncDef, dbDumpMenuCallFunc); diff --git a/src/ioc/dbStatic/dbStaticPvt.h b/src/ioc/dbStatic/dbStaticPvt.h index 842c0dc21..62e595e02 100644 --- a/src/ioc/dbStatic/dbStaticPvt.h +++ b/src/ioc/dbStatic/dbStaticPvt.h @@ -25,6 +25,7 @@ dbDeviceMenu *dbGetDeviceMenu(DBENTRY *pdbentry); void dbFreeLinkContents(struct link *plink); void dbFreePath(DBBASE *pdbbase); int dbIsMacroOk(DBENTRY *pdbentry); +void disableDbLoadRecords(); /*The following routines have different versions for run-time no-run-time*/ long dbAllocRecord(DBENTRY *pdbentry,const char *precordName);