The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #7 (permalink)  
Old 06-19-2009
redoubtable redoubtable is offline
Registered User
  
 

Join Date: Aug 2008
Location: Portugal
Posts: 242
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.