Solved

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

Posted on 1998-11-22
3
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 50 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

752 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