Better handle low memory conditions.

remotes/origin/master-1.0.x
Victor Julien 15 years ago
parent f07997fd4a
commit 718fecb6fc

@ -79,7 +79,7 @@ static uint32_t FragmentDataParser(Flow *f, void *dcerpcudp_state,
*stub_data_buffer = realloc(*stub_data_buffer, *stub_data_buffer_len + stub_len); *stub_data_buffer = realloc(*stub_data_buffer, *stub_data_buffer_len + stub_len);
if (*stub_data_buffer == NULL) { if (*stub_data_buffer == NULL) {
SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory");
exit(EXIT_FAILURE); goto end;
} }
memcpy(*stub_data_buffer + *stub_data_buffer_len, input, stub_len); memcpy(*stub_data_buffer + *stub_data_buffer_len, input, stub_len);
@ -99,6 +99,7 @@ static uint32_t FragmentDataParser(Flow *f, void *dcerpcudp_state,
} }
#endif #endif
end:
SCReturnUInt((uint32_t)stub_len); SCReturnUInt((uint32_t)stub_len);
} }

@ -927,7 +927,7 @@ static uint32_t StubDataParser(DCERPC *dcerpc, uint8_t *input, uint32_t input_le
*stub_data_buffer = realloc(*stub_data_buffer, *stub_data_buffer_len + stub_len); *stub_data_buffer = realloc(*stub_data_buffer, *stub_data_buffer_len + stub_len);
if (*stub_data_buffer == NULL) { if (*stub_data_buffer == NULL) {
SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory"); SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory");
exit(EXIT_FAILURE); goto end;
} }
memcpy(*stub_data_buffer + *stub_data_buffer_len, input, stub_len); memcpy(*stub_data_buffer + *stub_data_buffer_len, input, stub_len);
@ -947,6 +947,7 @@ static uint32_t StubDataParser(DCERPC *dcerpc, uint8_t *input, uint32_t input_le
} }
#endif #endif
end:
SCReturnUInt((uint32_t)stub_len); SCReturnUInt((uint32_t)stub_len);
} }

@ -195,8 +195,10 @@ void DecodeGRE(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, uint8_t *pkt, u
if (pq != NULL) { if (pq != NULL) {
Packet *tp = PacketPseudoPktSetup(p, pkt + header_len, Packet *tp = PacketPseudoPktSetup(p, pkt + header_len,
len - header_len, IPPROTO_IP); len - header_len, IPPROTO_IP);
DecodeTunnel(tv, dtv, tp, tp->pkt, tp->pktlen, pq); if (tp != NULL) {
PacketEnqueue(pq,tp); DecodeTunnel(tv, dtv, tp, tp->pkt, tp->pktlen, pq);
PacketEnqueue(pq,tp);
}
} }
break; break;
} }
@ -206,8 +208,10 @@ void DecodeGRE(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, uint8_t *pkt, u
if (pq != NULL) { if (pq != NULL) {
Packet *tp = PacketPseudoPktSetup(p, pkt + header_len, Packet *tp = PacketPseudoPktSetup(p, pkt + header_len,
len - header_len, PPP_OVER_GRE); len - header_len, PPP_OVER_GRE);
DecodeTunnel(tv, dtv, tp, tp->pkt, tp->pktlen, pq); if (tp != NULL) {
PacketEnqueue(pq,tp); DecodeTunnel(tv, dtv, tp, tp->pkt, tp->pktlen, pq);
PacketEnqueue(pq,tp);
}
} }
break; break;
} }
@ -217,8 +221,10 @@ void DecodeGRE(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, uint8_t *pkt, u
if (pq != NULL) { if (pq != NULL) {
Packet *tp = PacketPseudoPktSetup(p, pkt + header_len, Packet *tp = PacketPseudoPktSetup(p, pkt + header_len,
len - header_len, IPPROTO_IPV6); len - header_len, IPPROTO_IPV6);
DecodeTunnel(tv, dtv, tp, tp->pkt, tp->pktlen, pq); if (tp != NULL) {
PacketEnqueue(pq,tp); DecodeTunnel(tv, dtv, tp, tp->pkt, tp->pktlen, pq);
PacketEnqueue(pq,tp);
}
} }
break; break;
} }
@ -228,8 +234,10 @@ void DecodeGRE(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, uint8_t *pkt, u
if (pq != NULL) { if (pq != NULL) {
Packet *tp = PacketPseudoPktSetup(p, pkt + header_len, Packet *tp = PacketPseudoPktSetup(p, pkt + header_len,
len - header_len, VLAN_OVER_GRE); len - header_len, VLAN_OVER_GRE);
DecodeTunnel(tv, dtv, tp, tp->pkt, tp->pktlen, pq); if (tp != NULL) {
PacketEnqueue(pq,tp); DecodeTunnel(tv, dtv, tp, tp->pkt, tp->pktlen, pq);
PacketEnqueue(pq,tp);
}
} }
break; break;
} }

