Do we need to call wordfree upon wordexp failure?

对着背影说爱祢 提交于 2021-02-08 19:38:27

问题


Do we need to call wordfree upon wordexp failure? Calling wordfree seems to segfault in some cases (eg when wordfree returns error code with string is "foo 'bar"). This isn't clear from man page, and I've seen wordfree used in some error cases.


回答1:


According to the GNU's manual example, it should be called on error only if WRDE_NOSPACE was returned:

switch (wordexp (program, &result, 0))
{
case 0:         /* Successful.  */
  break;
case WRDE_NOSPACE:
  /* If the error was WRDE_NOSPACE,
     then perhaps part of the result was allocated.  */
  wordfree (&result);
default:                    /* Some other error.  */
  return -1;
}


来源:https://stackoverflow.com/questions/23120084/do-we-need-to-call-wordfree-upon-wordexp-failure

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