From 042119898d637367054db557b8f56e9b4b81a320 Mon Sep 17 00:00:00 2001 From: Douglas Clowes Date: Fri, 12 Sep 2014 13:00:26 +1000 Subject: [PATCH] SICS-778 Fix crash in conman.c hookFunc --- conman.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/conman.c b/conman.c index 6afdc5cc..e069cc51 100644 --- a/conman.c +++ b/conman.c @@ -2127,8 +2127,17 @@ static void hookFunc(const char *pText, OutCode eOut, void*pData) { SConnection *pCon = (SConnection *) pData; int text_len = strlen(pText); + char txt[5]; - ANETwrite(pCon->sockHandle, pText, text_len); + if (!VerifyConnection(pCon)) { + return; + } + if (!ANETvalidHandle(pCon->sockHandle)) { + return; + } + snprintf(txt, 5, "%3s:", OutCodeToTxt(eOut)); + ANETwrite(pCon->sockHandle, txt, 4); + ANETwrite(pCon->sockHandle, (char *)pText, text_len); if (pText[text_len - 1] != '\n') ANETwrite(pCon->sockHandle, "\n", 1); } @@ -2151,9 +2160,13 @@ int KillCapture(SConnection * pCon) int LogCapture(SConnection * pCon, SicsInterp * pSics, void *pData, int argc, char *argv[]) { + SConnection * pConMaster; char pBueffel[512]; int i; + pConMaster = SCfindMaster(pCon); + if (pConMaster == NULL) + return 0; /* check no af args */ if (argc < 2) { snprintf(pBueffel,sizeof(pBueffel)-1, "Insufficient number of arguments to %s", argv[0]); @@ -2164,18 +2177,19 @@ int LogCapture(SConnection * pCon, SicsInterp * pSics, void *pData, /* Branch according to argv[1] */ if (strcmp(argv[1], "kill") == 0) { - KillCapture(pCon); + KillCapture(pConMaster); return 1; } else if (strcmp(argv[1], "all") == 0) { - AddSICSLogHook(hookFunc, "all", pCon); + AddSICSLogHook(hookFunc, "all", pConMaster); return 1; } else if (argc == 2) { /* must be outcode, try find it */ - AddSICSLogHook(hookFunc, argv[1], pCon); + AddSICSLogHook(hookFunc, argv[1], pConMaster); return 1; } else { /* make it a list */ - int i, len; + int i; + size_t len; char *pBuff; for (i = 1, len = 0; i < argc; ++i) len += strlen(argv[i]) + 1; @@ -2193,7 +2207,7 @@ int LogCapture(SConnection * pCon, SicsInterp * pSics, void *pData, strcpy(&pBuff[len], argv[i]); len += strlen(argv[i]); } - AddSICSLogHook(hookFunc, pBuff, pCon); + AddSICSLogHook(hookFunc, pBuff, pConMaster); if (pBuff != pBueffel) free(pBuff); return 1; @@ -2212,7 +2226,8 @@ int LogOutput(SConnection * pCon, SicsInterp * pSics, void *pData, { char pBueffel[512]; char *pBuff; - int i, len, result, start; + int i, result, start; + size_t len; OutCode outcode; /* check no af args */ @@ -2323,7 +2338,7 @@ int SCTaskFunction(void *pData) pPassword = strtok(NULL, " \t\r\n"); iRet = IsValidUser(pUser, pPassword); if (iRet >= 0) { - SCWrite(self, "Login OK", eError); + SCWrite(self, "Login OK", eLog); self->iLogin = 1; SCSetRights(self, iRet); pHost[0] = '\0';