NULL值被打印出来

huangapple go评论57阅读模式
英文:

Reg: NULL value getting printed

问题

请查看以下显示出现奇怪行为的代码部分:

static int call_apab_process()
{
   static char *pcFnctnNm = "call_apab_process";
   int rc = SUCCESS;
   char cmd[4000];
   char pid[11];
   char logDir[2000];
   cmd[0] = '
static int call_apab_process()
{
   static char *pcFnctnNm = "call_apab_process";
   int rc = SUCCESS;
   char cmd[4000];
   char pid[11];
   char logDir[2000];
   cmd[0] = '\0';
   pid[0] = '\0';
   logDir[0] = '\0';
   dbg_indent(pcFnctnNm);
   dbg_print("Entering Into call_apab_process");
   dbg_print("Print Parameters..");
   dbg_print("MY_HOME     :[%s]", getenv("MY_HOME"));
   dbg_print("SESSION_ID    :[%ld]", pvsp_env->sessionid);
   sprintf(cmd, "%s/bin/GQ19apab_DB_auto.sh %ld", getenv("MY_HOME"), pvsp_env->sessionid);
   sprintf(pid, "0%ld", pvsp_env->sessionid);
   sprintf(logDir, "%s/tmp/db", getenv("MY_HOME"));
   dbg_print("cmd :[%s]", cmd);
}
'
;
pid[0] = '
static int call_apab_process()
{
   static char *pcFnctnNm = "call_apab_process";
   int rc = SUCCESS;
   char cmd[4000];
   char pid[11];
   char logDir[2000];
   cmd[0] = '\0';
   pid[0] = '\0';
   logDir[0] = '\0';
   dbg_indent(pcFnctnNm);
   dbg_print("Entering Into call_apab_process");
   dbg_print("Print Parameters..");
   dbg_print("MY_HOME     :[%s]", getenv("MY_HOME"));
   dbg_print("SESSION_ID    :[%ld]", pvsp_env->sessionid);
   sprintf(cmd, "%s/bin/GQ19apab_DB_auto.sh %ld", getenv("MY_HOME"), pvsp_env->sessionid);
   sprintf(pid, "0%ld", pvsp_env->sessionid);
   sprintf(logDir, "%s/tmp/db", getenv("MY_HOME"));
   dbg_print("cmd :[%s]", cmd);
}
'
;
logDir[0] = '
static int call_apab_process()
{
   static char *pcFnctnNm = "call_apab_process";
   int rc = SUCCESS;
   char cmd[4000];
   char pid[11];
   char logDir[2000];
   cmd[0] = '\0';
   pid[0] = '\0';
   logDir[0] = '\0';
   dbg_indent(pcFnctnNm);
   dbg_print("Entering Into call_apab_process");
   dbg_print("Print Parameters..");
   dbg_print("MY_HOME     :[%s]", getenv("MY_HOME"));
   dbg_print("SESSION_ID    :[%ld]", pvsp_env->sessionid);
   sprintf(cmd, "%s/bin/GQ19apab_DB_auto.sh %ld", getenv("MY_HOME"), pvsp_env->sessionid);
   sprintf(pid, "0%ld", pvsp_env->sessionid);
   sprintf(logDir, "%s/tmp/db", getenv("MY_HOME"));
   dbg_print("cmd :[%s]", cmd);
}
'
;
dbg_indent(pcFnctnNm); dbg_print("Entering Into call_apab_process"); dbg_print("Print Parameters.."); dbg_print("MY_HOME :[%s]", getenv("MY_HOME")); dbg_print("SESSION_ID :[%ld]", pvsp_env->sessionid); sprintf(cmd, "%s/bin/GQ19apab_DB_auto.sh %ld", getenv("MY_HOME"), pvsp_env->sessionid); sprintf(pid, "0%ld", pvsp_env->sessionid); sprintf(logDir, "%s/tmp/db", getenv("MY_HOME")); dbg_print("cmd :[%s]", cmd); }

以下是输出结果:

Entering Into call_dadp_process
Print Parameters..
MY_HOME     :[/opt/apnp]
SESSION_ID    :[1054628118]
cmd :[]

有人能否指导我了解为什么 cmd 的值显示为空?当我单独打印 MY_HOME 和 session_id 的值时,我确实看到这些值是存在的。

英文:

Please find the code below which is showing some weird behavior

