0

Here is the function that isn't working:

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;
}

When run like this, I get the following output:

[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

And, if I go in and change

if ((repl = strstr(tmp, cursym->sym_name)) != NULL)
to
if ((repl = strstr(tmp, "$start")) != NULL)

I get the following output:

[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

As it should be. Attached are pastebin links to the whole project:

main.cpp: http://pastebin.com/AiDCbsCt

Symtab.h: http://pastebin.com/Kmcn6NzV

Symtab.cpp: http://pastebin.com/mxtPL1d2

Any ideas?

vonbrand
  • 11,412
  • 8
  • 32
  • 52
phyrrus9
  • 1,441
  • 11
  • 26

1 Answers1

0

Found and corrected the problem

            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

Needs to be changed to

            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
phyrrus9
  • 1,441
  • 11
  • 26
  • 1
    Sounds like OP was not aware that `strncpy` does not null-terminate the destination buffer if it ran out of room. He seems to have been treating the third argument as a source length, when in fact it represents the destination buffer size. – M.M Mar 12 '14 at 02:39
  • Well, I do now, thanks for telling me that! I thought there was just something jank with my copy. – phyrrus9 Mar 12 '14 at 02:40
  • 1
    Personally I don't use `strncpy` for this reason - I will use `memcpy` and manually put the terminator on; or use `snprintf` which always null-terminates. – M.M Mar 12 '14 at 02:41
  • If you're using C++ anyway, why not use string objects? – rainbowgoblin Mar 12 '14 at 03:53
  • @rainbowgoblin I believe that the string class is EVIL. Only reason it is C++ is because I didn't want to write a C queue – phyrrus9 Mar 12 '14 at 14:36