LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-17-2007, 01:47 PM   #1
estratos
Member
 
Registered: Jan 2006
Distribution: Ubuntu
Posts: 85

Rep: Reputation: 15
Memory problems on embedded CGI


Hi all.

I'm working on an embedded application for OpenSlug (Linksys NSLU2). I'm currently porting some CGI's that already worked for an ARM7 platform but something is wrong.

These cgi's are basically parsing the string POSTED from a precedent cgi and copying the results into different local buffers:

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define STRING_KEY		1
#define STRING_VALUE		2

//*********************************************************************************************
// Main
// main function of the cgi
//*********************************************************************************************

int main(void)
{
  char *strLen;
  long intTotLen, intReadLen = 0;		
  char chr;
  char strKey[20];
  char strValue[120];
  short int flgStrType = STRING_KEY;
  int i = 0;
	
  // html header
  printf("<html>\n");

  printf("<head></head>\n");
	
  // Get the legth of the packet sent by the POST method
  strLen = getenv("CONTENT_LENGTH");
  intTotLen = atoi(strLen);
	
  // Only if received anything
  if (intTotLen > 0)
  {
    // While any character to read from the POST string
    while (intReadLen < intTotLen)
    {
      chr = fgetc(stdin);  // Capture the following character
      intReadLen++;	   // Increment the number of characters read
      printf("%c",chr);    // print character
      // Evaluate the captured character
      switch(chr)
      {
	case '&':
	  strValue[i] = 0;  // Terminate string
	  flgStrType = STRING_KEY; // Ready to find a new value
	  i = 0;	// Go for a new string
	  break;
	case '=':
 	  strKey[i] = 0;  // Terminate string
	  flgStrType = STRING_VALUE;  // Ready to find a new value
	  i = 0;	// Go for a new string
	  break;
	default:
	  if (flgStrType == STRING_KEY)
	    strKey[i] = chr; // Store the character in the keyword buffer
	  else
	    strValue[i] = chr;	// Store the character in the value buffer
  	  break;
      }
    }
    strValue[i]= 0; 	// Terminate the last value string
    printf("<br>strKey = %s strValue = %s<br>", strKey, strValue);
  }
		
  // End of the html body
  printf("</body>\n");

  printf("</html>\n");

  return(0);
}
This cgi outputs the following result:
action=Create+event
strKey = action strValue = wÿŽ@Create

However, if I declare strvalue as static (static char strValue[100]) then the problem gets solved. I've encountered similar problems on other embedded platforms and most of the times this is due to memory misalignments but NSLU2's processor has a MMU and the global arrays have been manually aligned as well.

Please, I'm open to any kind of technical suggestions.

thank you very much,

Daniel.
 
Old 08-18-2007, 11:30 PM   #2
JoeyAdams
Member
 
Registered: Jun 2006
Distribution: Kubuntu Hardy
Posts: 94

Rep: Reputation: 15
That's weird. I have a hard time believing the stack space would be limited so severely. Perhaps the CGI script is being called by the server program without a new stack being created for it, and the server program is hogging lots of memory. Another possibility is that the compiler generated incorrect code for the particular flavor of your target (maybe the target processor doesn't have an immediate subtract operation that can do numbers like 120). Maybe you should initialize each of your buffers like so:

memset(strKey,'?',sizeof(strKey)-1);
strKey[sizeof(strKey)-1]=0;
memset(strValue,'?',sizeof(strValue)-1);
strValue[sizeof(strValue)-1]=0;

This might let you see if strValue is being set to 8 characters instead of 120 or something like that. If you get:

action=Create+event
strKey = action strValue = ????????Create

Then there is a possibility that strValue is not being given a correct size.

Also, compile with debugging mode on so you can objdump -S the object (it lets you look at the lines of C and the assembly produced together).

If you just want a quick fix, you can do static char strKey[20] and static char strValue[120] and those might work better. Making them static allocates the buffers in BSS instead of stack (BSS shouldn't have a hard size limit, but stacks usually do). You might also be able to use malloc, which allocates the buffer in RAM without a hard size cap, either. Just be sure to free what you malloc! The target OS might not be able to free buffers automatically at the end like a fullscale OS would.
 
Old 08-19-2007, 03:58 AM   #3
estratos
Member
 
Registered: Jan 2006
Distribution: Ubuntu
Posts: 85

Original Poster
Rep: Reputation: 15
Thanks Joey for your help.

Initializing the local variables as you suggest gives me:

epindex=12&action=Edit
strKey = action strValue = ????

This confuses me even more because the length of strValue is the same than "Edit" so that strValue[4] = 0 did work.

Declaring strValue as static solves the problem again...

Daniel.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Out Of Memory problems. My server is down. Memory logs attached. guarriman Linux - General 7 06-10-2008 07:46 AM
Apache CGI problems cheycomm Linux - Software 6 03-12-2006 09:58 PM
Gpio Twiddling In Embedded Linux? or How to access PCI memory space regs? jds-linux Programming 1 07-11-2004 02:17 AM
problems with kile and embedded file viewers davec Linux - Newbie 0 04-22-2004 05:55 PM
Problems CGI apache Joshsawyer77 Linux - Software 5 01-23-2003 08:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration