C查找替换isn';行不通

C查找替换isn';行不通,c,strstr,C,Strstr,以下是不起作用的函数: char * insert_symtab(char *buf) { char *ret = (char *)malloc(strlen(buf) + 512); char *tmp = NULL; char *repl = NULL; char obuf[32]; //for converting int to hex Symtab_struct *cursym; lineQueue *lq = readlines(buf); int i;

以下是不起作用的函数:

char * insert_symtab(char *buf)
{
  char *ret = (char *)malloc(strlen(buf) + 512);
  char *tmp = NULL;
  char *repl = NULL;
  char obuf[32]; //for converting int to hex
  Symtab_struct *cursym;
  lineQueue *lq = readlines(buf);
  int i;
  strcpy(ret, "");
  while (!lq->empty())
  {
    tmp = getlinefromqueue(lq);
    for (i = 0; (cursym = symtab->findNodeByNumber(i)) != NULL; i++)
    {
        sprintf(obuf, "0x%04x", cursym->sym_location);
        //printf("-->looking for %s\n", cursym->sym_name);
        if ((repl = strstr(tmp, cursym->sym_name)) != NULL)
        {
            printf("---->Found %s\n", cursym->sym_name);
            strncpy (repl, obuf, strlen(cursym->sym_name));
        }
    }
    strcat(ret, tmp);
    strcat(ret, "\n");
  }
  return ret;
}
这样运行时,我得到以下输出:

[WARN process_includes] Cannot find file included 1test2.asm
[DEBUG build_symtab] Inserting symbol  to location 0x0000
[DEBUG build_symtab] Inserting symbol $start to location 0x0008
[DEBUG build_symtab] Inserting symbol $eight to location 0x0008
[WARN in Symtab::insertNode] Redefinition of symbol $start is ignored
;include '1test2.asm'
:start:
mov a b
add a b
:eight:
:start:
sub a b
$start
[WARN process_includes] Cannot find file included 1test2.asm
[DEBUG build_symtab] Inserting symbol  to location 0x0000
[DEBUG build_symtab] Inserting symbol $start to location 0x0008
[DEBUG build_symtab] Inserting symbol $eight to location 0x0008
[WARN in Symtab::insertNode] Redefinition of symbol $start is ignored
---->Found $start
;include '1test2.asm'
:start:
mov a b
add a b
:eight:
:start:
sub a b
0x0000
如果我进去换衣服

if ((repl = strstr(tmp, cursym->sym_name)) != NULL)
to
if ((repl = strstr(tmp, "$start")) != NULL)
我得到以下输出:

[WARN process_includes] Cannot find file included 1test2.asm
[DEBUG build_symtab] Inserting symbol  to location 0x0000
[DEBUG build_symtab] Inserting symbol $start to location 0x0008
[DEBUG build_symtab] Inserting symbol $eight to location 0x0008
[WARN in Symtab::insertNode] Redefinition of symbol $start is ignored
;include '1test2.asm'
:start:
mov a b
add a b
:eight:
:start:
sub a b
$start
[WARN process_includes] Cannot find file included 1test2.asm
[DEBUG build_symtab] Inserting symbol  to location 0x0000
[DEBUG build_symtab] Inserting symbol $start to location 0x0008
[DEBUG build_symtab] Inserting symbol $eight to location 0x0008
[WARN in Symtab::insertNode] Redefinition of symbol $start is ignored
---->Found $start
;include '1test2.asm'
:start:
mov a b
add a b
:eight:
:start:
sub a b
0x0000
应该如此。附件是指向整个项目的pastebin链接:

main.cpp:

Symtab.h:

Symtab.cpp:


有什么想法吗?

发现并纠正了问题

            for (i = 1; i < 28 && tmp[i] != ':'; i++); //i now points to the next colon
            strncpy(sym_name_tmp, tmp + 1, i - 1); //grab the symbol
for(i=1;i<28&&tmp[i]!=':';i++)//我现在指向下一个冒号
strncpy(符号名称、tmp、tmp+1、i-1)//抓住符号
需要改成

            for (i = 1; i < 28 && tmp[i] != ':'; i++); //i now points to the next colon
            strncpy(sym_name_tmp, tmp + 1, i); //grab the symbol
            *strstr(sym_name_tmp, ":") = 0; //this fixes teh 0x7f issue
for(i=1;i<28&&tmp[i]!=':';i++)//我现在指向下一个冒号
strncpy(符号名称tmp,tmp+1,i)//抓住符号
*strstr(sym_name_tmp,“:”)=0//这修复了0x7f问题

使用调试器检查调用
strstrstr()
cursym->sym\u name
是否曾经是“$start”。您正在读取什么输入数据?@timrau(gdb)p*cursym$2={sym\u name=“$start”,“'\000”,sym\u location=0,next=0x100103b40}@rainbowlin@phyrus9接下来,检查
tmp
是否曾经包含
“$start”
cursym->sym\u name
时,“$start”“
。听起来OP好像没有意识到,
strncpy
在目标缓冲区空间不足时不会为空终止目标缓冲区。他似乎一直把第三个参数当作源长度,而实际上它代表了目标缓冲区的大小!我以为我的副本有点不对劲。我个人不会因为这个原因使用strncpy——我会使用memcpy并手动打开终结者;或者使用<代码> SNPROTFF < /COD>始终是空的。如果你使用C++,为什么不使用字符串对象?@ RunBooGbLin,我相信字符串类是邪恶的。唯一的原因是C++是因为我不想写C队列