static int call_apab_process()      
{
   static char *pcFnctnNm = "call_apab_process";
   int rc = SUCCESS;
   char cmd[4000];
   char pid[11];
   char logDir[2000];
   cmd[0] = '
static int call_apab_process()      
{
static char *pcFnctnNm = "call_apab_process";
int rc = SUCCESS;
char cmd[4000];
char pid[11];
char logDir[2000];
cmd[0] = '\0';
pid[0] = '\0';
logDir[0] = '\0';
dbg_indent(pcFnctnNm);
dbg_print("Entering Into call_apab_process");
dbg_print("Print Parameters..");
dbg_print("MY_HOME     :[%s]",getenv("MY_HOME"));
dbg_print("SESSION_ID    :[%ld]",pvsp_env->sessionid);
sprintf(cmd,"%s/bin/GQ19apab_DB_auto.sh %ld",getenv("MY_HOME"),pvsp_env->sessionid);
sprintf(pid,"0%ld",pvsp_env->sessionid);
sprintf(logDir,"%s/tmp/db",getenv("MY_HOME"));
dbg_print("cmd :[%s]",cmd);
}
The output is as given below.
Entering Into call_dadp_process
Print Parameters..
MY_HOME     :[/opt/apnp]
SESSION_ID    :[1054628118]
cmd :[]
'; pid[0] = '
static int call_apab_process()      
{
static char *pcFnctnNm = "call_apab_process";
int rc = SUCCESS;
char cmd[4000];
char pid[11];
char logDir[2000];
cmd[0] = '\0';
pid[0] = '\0';
logDir[0] = '\0';
dbg_indent(pcFnctnNm);
dbg_print("Entering Into call_apab_process");
dbg_print("Print Parameters..");
dbg_print("MY_HOME     :[%s]",getenv("MY_HOME"));
dbg_print("SESSION_ID    :[%ld]",pvsp_env->sessionid);
sprintf(cmd,"%s/bin/GQ19apab_DB_auto.sh %ld",getenv("MY_HOME"),pvsp_env->sessionid);
sprintf(pid,"0%ld",pvsp_env->sessionid);
sprintf(logDir,"%s/tmp/db",getenv("MY_HOME"));
dbg_print("cmd :[%s]",cmd);
}
The output is as given below.
Entering Into call_dadp_process
Print Parameters..
MY_HOME     :[/opt/apnp]
SESSION_ID    :[1054628118]
cmd :[]
'; logDir[0] = '
static int call_apab_process()      
{
static char *pcFnctnNm = "call_apab_process";
int rc = SUCCESS;
char cmd[4000];
char pid[11];
char logDir[2000];
cmd[0] = '\0';
pid[0] = '\0';
logDir[0] = '\0';
dbg_indent(pcFnctnNm);
dbg_print("Entering Into call_apab_process");
dbg_print("Print Parameters..");
dbg_print("MY_HOME     :[%s]",getenv("MY_HOME"));
dbg_print("SESSION_ID    :[%ld]",pvsp_env->sessionid);
sprintf(cmd,"%s/bin/GQ19apab_DB_auto.sh %ld",getenv("MY_HOME"),pvsp_env->sessionid);
sprintf(pid,"0%ld",pvsp_env->sessionid);
sprintf(logDir,"%s/tmp/db",getenv("MY_HOME"));
dbg_print("cmd :[%s]",cmd);
}
The output is as given below.
Entering Into call_dadp_process
Print Parameters..
MY_HOME     :[/opt/apnp]
SESSION_ID    :[1054628118]
cmd :[]
'; dbg_indent(pcFnctnNm); dbg_print("Entering Into call_apab_process"); dbg_print("Print Parameters.."); dbg_print("MY_HOME :[%s]",getenv("MY_HOME")); dbg_print("SESSION_ID :[%ld]",pvsp_env->sessionid); sprintf(cmd,"%s/bin/GQ19apab_DB_auto.sh %ld",getenv("MY_HOME"),pvsp_env->sessionid); sprintf(pid,"0%ld",pvsp_env->sessionid); sprintf(logDir,"%s/tmp/db",getenv("MY_HOME")); dbg_print("cmd :[%s]",cmd); } The output is as given below. Entering Into call_dadp_process Print Parameters.. MY_HOME :[/opt/apnp] SESSION_ID :[1054628118] cmd :[]

Can someone please guide me to understand why the value of cmd is displayed as null? When I print the values of MY_HOME and session_id individually I do see that the values are present.

答案1

得分: 3

The translated content is as follows:

sprintf(pid, "0%ld", pvsp_env->sessionid);

绝对越界写入。您的输出显示

SESSION_ID    :[1054628118]

一个包含10位数字的数字。在`sprintf()`中,您添加了一个前导`0`,因此您写入了11个字节+`'
sprintf(pid, "0%ld", pvsp_env->sessionid);

绝对越界写入。您的输出显示

SESSION_ID    :[1054628118]

一个包含10位数字的数字。在`sprintf()`中,您添加了一个前导`0`,因此您写入了11个字节+`'\0'`,总共12个字节,但`pid`只定义为`char pid[11];`

这会在您的情况下引发未定义行为,显然`'\0'`会覆盖`cmd`的第一个字节,并使其成为空字符串(而不是您所述的NULL,这意味着一个NULL指针)。
'
`,总共12个字节,但`pid`只定义为`char pid[11];`
这会在您的情况下引发未定义行为,显然`'
sprintf(pid, "0%ld", pvsp_env->sessionid);

绝对越界写入。您的输出显示

SESSION_ID    :[1054628118]

一个包含10位数字的数字。在`sprintf()`中,您添加了一个前导`0`,因此您写入了11个字节+`'\0'`,总共12个字节,但`pid`只定义为`char pid[11];`

这会在您的情况下引发未定义行为,显然`'\0'`会覆盖`cmd`的第一个字节,并使其成为空字符串(而不是您所述的NULL,这意味着一个NULL指针)。
'
`会覆盖`cmd`的第一个字节,并使其成为空字符串(而不是您所述的NULL,这意味着一个NULL指针)。
英文:
sprintf(pid,"0%ld",pvsp_env->sessionid);

definitly writes out of bounds. Your output shows

SESSION_ID    :[1054628118]

a 10 digit number. In sprintf() you add a leading 0 thus you are writing 11 bytes + '\0' makes 12 in total, but pid is only defined as char pid[11];

That invokes Undefined Behaviour in your case the '\0' obviously overwrites the first byte of cmd and makes it an empty string (not NULL as you stated what would mean a NULL pointer)

huangapple
  • 本文由 发表于 2023年5月25日 21:18:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332746.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定