Solved

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

Posted on 1998-11-22
3
226 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

RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

Question has a verified solution.

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

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

624 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