A
A
armadillo-cld2020-07-26 04:57:13
C++ / C#
armadillo-cld, 2020-07-26 04:57:13

InternetReadFile adds heresy to the end of the line. What to do?

Hello.
There is a built-in Requests class.
I use the GetRequest function in it.
Actually, here she is:

string GetRequest(string url, string path = "", string ContentType = "") {
    InternetSetOption(0, 42, NULL, 0);

    HINTERNET hSocket = InternetOpen(TEXT("FORMATC"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, NULL);
    if (hSocket != NULL) {
      HINTERNET hConnection = InternetConnect(hSocket, TEXT(url.c_str()), INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
      if (hConnection != NULL) {
        HINTERNET hRequest;
        if (path == "") {
          hRequest = HttpOpenRequest(hConnection, TEXT("GET"), NULL, NULL, NULL, 0, INTERNET_FLAG_RELOAD | INTERNET_FLAG_SECURE, 1);
        }
        else {
          hRequest = HttpOpenRequest(hConnection, TEXT("GET"), TEXT(path.c_str()), NULL, NULL, 0, INTERNET_FLAG_RELOAD | INTERNET_FLAG_SECURE, 1);
        }

        if (hRequest != NULL) {
          if (ContentType == "") {
            HttpSendRequest(hRequest, NULL, 0, NULL, 0);
          }
          else {
            HttpSendRequest(hRequest, ContentType.c_str(), ContentType.length(), NULL, NULL);
          }
          char strContent[1024];
          DWORD dwByteRead;
          InternetReadFile(hRequest, strContent, sizeof(strContent) - 1, &dwByteRead);

          return string(strContent);
        }
        InternetCloseHandle(hRequest);
      }
      InternetCloseHandle(hConnection);
    }
    InternetCloseHandle(hSocket);
    return "";
  }

After some time, some characters began to be added to the end of the line.
The first 10-15 times everything worked fine, output like this:
{"response":"ok"}
And I parsed json, and got the response value.
But then, through debugging, I noticed that it gives out not such a line {"response":"ok"}, but this one: {"response":"ok"}(*@D. Well, there are not such symbols, but I think you understand. In general, because of these characters, nlohmann::json crashes, and I cannot parse normally. Help what to do?

Updated. Here are two screenshots of the request result: https://imgur.com/a/2d1E3nm

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
armadillo-cld, 2020-07-26
@armadillo-cld

Ahem-ahem...
I should probably go to bed, it's 5 o'clock in the morning, after all.
The solution came immediately when I looked at the array.
It was declared like this:
strContent[1024];
In fact, there is nothing there, so it was enough to fill it with zeros:
strContent[1024] = {0};
And it all worked.
Do not regard it as spam, I was just inattentive, but I'll leave the decision, all of a sudden someone will also have this.

N
none7, 2020-07-26
@none7

Wrong You have chosen a solution to the problem.

InternetReadFile(hRequest, strContent, sizeof(strContent)/* - 1*/, &dwByteRead);
return string(strContent, dwByteRead);

InternetReadFile is not a string function, it does not add zeros to the end of the array, and moreover, it can add them anywhere in the array if a binary file is being read. By the way, there is such a class of vulnerabilities, when a string is either an array of characters like std::string, or null-terminated. Checks on one mean absolutely nothing to the other, since std::string can store a null character!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question