问题代码如下:
#define MAX_LINE_LEN (10240*2)
char tieziLine[MAX_LINE_LEN];
wchar_t oneTieziLine_wchar[MAX_LINE_LEN];
mbstowcs(oneTieziLine_wchar,tieziLine,sizeof(oneTieziLine_wchar));
经过一番google之后,找到了答案:
http://www.cplusplus.com/reference/cstdlib/mbstowcs/
http://pentest.cryptocity.net/files/code_analysis/code_audits_2_fall2010.pdf
mbstowcs() takes a count of wide characters to write, not a size in bytes of the destination buffer
mbstowcs(oneTieziLine_wchar,tieziLine, sizeof(oneSample_wchar)); //这行代码有问题,我测试了一下,我机器上sizeof(wchar_t)为4。显然sizeof(oneTieziLine_wchar)大于tieziLine分配的大小。
为了验证结论,我找到了mbstowcs的实现:
同时gdb验证一下: