C program crash... I'm stumped.

This is a discussion on C program crash... I'm stumped. within the C Programming forums, part of the General Programming Boards category; On my Mac, I have coded up a C cgi program. It works fine in Xcode, and from the Terminal, ...

  1. #1
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309

    C program crash... I'm stumped.

    On my Mac, I have coded up a C cgi program. It works fine in Xcode, and from the Terminal, and when accessed by a html form under Safari and FireFox on the Mac. However, when I access the form on my home network, from my Windows XP machine running IE 6, it crashes when I click "submit" on my form.

    Now, here's the tricky part: when it crashes, and IE displays the "Internal Server Error" page, if I click IE's REFRESH button, and click the RETRY popup to confirm resending the form data, the cgi program runs perfect and I get the output! This is blowing my mind!!

    Here's the crash log:
    Code:
    **********
    
    Host Name:      todd-burchs-computer
    Date/Time:      2008-03-16 08:12:04.574 -0500
    OS Version:     10.4.11 (Build 8S2167)
    Report Version: 4
    
    Command: getmethod
    Path:    /Library/WebServer/CGI-Executables/getmethod
    Parent:  httpd [457]
    
    Version: ??? (???)
    
    PID:    3779
    Thread: 0
    
    Exception:  EXC_BAD_ACCESS (0x0001)
    Codes:      KERN_INVALID_ADDRESS (0x0001) at 0x7da5c33c
    
    Thread 0 Crashed:
    0   getmethod   0x00008bba main + 688 (main.c:58)
    1   getmethod   0x00001e9a start + 258
    2   getmethod   0x00001dc1 start + 41
    
    Thread 0 crashed with X86 Thread State (32-bit):
      eax: 0x6f697462  ebx: 0x00008921  ecx: 0x00000000  edx: 0x00000000
      edi: 0xbffff229  esi: 0x00008c91  ebp: 0xbffff358  esp: 0xbffff180
       ss: 0x0000001f  efl: 0x00010202  eip: 0x00008bba   cs: 0x00000017
       ds: 0x0000001f   es: 0x0000001f   fs: 0x00000000   gs: 0x00000037
    
    Binary Images Description:
        0x1000 -     0x4fff getmethod       /Library/WebServer/CGI-Executables/getmethod
        0x8000 -     0x8fff getmethod       /Library/WebServer/CGI-Executables/getmethod
    0x8fe00000 - 0x8fe4afff dyld 46.16      /usr/lib/dyld
    0x90000000 - 0x90171fff libSystem.B.dylib       /usr/lib/libSystem.B.dylib
    0x901c1000 - 0x901c3fff libmathCommon.A.dylib   /usr/lib/system/libmathCommon.A.dylib
    0x90bd2000 - 0x90bd9fff libgcc_s.1.dylib        /usr/lib/libgcc_s.1.dylib
    0x963fa000 - 0x963fbfff com.apple.zerolink 1.2 (3)      /System/Library/PrivateFrameworks/ZeroLink.framework/Versions/A/ZeroLink
    And here's the C program. I'm assuming line 58 is the line where's crashing, so I've highlighted line 58 in red.

    Code:
    #include <stdio.h>
    #include <string.h> 
    #include <stdlib.h> 
    
    
    //int main (int argc, const char * argv[]) {
    int main (int argc, char * const argv[], char * envp[] ) {
    
    	int i ; 
    	char * position , * envptr ;  
    	char buffer[256] ; 
    	char * ctype ; 
    	int clength = 0 ; 
    	char const * names[] = { 
    		"CONTENT_LENGTH" , 
    		"CONTENT_TYPE" , 
    		"DOCUMENT_ROOT" , 
    		"HTTP_ACCEPT" , 
    		"HTTP_ACCEPT_ENCODING" , 
    		"HTTP_ACCEPT_LANGUAGE" , 
    		"HTTP_CONNECTION" , 
    		"HTTP_HOST" , 
    		"HTTP_REFERER" , 
    		"HTTP_USER_AGENT" , 
    		"PATH" , 
    		"REMOTE_ADDR" ,
    		"REMOTE_PORT" , 
    		"SCRIPT_FILENAME" , 
    		"SCRIPT_URI" , 
    		"SCRIPT_URL", 
    		"SERVER_ADDR" , 
    		"SERVER_ADMIN" , 
    		"SERVER_NAME" , 
    		"SERVER_PORT" , 
    		"SERVER_SIGNATURE" , 
    		"SERVER_SOFTWARE" , 
    		"GATEWAY_INTERFACE" , 
    		"SERVER_PROTOCOL" , 
    		"REQUEST_METHOD" , 
    		"QUERY_STRING" , 	
    		"REQUEST_URI" , 
    		"SCRIPT_NAME" , 
    		NULL
    	} ; 
    	
    	printf("Content-Type: text/html\n\n") ; 
    
    	printf("<html>\n") ; 
    	printf("<head><title>From A C Program</title>\n") ; 
    	printf("<style type='text/css'>\n") ; 
    	printf(".yellow { background-color: yellow ; border: 1px solid black ;}\n") ; 
    	printf("</style>\n") ; 
    	printf("</head>\n") ; 
    	printf("<body><p>Hi there</p>\n") ; 
    	printf("<table class='yellow'>\n") ; 
    
      	i = 0 ; 
    	while( names[i] != NULL ) { // this is line 58 
    
    		printf("<tr><td>\n") ; 
    		printf("\t%-30s\n", names[i] ) ; 
    		printf("</td><td>\n") ; 
    
    		strcpy(buffer,names[i]) ;
    		strcat(buffer,"=") ; 
    
    		envptr = getenv(names[i]) ; 
    		if (envptr) strcat(buffer, envptr) ;
    		else strcat(buffer, "(null)") ; 
    
    		position = strchr(buffer, '=') ;   
    
    		buffer[position-buffer] = 0 ;   // null term the environment variable name 
    		printf("\t%s\n", position+1) ; 
    		
    		if (strcmp("CONTENT_TYPE",buffer)==0) { 
    			ctype = position+1 ; 
    		} 
    		
    		if (strcmp("CONTENT_LENGTH",buffer)==0) { 
    			clength = atoi(position+1) ; 
    		} 
    		printf("</td></tr>\n") ; 
    		
    		i++ ; 
    	} 
    
    	printf("</table>\n");
    	printf("<table frame='box' rules='all' cellpadding='5' >\n<tr><td>data... bytes = %d</td>\n<td>", clength );
    
    	for ( i = 0 ; i < clength ; ++i ) { 
    		char c = fgetc(stdin) ; 
    		if (c=='\n') printf("<br />") ; 
    		else printf( "%c", c ) ; 
    	}
    
    	printf("</td>\n</tr>\n") ; 
    	printf("</table>\n</body>\n");
    	printf("</html>\n");
    	fflush(stdout) ; 
        return 0;
    }
    Any ideas? If you want me to post the html form, I can do that.

    Thanks, Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Stack around buffer was corrupted.
    EDIT: And the offending line is
    Code:
    if (envptr) strcat_s(buffer, sizeof(buffer), envptr) ;
    On loop i = 10.
    This is what I've figured out after 1 minute of debugging,
    Ph33r Visual Studio's debugging facilities!

    In other words, the environment variables are causing a buffer overrun. And since they may be different for different machines... Well, there's your problem.

    I get:
    Size of envptr: 281
    Current size of buffer: 5
    Space left: -30

    Alright. Problem solved. Moving on
    Last edited by Elysia; 03-16-2008 at 07:58 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    That is EXACTLY it!!

    Thanks Elysia! (1 minute - you smarty pants!! I've been working on this all friggin' weekend.!)

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Quote Originally Posted by Todd Burch View Post
    That is EXACTLY it!!

    Thanks Elysia! (1 minute - you smarty pants!! I've been working on this all friggin' weekend.!)

    Todd
    You must know your tools well
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    With Ph33r (whatever that is, a debugger of some sort I presume), were you able to step through the code as the program was running in actual "cgi" mode?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Ph33r means "fear"
    It's a C program, so I used Visual Studio.
    It's a program. And it runs on a server. So...
    Last edited by Elysia; 03-16-2008 at 08:24 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    OK. I'm not familiar with strcat_s ("s" for "safe"?) Is this a non-standard MS thing? Seems to be the thing to use.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    It's a secure version of the standard library. I don't know if any other compilers support it, but if you use Visual Studio, I would recommend using them 100&#37;. They are easy type get rid of for portability with some wrapper or define anyway.
    They stand for "secure."
    They check for overflows for one thing. Handy stuff.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char vs int - program crash!!
    By Goldrak in forum C++ Programming
    Replies: 4
    Last Post: 04-07-2006, 08:17 PM
  2. My program causes my compiler to crash
    By carolsue2 in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2006, 04:06 AM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  4. sprintf() giving crash to program!!
    By maven in forum C Programming
    Replies: 4
    Last Post: 01-01-2006, 11:26 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21