【问题标题】:Getting error from sscanf when trying to read string using CGI尝试使用 CGI 读取字符串时从 sscanf 获取错误
【发布时间】:2021-12-04 15:49:51
【问题描述】:

当我试图读取一个字符串时,我收到一个错误表单 sscanf。获取字符串的某个部分。我从 HTML 文件中获取字符串,并使用 CGI 作为服务器端来读取表单。

<!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>

当您提交此表单时,它会带您进入服务器端脚本,该脚本基本上会告诉用户他们输入的名称以及使用 CGI 挑选的动物。

但我在尝试使用sscanf从数据中读取和拼接字符串时遇到问题

使用 C++ 的 CGI 代码

#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; 
}

对此我们将不胜感激。我不知道我做错了什么。下面我将展示输出应该是什么样子。

输出:

name="bob"

animal="lion"

【问题讨论】:

  • nameanimal 是未初始化的指针。将它们传递给 sscanf 是未定义的行为。而是对char name[256] = {0}; 执行类似的操作,对animal 执行相同操作。 sscanf 周围的 if 什么都不做,因为您没有根据条件采取行动。您还应该在使用 getenv 之前检查它是否返回了一个有效的指针。
  • @Peter 我注意到你接受了答案,然后又不接受,所以我想有什么问题吗?请询问是否有任何不清楚或由于某种原因无法正常工作。

标签: c++ cgi


【解决方案1】:

nameanimal 不指向您分配的任何内存。一个简单的解决方法是将它们变成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";
}

请注意 %[^&amp;] 而不是 %s 进行扫描。它会扫描直到但不包括&amp;

Demo

【讨论】:

    猜你喜欢
    • 2016-08-14
    • 2012-06-06
    • 2023-03-10
    • 1970-01-01
    • 2012-11-10
    • 1970-01-01
    • 2019-04-10
    • 2022-06-10
    相关资源
    最近更新 更多