Peter Holmes
Peter Holmes

Reputation: 11

Getting error from sscanf when trying to read string using CGI

I am getting an error form sscanf when I am trying to read a string. Get a certain part of the string. I am getting the string from an HTML file and I am using CGI as the server side to read from the form.

<!DOCTYPE html>
<html>
<head>
    <title>CGI-zoo</title>
    <style>
        body {
            text-align: center;
        }

        #nameErrorMsg {
            color: red;
        }

        #title {
            color: orange;
        }

        #animalDiv {
            border-style: groove;
        }
    </style>

</head>

<body>
    <script>


        //global variable for name
        var name;
        //function      :check()
        //Parameters    : void
        //Returns       : void
        //Description   : This function will make sure that the user enters something for the name and does not leave the text box blank
        function checkName()
        {
        name= name=document.getElementById("nameEntered").value;

        if(name==""||name==undefined)
        {
        //error msg
        document.getElementById("nameErrorMsg").innerHTML="This text box can not be blank";

        return false;
        }
        else {
        //hide the name text box and button
        document.getElementById("nameDiv").hidden=true;


        //unhides the drop list
        document.getElementById("askingUserWhatAnimal").innerHTML="Hello "+name+ ", Please choose one of the six animals to learn some facts from";
        document.getElementById("animalDiv").hidden=false;

        //unhide the submit form button
        document.getElementById("submitForm").hidden=false;
        return true;
        }
        }

    </script>
    <form id="cgiZoo" action="animal.exe" method="get">
        <h1 id="title"> cgi-Zoo</h1>
        <hr>

        <div id="nameDiv">
            <p id="nameErrorMsg"></p>

            <label id="nameLabel">Name:</label>
            <input type="text" id="nameEntered" placeholder="Name" name="nameEntered" v>
            <button type="button" onclick="checkName()" id="nameCheckBtn">Submit </button>
        </div>

        <!---Animal drop down list -->

        <div hidden id="animalDiv">
            <p id="askingUserWhatAnimal"></p>

            <label>Animals</label>

            <select name="animalList" id="animalList" required="required">
                <option></option>
                <option value="Lion">Lion</option>
                <option value="Elephant">Elephant</option>
                <option value="Giraffe">Giraffe</option>
                <option value="Moneky">Moneky</option>
                <option value="Zebra">Zebra</option>
                <option value="Panda">Panda</option>
            </select>
            <br>
            <br>
        </div>
        <br>
        <input hidden type="submit" id="submitForm" form="cgiZoo" name="submit">
    </form>
</body>
</html>

When you submit this form it is suppose to take you to a server side script that basically tells the use what name they entered and what animal they picked using CGI.

But I am having an issue trying to read and splice the string from the data using sscanf

CGI code using C++

#include <iostream>
#include <string.h>

#pragma warning(disable: 4996)
#define CGI_VARS        30
#define SEND_METHOD     19      // REQUEST_METHOD tells us if the form was sent using GET or POST
#define DATA_LENGTH     14  

using namespace std;

int main()
{
    cout << "Content-type:text/html\r\n\r\n";
    cout << "<html>\n";
    cout << "<head>\n";
    cout << "<title>CGI Environment Variables</title>\n";
    cout << "</head>\n";
    cout << "<body>\n";
    
    char* data;
    
    char* name;
    char *animal; 

    //returns pointer
    data = getenv("QUERY_STRING");
    
    if (sscanf(data, "nameEntered=%s&animalName=%s", name, animal));

    cout << name;
    cout << animal;

    cout << "</body>";
    
    return 0; 
}

Much help would be appreciated with this. I don't know what I am doing wrong. Below this I will show what the output should look like.

Output:

name="bob"

animal="lion"

Upvotes: 1

Views: 120

Answers (1)

Ted Lyngmo
Ted Lyngmo

Reputation: 117433

name and animal doesn't point at any memory that you've allocated. One simple fix could be to make them into char[]s:

char name[256];
char animal[256];
char* data = getenv("QUERY_STRING");

// check that data is non-null and check the sscanf result:
if(data && sscanf(data, "nameEntered=%255[^&]&animalName=%255[^&]", name, animal) == 2) 
{   //                                ^^^                 ^^^
    //                      limit the scan to the size of the char[] - 1
    std::cout << "name=\"" << name << "\"\n\nanimal=\"" << animal << "\"\n";
}

Note %[^&] instead of %s for the scans. It scans up until but not including &.

Demo

Upvotes: 2

Related Questions