wcstombs 分段错误

问题描述 投票:0回答:2

奇怪的是,这段代码编译时没有警告:

int
main (void)
{
  int i;  
  char pmbbuf[4]; 

  wchar_t *pwchello = L"1234567890123456789012345678901234567890";

  i = wcstombs (pmbbuf, pwchello, wcslen(pwchello)* MB_CUR_MAX + 1);

  printf("%d\n", MB_CUR_MAX);
  printf ("   Characters converted: %u\n", i);
  printf ("   Multibyte character: %s\n\n", pmbbuf);

  return 0;
}

但是当我运行 ./a.out 时,它打印出:

1
转换字符数:40
多字节字符:1234(

分段错误

关于导致分段错误的原因有什么想法吗?

c segmentation-fault
2个回答
3
投票

您会遇到缓冲区溢出,因为您在转换后没有以空值终止缓冲区,并且缓冲区大小也不足以保存结果。

您可以动态分配内存,因为您事先不知道需要多少内存:

int i;
char pmbbuf*;
wchar_t *pwchello = L"1234567890123456789012345678901234567890";
// this will not write anything, but return the number of bytes in the result
i = wcstombs (0, pwchello, wcslen(pwchello)* MB_CUR_MAX + 1);
//allocate memory - +1 byte for the trailing null, checking for null pointer returned omitted (though needed)
pmbbuf = malloc( i + 1 );
i = wcstombs (pmbbuf, pwchello, wcslen(pwchello)* MB_CUR_MAX + 1);
//put the trailing null
pmbbuf[i] = 0;
//whatever you want to do with the string - print, e-mail, fax, etc.
// don't forget to free memory
free( pmbbuf );
//defensive - to avoid misuse of the pointer 
pmbbuf = 0;

2
投票

您正在尝试将一个肯定超过 4 个字符的字符串放入可容纳 4 个字符的字符数组中。由于您没有指定“4”作为最大大小,转换将写入它不拥有的内存或可能被其他变量使用的内存,内部保存数据(例如堆栈上的函数返回值或类似数据)。这将导致段错误,因为您在调用

wcstombs
之前覆盖了推送到堆栈上的数据(堆栈自上而下增长)。

© www.soinside.com 2019 - 2024. All rights reserved.