I'm guessing the problem is either in WriteFileName or fwriting to a closed stream due to improper verification if fopen() fails.
The following code works under Linux
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define NSE_MAX_PACKET_RECV_SIZE 1050
int
main(int argc, char *argv[])
{
FILE *fpWriteFile;
char *recvgen;
char WriteFileName[1024];
if (argc != 2)
exit(1);
strncpy(WriteFileName, argv[1], 1024);
if (strlen(argv[1]) >= 1024)
WriteFileName[1023] = '\0';
fpWriteFile = fopen(WriteFileName,"wb+");
if (fpWriteFile == NULL)
exit(1);
recvgen = (char *)malloc(sizeof(char)*NSE_MAX_PACKET_RECV_SIZE);
if (recvgen == NULL)
exit(1);
fwrite (recvgen,NSE_MAX_PACKET_RECV_SIZE,1,fpWriteFile);
exit (0);
}
Code:
cc -o file file.c
./file `perl -e 'print "A" x 10'` && echo $? -> OK: 0
./file `perl -e 'print "A" x 1024'` ; echo $? -> NOT OK (failing with filename too long): 1
You won't have a segfault now, please try to just debug it with printf()s and find out the problem.