@ -563,12 +563,13 @@ void DecodeIPV4(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, uint8_t *pkt,
Packet *tp = PacketPseudoPktSetup(p, pkt + IPV4_GET_HLEN(p), Packet *tp = PacketPseudoPktSetup(p, pkt + IPV4_GET_HLEN(p),
IPV4_GET_IPLEN(p) - IPV4_GET_HLEN(p), IPV4_GET_IPLEN(p) - IPV4_GET_HLEN(p),
IPV4_GET_IPPROTO(p)); IPV4_GET_IPPROTO(p));
if (tp != NULL) {
/* send that to the Tunnel decoder */
DecodeTunnel(tv, dtv, tp, tp->pkt, tp->pktlen, pq);
/* send that to the Tunnel decoder */ /* add the tp to the packet queue. */
DecodeTunnel(tv, dtv, tp, tp->pkt, tp->pktlen, pq); PacketEnqueue(pq,tp);
}
/* add the tp to the packet queue. */
PacketEnqueue(pq,tp);
} }
break; break;
} }

@ -284,9 +284,6 @@ void SCLogOutputBuffer(SCLogLevel log_level, char *msg)
SCError SCLogMessage(SCLogLevel log_level, char **msg, const char *file, SCError SCLogMessage(SCLogLevel log_level, char **msg, const char *file,
unsigned line, const char *function) unsigned line, const char *function)
{ {
char *temp_fmt = strdup(sc_log_config->log_format);
char *temp_fmt_h = temp_fmt;
char *substr = temp_fmt;
char *temp = *msg; char *temp = *msg;
const char *s = NULL; const char *s = NULL;
@ -296,9 +293,12 @@ SCError SCLogMessage(SCLogLevel log_level, char **msg, const char *file,
/* no of characters_written(cw) by snprintf */ /* no of characters_written(cw) by snprintf */
int cw = 0; int cw = 0;
char *temp_fmt = strdup(sc_log_config->log_format);
char *temp_fmt_h = temp_fmt;
char *substr = temp_fmt;
if (temp_fmt == NULL) { if (temp_fmt == NULL) {
printf("Error allocating memory\n"); goto error;
exit(EXIT_FAILURE);
} }
if (sc_log_module_initialized != 1) { if (sc_log_module_initialized != 1) {
@ -501,7 +501,7 @@ SCLogOPBuffer *SCLogAllocLogOPBuffer(void)
SCLogOPIfaceCtx *op_iface_ctx = NULL; SCLogOPIfaceCtx *op_iface_ctx = NULL;
int i = 0; int i = 0;
if ( (buffer = SCMalloc(sc_log_config->op_ifaces_cnt * if ( (buffer = malloc(sc_log_config->op_ifaces_cnt *
sizeof(SCLogOPBuffer))) == NULL) { sizeof(SCLogOPBuffer))) == NULL) {
SCLogError(SC_ERR_FATAL, "Fatal error encountered in SCLogAllocLogOPBuffer. Exiting..."); SCLogError(SC_ERR_FATAL, "Fatal error encountered in SCLogAllocLogOPBuffer. Exiting...");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -529,7 +529,7 @@ static inline SCLogOPIfaceCtx *SCLogAllocLogOPIfaceCtx()
{ {
SCLogOPIfaceCtx *iface_ctx = NULL; SCLogOPIfaceCtx *iface_ctx = NULL;
if ( (iface_ctx = SCMalloc(sizeof(SCLogOPIfaceCtx))) == NULL) { if ( (iface_ctx = malloc(sizeof(SCLogOPIfaceCtx))) == NULL) {
SCLogError(SC_ERR_FATAL, "Fatal error encountered in SCLogallocLogOPIfaceCtx. Exiting..."); SCLogError(SC_ERR_FATAL, "Fatal error encountered in SCLogallocLogOPIfaceCtx. Exiting...");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -559,28 +559,43 @@ static inline SCLogOPIfaceCtx *SCLogInitFileOPIface(const char *file,
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
iface_ctx->iface = SC_LOG_OP_IFACE_FILE; if (file == NULL || log_format == NULL) {
goto error;
if (file != NULL &&
(iface_ctx->file = strdup(file)) == NULL) {
printf("Error allocating memory\n");
exit(EXIT_FAILURE);
} }
iface_ctx->iface = SC_LOG_OP_IFACE_FILE;
if ( (iface_ctx->file_d = fopen(file, "w+")) == NULL) { if ( (iface_ctx->file_d = fopen(file, "w+")) == NULL) {
printf("Error opening file %s\n", file); printf("Error opening file %s\n", file);
return NULL; goto error;
} }
if (log_format != NULL && if ((iface_ctx->file = strdup(file)) == NULL) {
(iface_ctx->log_format = strdup(log_format)) == NULL) { goto error;
printf("Error allocating memory\n"); }
exit(EXIT_FAILURE);
if ((iface_ctx->log_format = strdup(log_format)) == NULL) {
goto error;
} }
iface_ctx->log_level = log_level; iface_ctx->log_level = log_level;
return iface_ctx; return iface_ctx;
error:
if (iface_ctx->file != NULL) {
free((char *)iface_ctx->file);
iface_ctx->file = NULL;
}
if (iface_ctx->log_format != NULL) {
free((char *)iface_ctx->log_format);
iface_ctx->log_format = NULL;
}
if (iface_ctx->file_d != NULL) {
fclose(iface_ctx->file_d);
iface_ctx->file_d = NULL;
}
return NULL;
} }
/** /**
@ -924,7 +939,7 @@ SCLogInitData *SCLogAllocLogInitData(void)
{ {
SCLogInitData *sc_lid = NULL; SCLogInitData *sc_lid = NULL;
if ( (sc_lid = SCMalloc(sizeof(SCLogInitData))) == NULL) if ( (sc_lid = malloc(sizeof(SCLogInitData))) == NULL)
return NULL; return NULL;
memset(sc_lid, 0, sizeof(SCLogInitData)); memset(sc_lid, 0, sizeof(SCLogInitData));
@ -1074,7 +1089,7 @@ void SCLogInitLogModule(SCLogInitData *sc_lid)
#endif /* OS_WIN32 */ #endif /* OS_WIN32 */
/* sc_log_config is a global variable */ /* sc_log_config is a global variable */
if ( (sc_log_config = SCMalloc(sizeof(SCLogConfig))) == NULL) { if ( (sc_log_config = malloc(sizeof(SCLogConfig))) == NULL) {
SCLogError(SC_ERR_FATAL, "Fatal error encountered in SCLogInitLogModule. Exiting..."); SCLogError(SC_ERR_FATAL, "Fatal error encountered in SCLogInitLogModule. Exiting...");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -1197,7 +1212,8 @@ void SCLogLoadConfig(void)
SCLogInitLogModule(sc_lid); SCLogInitLogModule(sc_lid);
//exit(1); //exit(1);
/* \todo Can we free sc_lid now? */ /* \todo Can we free sc_lid now? */
if (sc_lid != NULL) SCFree(sc_lid); if (sc_lid != NULL)
free(sc_lid);
} }
/** /**
@ -1219,7 +1235,7 @@ void SCLogInitLogModuleIfEnvSet(void)
SCLogLevel log_level = SC_LOG_NOTSET; SCLogLevel log_level = SC_LOG_NOTSET;
/* sc_log_config is a global variable */ /* sc_log_config is a global variable */
if ( (sc_log_config = SCMalloc(sizeof(SCLogConfig))) == NULL) if ( (sc_log_config = malloc(sizeof(SCLogConfig))) == NULL)
return; return;
memset(sc_log_config, 0, sizeof(SCLogConfig)); memset(sc_log_config, 0, sizeof(SCLogConfig));
sc_lc = sc_log_config; sc_lc = sc_log_config;
@ -1345,7 +1361,7 @@ static char *SCLogGetLogFilename(char *filearg)
if (ConfGet("default-log-dir", &log_dir) != 1) if (ConfGet("default-log-dir", &log_dir) != 1)
log_dir = DEFAULT_LOG_DIR; log_dir = DEFAULT_LOG_DIR;
log_filename = SCMalloc(PATH_MAX); log_filename = malloc(PATH_MAX);
if (log_filename == NULL) if (log_filename == NULL)
return NULL; return NULL;
snprintf(log_filename, PATH_MAX, "%s/%s", log_dir, filearg); snprintf(log_filename, PATH_MAX, "%s/%s", log_dir, filearg);

@ -127,6 +127,7 @@
#else /* DBG_MEM_ALLOC */ #else /* DBG_MEM_ALLOC */
#if 0
#define SCMalloc(a) ({ \ #define SCMalloc(a) ({ \
void *ptrmem = malloc(a); \ void *ptrmem = malloc(a); \
if (ptrmem == NULL) { \ if (ptrmem == NULL) { \
@ -162,6 +163,11 @@
} \ } \
(void*)ptrmem; \ (void*)ptrmem; \
}) })
#endif
#define SCMalloc malloc
#define SCRealloc realloc
#define SCCalloc calloc
#define SCStrdup strdup
#define SCFree(a) free((a)) #define SCFree(a) free((a))

Loading…
Cancel
Save