在将 HP-UX C++ 应用程序移植到 LINUX 的过程中,我注意到有一个 int 类型的函数,其中并非所有代码路径似乎都返回整数值。当我在 HP-UX 下编译和运行应用程序时(使用 acc 编译器进行编译),它通过未明确声明返回值的代码路径返回 0。但是,当我在 LINUX 下编译和运行同一个应用程序时,我得到一个返回值 -72 并且应用程序因此出现错误(这是在返回值小于 0 时设计的)。我注意到经典 C++(ACC 编译器支持的一个非常古老且过时的标准)处理变量范围的方式与标准 C++(不幸的是 ACC 不支持)略有不同。在经典 C++ 中,似乎在 forloop 声明中声明一个整数
for( int index = 0; index < array.length; index++ )
变量 index 可以在 forloop 之外访问,尽管我不确定返回语句是否以相同的方式处理,以便 aCC 识别所有代码路径都返回一个值。
我正在处理的功能如下:
int process_phase (const char *phase, const char *seg_type, const char *dist_target, const char *action_target, char *cmd)
{
char cmd2[MAX_STRING];
printf( "I AM INSIDE THE PROCESS_PHASE\n" );
if (TRACE_MODE)
{
printf ("%s %s\n", MSG_LOOKUP("MSG_PHASE"), phase);
printf ("%s %s\n", MSG_LOOKUP("MSG_SEG_TYPE"), seg_type);
printf ("%s %s\n", MSG_LOOKUP("MSG_DIST_TARGET"), dist_target);
printf ("%s %s\n", MSG_LOOKUP("MSG_ACTION_TARGET"), action_target);
printf ("%s %s\n", MSG_LOOKUP("MSG_CMD"), cmd);
}
// Remove the pre- and post- prefixes
const char *phase_ref = strchr(phase, '-');
printf ("PHASE REF BEFORE PREFIX REMOVAL: %s\n", phase_ref );
if (phase_ref)
{
phase_ref++;
}
else
{
phase_ref = phase;
}
printf ("PHASE REF AFTER PREFIX REMOVAL: %s\n", phase_ref );
if (TRACE_MODE)
{
printf ("%s %s\n", MSG_LOOKUP("MSG_PHASE_REF"), phase_ref);
}
printf ( "==========TEST PHASE 1 BEGIN==========\n" );
if (strcasecmp(phase_ref, "all_phases") != 0)
{
if (DO_TRANSFER && strcasecmp(phase_ref, "transfer") != 0)
{
printf ("IN ONE\n");
return 0;
}
else if (DO_TAPE_GENERATION && strcasecmp(phase_ref, "tape_generation") != 0)
{
printf ("IN TWO\n");
return 0;
}
else if (DO_TAPE_EXTRACTION && strcasecmp(phase_ref, "tape_extraction") != 0)
{
printf ("IN THREE\n");
return 0;
}
else if (DO_PREPARATION && strcasecmp(phase_ref, "preparation") != 0)
{
printf ("IN FOUR\n");
return 0;
}
else if (DO_DISTRIBUTION && strcasecmp(phase_ref, "distribution") != 0)
{
printf ("IN FIVE\n");
return 0;
}
else if (DO_VERIFICATION && strcasecmp(phase_ref, "verification") != 0)
{
printf ("IN SIX\n");
return 0;
}
else if (DO_ACTIVATION && strcasecmp(phase_ref, "activation") != 0)
{
printf ("IN SEVEN\n");
return 0;
}
else if (DO_REMOVAL && strcasecmp(phase_ref, "removal") != 0)
{
printf ("IN EIGHT\n");
return 0;
}
}
printf ( "==========TEST PHASE 1 END==========\n" );
// if (strstr(seg_type, envvar("SEGMENT_TYPE")) == 0 && strcasecmp(seg_type, "ALL") !=0) return 0;
char tmp_seg_type[MAX_BUFFLEN];
sprintf (tmp_seg_type, "_%s_", seg_type);
printf( "tmp_seg_type: %s\n", tmp_seg_type );
char tmp_envar_seg_type[MAX_BUFFLEN];
sprintf (tmp_envar_seg_type, "_%s_", envvar("SEGMENT_TYPE"));
printf( "tmp_seg_type: %s\n", tmp_seg_type );
if ( strstr(tmp_seg_type, tmp_envar_seg_type) == 0 && strcasecmp(seg_type, "ALL") != 0 )
{
printf( "IN TEST PHASE TWO\n" );
return 0;
}
char match_list[MAX_BUFFLEN];
printf ( "==========TEST PHASE THREE BEGIN==========\n" );
if ( DO_TRANSFER )
{
printf( "IN ONE\n" );
sprintf( match_list, "DIST_NODES", dist_target );
}
else if (DO_TAPE_GENERATION)
{
printf( "IN TWO\n" );
sprintf( match_list, "HOST_NODES", dist_target );
}
else
{
printf( "IN THREE\n" );
sprintf( match_list, "%s_NODES", dist_target );
printf( "match_list: %s\n", match_list );
}
printf ( "==========TEST PHASE THREE END==========\n" );
char matched_nodes[MAX_BUFFLEN];
get_env(match_list, matched_nodes);
printf( "matched_nodes: %s\n", matched_nodes );
word_sort_unique(matched_nodes);
printf( "sorted_matched_nodes: %s\n", matched_nodes );
const char *element_separator = " ";
const char *curr_node_type;
char *curr_node = strtok(matched_nodes, element_separator);
printf( "curr_node: %s\n", curr_node );
int whileiteration = 0;
while (curr_node)
{
printf( "WHILE LOOP ITERATION: %d\n", whileiteration );
int node_idx;
bool matched = false;
for (node_idx=0; node_idx<node_count; node_idx++)
{
if (strcmp(curr_node, node_table[node_idx]) == 0)
{
matched = true;
break;
}
}
if (matched)
{
if (strcasecmp(action_target, "TARGET") == 0)
{
if (indent[node_idx][0] == 0)
{
fprintf (outfile[node_idx], "remsh %s \"\n", curr_node);
indent[node_idx] = " ";
fprintf (outfile[node_idx], "%s. %s/site_profile\n", indent[node_idx], envvar ("TOOLS_DIR"));
fprintf (outfile[node_idx], "%s. %s/install_profile\n", indent[node_idx], envvar ("TOOLS_DIR"));
fprintf (outfile[node_idx], "%sexport LANG=%s\n", indent[node_idx], envvar ("LANG"));
}
}
else
{
// Input redirection from /dev/echo enables that the ports reserved for remsh on both client and
// server node get released immediately without any inactivity timeout period
if (indent[node_idx][0] != 0)
{
fprintf (outfile[node_idx], "\" < /dev/echo \n");
}
indent[node_idx] = "";
}
// Do parameter substitution
strcpy(cmd2, cmd);
expand_string (cmd2, "$BUILD_VERSION", BUILD_VERSION);
expand_string (cmd2, "${BUILD_VERSION}", BUILD_VERSION);
expand_string (cmd2, "$TARGET_NODE", curr_node);
expand_string (cmd2, "${TARGET_NODE}", curr_node);
curr_node_type = target_lookup (curr_node);
expand_string (cmd2, "$NODE_TYPE", curr_node_type);
expand_string (cmd2, "${NODE_TYPE}", curr_node_type);
fprintf(outfile[node_idx], "%s%s\n", indent[node_idx], cmd2);
file_active[node_idx] = true;
}
curr_node = strtok(NULL, element_separator);
whileiteration++;
}
printf ( "EXITING PROCESS PHASE\n" );
}
我已经使用代码检测在 HP-UX 和 LINUX 上打印出变量和其他输出,直到 while 循环它们看起来是相同的。在某些情况下,即使由于变量 curr_node 为空而忽略了 while 循环并退出了函数,其中 EXITING PROCESS PHASE 打印到 stdout,HP-UX 和 LINUX 之间的输出也是相同的。但是,HP-UX 和 LINUX 之间的返回值是完全不同的。我想不通的是为什么。