Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Simple CGI (written in VC++) does not work. Why?

Posted on 1998-11-22
3
Medium Priority
?
234 Views
Last Modified: 2013-12-25
I am writing a simple CGI script to print the variables passed through the form. My form has only on submit button and i use POST method while executing the script. The script is simple:
#include <iostream.h>
#include <string.h>

void main(){
cout << "Content-type:TEXT/HTML\n\n";
char* streamSize = getenv("CONTENT_LENGTH");
long dataSize = atol(streamSize); // convert size from char*
char* buffer = new char[dataSize]; // get some room
cin >> buffer;
cout << buffer; // just print variables

delete [] buffer; //let's delete buffer

cout << "this is it";

}
Simple. is not it? Even more it works as a CGI script on the web (IIS 3.0) only when i commented the
delete [] buffer; line. Otherwise it does not.
Is there a specific reason why?
 
0
Comment
Question by:misha051797
  • 2
3 Comments
 
LVL 8

Expert Comment

by:MaDdUCK
ID: 1829733
It is not wise to use cin like this for POST input. Use fread() instead. Here you can specify the maximum number of characters to be read. Your method will work, but this method is cleaner.

here is the code that I use:

==
char *data = new char[len + 1];
      
int new_len = fread(data, 1, len, stdin);
data[new_len]='\0';
std::string result = data;

delete[] data;

return result;
==

it may not be as pretty, but it works fine and cannot ever overflow or seg fault.
also, change your new char line to new char[dataSize + 1] ! (you need the \0 character.

Can you run the prog from the command line? simply set the dataSize to say 10, and then enter a 10 byte string using the keyboard. See what happens.

keep me posted!

0
 

Author Comment

by:misha051797
ID: 1829734
Thank you. Great advise...
0
 
LVL 8

Accepted Solution

by:
MaDdUCK earned 100 total points
ID: 1829735
does this mean it worked?
if not, then please reject my answer again. Otherwise I will also be willing to walk you through the process...
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question