简体   繁体   中英

Why does my function gives me “Stack around the variable 'url' was corrupted.” error?

I have a function which should download me a website from a string given in argument (exacly string is an end of url). It's throwing me failure

Stack around the variable 'url' was corrupted.

My code:

void download_wordnik(string word) {
        string s1 = word;
        std::wstring w_word_Tmp1(s1.begin(), s1.end());
        wstring w_word1 = w_word_Tmp1;      
        std::wstring stemp1 = std::wstring(s1.begin(), s1.end());
        LPCWSTR sw1 = stemp1.c_str();

        TCHAR url[] = TEXT("https://www.wordnik.com/words");
        wsprintf(url, TEXT("%s\/%s\/"), url, sw1);

        LPCWSTR sw2 = stemp1.c_str();
        TCHAR path[MAX_PATH];
        GetCurrentDirectory(MAX_PATH, path);
        wsprintf(path, TEXT("%s\\wordnik\\%s\.txt"), path, sw2);
        HRESULT res = URLDownloadToFile(NULL, url, path, 0, NULL);


        // Checking download
        if(res == S_OK) {
            printf("Ok\n");
        } else if(res == E_OUTOFMEMORY) {
            printf("Buffer length invalid, or insufficient memory\n");
        } else if(res == INET_E_DOWNLOAD_FAILURE) {
            printf("URL is invalid\n");
        } else {
            printf("Other error: %d\n", res);
        }

}

I'm using this includes

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <Urlmon.h>
#include <regex>

#pragma comment(lib, "urlmon.lib")
using namespace std;

Probably because you're copying on url more than its capacity which leads to undefined behaviour:

TCHAR url[] = TEXT("https://www.wordnik.com/words");// The size is `30` but ...

wsprintf(url, TEXT("%s\/%s\/"), url, sw1);// It copies more than `30` characters.

Use std::wstring ways and don't mess with xprintf methods and fixed sized arrays. I'm not familiar with TCHAR or TEXT (Windows things), but you can do something like this:

std::wstring url;

url = std::wstring(TEXT("https://www.wordnik.com/words/")) + sw1 + TEXT("/");

You're writing outside the bounds of url when you wsprintf into it.

Instead do (for general formatting)

std::wostringstream urlstream;
urlstream << TEXT("https://www.wordnik.com/words/") << sw1 << TEXT("/"); 
std::wstring url = urlstream.str();

or (simpler)

std::wstring url = std::wstring(TEXT("https://www.wordnik.com/words/")) + sw1 + TEXT("/");

You're copying variables around quite a lot - as far as I can tell, you can cut your code down to this:

    std::wstring w_word(word.begin(), word.end());
    std::wstring url = std::wstring(TEXT("https://www.wordnik.com/words/")) + w_word + TEXT("/");
    TCHAR currentpath[MAX_PATH];
    GetCurrentDirectory(MAX_PATH, currentpath);
    std::wstring path = std::wstring(currentpath) + TEXT("\\wordnik\\") + w_word + TEXT(".txt");
    HRESULT res = URLDownloadToFile(NULL, url.c_str(), path.c_str(), 0, NULL);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM