Skip to content

Commit

Permalink
Fix stack overflow for error msg with path
Browse files Browse the repository at this point in the history
  • Loading branch information
bukka committed Dec 9, 2013
1 parent 4a0878a commit 588c1a7
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/fann_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <limits.h>

#include "config.h"
#include "fann.h"
Expand All @@ -30,6 +31,19 @@
#define snprintf _snprintf
#endif

/* define path max if not defined */
#if defined(_WIN32) && !defined(__MINGW32__)
#define PATH_MAX _MAX_PATH
#endif
#ifndef PATH_MAX
#ifdef _POSIX_PATH_MAX
#define PATH_MAX _POSIX_PATH_MAX
#else
#define PATH_MAX 4096
#endif
#endif


FILE * fann_default_error_log = (FILE *)-1;

/* resets the last error number
Expand Down Expand Up @@ -94,6 +108,7 @@ void fann_error(struct fann_error *errdat, const enum fann_errno_enum errno_f, .
{
va_list ap;
char *errstr;
int errfree = 0;
FILE * error_log = fann_default_error_log;

if(errdat != NULL)
Expand All @@ -105,12 +120,13 @@ void fann_error(struct fann_error *errdat, const enum fann_errno_enum errno_f, .
}
else
{
errstr = (char *) malloc(FANN_ERRSTR_MAX);
errstr = (char *) malloc(FANN_ERRSTR_MAX + PATH_MAX);
if(errstr == NULL)
{
fprintf(stderr, "Unable to allocate memory.\n");
return;
}
errfree = 1;
}

va_start(ap, errno_f);
Expand Down Expand Up @@ -188,7 +204,11 @@ void fann_error(struct fann_error *errdat, const enum fann_errno_enum errno_f, .

if(errdat != NULL)
{
errdat->errstr = errstr;
if (errdat->errstr == NULL)
{
errdat->errstr = errstr;
errfree = 0;
}
error_log = errdat->error_log;
}

Expand All @@ -200,6 +220,10 @@ void fann_error(struct fann_error *errdat, const enum fann_errno_enum errno_f, .
{
fprintf(error_log, "FANN Error %d: %s", errno_f, errstr);
}
if (errfree)
{
free(errstr);
}
}

/* INTERNAL FUNCTION
Expand Down

0 comments on commit 588c1a7

Please sign in to comment.