wcscat_s function - buffer error

北城以北 提交于 2019-12-12 05:59:52

问题


The question is simple: what's frong with this piece of code?

  size_t  buff = 1;
  size_t  new_buff;
  WCHAR   *var_path;
  WCHAR   *dir_path;

  var_path = new WCHAR[buff];
  new_buff = GetEnvironmentVariableW(L"APPDATA", var_path, buff);

  if (new_buff == 0) {
    return 1;
  } else if (new_buff > buff) {
    delete[] var_path;

    var_path = new WCHAR[new_buff];
    GetEnvironmentVariableW(L"APPDATA", var_path, new_buff);
  }

  dir_path = new WCHAR[new_buff];
  wcscpy_s(dir_path, new_buff, var_path);
  wcscat_s(dir_path, new_buff, L"\\directory");

It says that the Buffer is too small on wcscat_s


回答1:


You only allocate new_buff characters for dir_path (and tell wcscat_s about that size), then you want to append more characters to it. You need to allocate new_buff plus the length of L"\\directory", as well as tell wcscat_s about that actual size.



来源:https://stackoverflow.com/questions/17783456/wcscat-s-function-buffer-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!