简体   繁体   English

为什么我的函数给我“变量'url'周围的堆栈已损坏。”错误?

[英]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). 我有一个功能,可以从参数中给出的字符串中下载我的网站(完全字符串是url的结尾)。 It's throwing me failure 这让我失败了

Stack around the variable 'url' was corrupted. 变量“ url”周围的堆栈已损坏。

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: 可能是因为您在url复制的内容多于其容量,这导致未定义的行为:

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. 使用std::wstring方法,不要与xprintf方法和固定大小的数组xprintf I'm not familiar with TCHAR or TEXT (Windows things), but you can do something like this: 我不熟悉TCHARTEXT (Windows事物),但是您可以执行以下操作:

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. wsprintf进入url范围之外时,您正在写。

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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C++ INPUT class 错误(变量周围的堆栈已损坏。) - C++ error with INPUT class (Stack around the variable was corrupted.) 挂钩函数时出错,“变量x周围的堆栈已损坏。”? C ++ - Error when hooking a function, “Stack around the variable x was corrupted.”? C++ 围绕变量&#39;&#39;的C ++堆栈已损坏。 (D3D9) - C++ Stack around the variable '' was corrupted. (D3D9) “围绕变量 'b' 的堆栈已损坏。” (没有堆分配) - “Stack around the variable 'b' was corrupted.” (No heap allocations) 变量周围的堆栈损坏错误 - Stack around variable is corrupted error c ++“运行时检查失败#2 - 变量&#39;对&#39;周围的堆栈已损坏。” - c++ “Run-Time Check Failure #2 - Stack around the variable 'pair' was corrupted.” 运行时检查失败 #2 - 变量“e”周围的堆栈已损坏。 发生了 - Run-Time Check Failure #2 - Stack around the variable 'e' was corrupted. occurred 运行时检查失败#2-变量&#39;db&#39;周围的堆栈已损坏。 c ++(帮助) - Run-Time Check Failure #2 - Stack around the variable 'db' was corrupted. c++ (Help) 运行时检查失败 #2 - 变量“sortObject”周围的堆栈已损坏。 怎么修? - Run-Time Check Failure #2 - Stack around the variable 'sortObject' was corrupted. how to fix? 运行时检查失败 #2 - 变量“c2d”周围的堆栈已损坏。 与 c++ - Run-Time Check Failure #2 - Stack around the variable 'c2d' was corrupted. with c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM