ACE 的 BUG ? ACE_Process_Options::setenv

mymtom 2013-03-14 04:27:40
C++NPv1 8.3 的例子程序,会 Memory Fault.

#109 0x2838a53e in vsnprintf () from /lib/libc.so.7
#110 0x281aa607 in ACE_Process_Options::setenv (this=0xbfbfe094, variable_name=0x8048fad "PROGRAM=%s",
format=0xbfbfed16 "factorial") at OS_NS_stdio.inl:1044
#111 0x08048b99 in main (argc=1, argv=0xbfbfeba4) at factorial.cpp:25

发现希望调用的函数是
int ACE_Process_Options::setenv (const ACE_TCHAR *format, ...),
结果调用的却是
int ACE_Process_Options::setenv (const ACE_TCHAR *variable_name,
const ACE_TCHAR *format, ...)

这样设计函数有问题啊,
options.setenv ("PROGRAM=%s", ACE::basename (argv[0]));

可以两个都可以调用啊。


8.3 The ACE_Process_Options Class


/**
* @file factorial.cpp
* @brief
*/

#include "ace/OS.h"
#include "ace/ACE.h"
#include "ace/Process.h"

int main(int argc, char *argv[])
{
ACE_Process_Options options;
FILE *fp = NULL;
char *n_env = NULL;
int n;

if (argc == 1) {
// Top-level process.
n_env = ACE_OS::getenv ("FACTORIAL");
n = n_env == 0 ? 0 : atoi (n_env);
options.command_line ("%s %d", argv[0], n == 0 ? 10 : n);
const char *working_dir = ACE_OS::getenv ("WORKING_DIR");
if (working_dir) options.working_directory (working_dir);
fp = fopen ("factorial.log", "a");
options.setenv ("PROGRAM=%s", ACE::basename (argv[0]));
} else {
fp = fopen ("factorial.log", "a");
if (atoi (argv[1]) == 1) {
// Base case
fprintf (fp, "[%s|%d]: base case\n",
ACE_OS::getenv ("PROGRAM"), ACE_OS::getpid ());
fclose (fp);
return 1;
} else {
n = atoi (argv[1]);
options.command_line ("%s %d", argv[0], n - 1);
}
}

ACE_Process child;
// Make ''recursive'' call.
child.spawn (options);
child.wait ();
int factorial = n * child.exit_code (); // Compute n factorial
fprintf (fp, "[%s | %d]: %d! == %d\n",
ACE_OS::getenv ("PROGRAM"), ACE_OS::getpid (), n, factorial);
fclose (fp);

return 0;
}
...全文
294 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
prohibit 2013-04-28
  • 打赏
  • 举报
回复
引用 6 楼 mymtom 的回复:
[quote=引用 3 楼 prohibit 的回复:] 呃,这重载不起作用了?直接options.setenv ("%s=%s", "PROGRAM", ACE::basename(argv[0]));算了。。。
这个也不能确定该调用哪一个啊! 下面的两个函数都可以的,不知道标准上怎么描述。

int ACE_Process_Options::setenv (const ACE_TCHAR *format, ...),
int ACE_Process_Options::setenv (const ACE_TCHAR *variable_name,
                                 const ACE_TCHAR *format, ...)
[/quote]
char buf[128] = {0};
snprintf(buf, 128, "PROGRAM=%s", ACE::basename(argv[0]));
options.setenv(buf);
mymtom 2013-04-28
  • 打赏
  • 举报
回复
引用 3 楼 prohibit 的回复:
呃,这重载不起作用了?直接options.setenv ("%s=%s", "PROGRAM", ACE::basename(argv[0]));算了。。。
这个也不能确定该调用哪一个啊! 下面的两个函数都可以的,不知道标准上怎么描述。

int ACE_Process_Options::setenv (const ACE_TCHAR *format, ...),
int ACE_Process_Options::setenv (const ACE_TCHAR *variable_name,
                                 const ACE_TCHAR *format, ...)
云斜月 2013-04-27
  • 打赏
  • 举报
回复
顺便问下,linux下有atoi的实现?
云斜月 2013-04-27
  • 打赏
  • 举报
回复
发现希望调用的函数是 int ACE_Process_Options::setenv (const ACE_TCHAR *format, ...), 结果调用的却是 int ACE_Process_Options::setenv (const ACE_TCHAR *variable_name, const ACE_TCHAR *format, ...) 函数调用有个最佳匹配的问题,当只有一个参数时,肯定调用int ACE_Process_Options::setenv (const ACE_TCHAR *format, ...), 而大于等于两个参数时就是int ACE_Process_Options::setenv (const ACE_TCHAR *variable_name, const ACE_TCHAR *format, ...)。 所以说,,,
prohibit 2013-04-15
  • 打赏
  • 举报
回复
呃,这重载不起作用了?直接options.setenv ("%s=%s", "PROGRAM", ACE::basename(argv[0]));算了。。。
morris88 2013-04-15
  • 打赏
  • 举报
回复
宇智波-斜眼 2013-04-15
  • 打赏
  • 举报
回复
这么冷的帖

23,110

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