Solved

Problem with character strings in a delimeted buffer class

Posted on 2004-10-13
9
223 Views
Last Modified: 2012-06-21
I am working on a class that takes a person object, then packs it to a buffer and writes it to a file.  The problem is that when I try to read it back into the buffer from the file, I get a runtime error.  Here is a copy of the significant functions:

Any help would be appreciated


Here are the pack and unpack functions in my person class:


int Person::Pack (IOBuffer & Buffer) const
{// pack the fields into a FixedFieldBuffer,
 // return TRUE if all succeed, FALSE o/w
      int numBytes;
      Buffer . Clear ();
      numBytes = Buffer . Pack (FirstName);
      if (numBytes == -1) return FALSE;
      numBytes = Buffer . Pack (LastName);
      if (numBytes == -1) return FALSE;
      numBytes = Buffer . Pack (IDNumber);
      if (numBytes == -1) return FALSE;

      return TRUE;
}


int Person::Unpack (IOBuffer & Buffer)
{

      Clear ();
      int numBytes;
      numBytes = Buffer . Unpack (FirstName);
cout << numBytes;
      if (numBytes == -1) return FALSE;
      FirstName[numBytes] = 0;
      numBytes = Buffer . Unpack (LastName);
      if (numBytes == -1) return FALSE;
      LastName[numBytes] = 0;
      numBytes = Buffer . Unpack (IDNumber);
      if (numBytes == -1) return FALSE;
      IDNumber[numBytes] = 0;
      return TRUE;
}



Here is the unpack from  delimited buffer:

int DelimFieldBuffer :: Pack (const void * field, int size)
// set the value of the next field of the buffer;
// if size = -1 (default) use strlen(field) as Delim of field
// return number of bytes packed, -1 if error
{
      // look for delimeter in field!
      short len; // length of string to be packed
      if (size >= 0) len = size;
      else len = strlen ((char*)field);
      if (len > (short)strlen((char*)field)) // field is too short!
            return -1;
      int start = NextByte; // first character to be packed
      NextByte += len + 1;
      if (NextByte > MaxBytes) return -1;
      memcpy (&Buffer[start], field, len);
      Buffer [start+len] = Delim; // add delimeter
      BufferSize = NextByte;
      return len;
}

int DelimFieldBuffer :: Unpack (void * field, int maxBytes)
// extract the value of the next field of the buffer
// return the number of bytes extracted, -1 if error
{
      
      int len = -1; // length of packed string
      int start = NextByte; // first character to be unpacked
      for (int i = start; i < BufferSize; i++)
            if (Buffer[i] == Delim)
                  {len = i - start; break;}
      if (len == -1) return -1; // delimeter not found
      NextByte += len + 1;
      if (NextByte > BufferSize) return -1;
      // check for maxBytes

      memcpy (field,&Buffer[start] , len);      
      if (maxBytes > len || maxBytes == -1)
  ((char*)field) [len] = 0; // zero termination for string
      return len;
}



and here is the test:



      // Test writing
      //Buff . Print (cout);
      ofstream outfile (myfile,ios::out);
      result = Buff . WriteHeader (outfile);
      cout << "write header "<<result<<endl;
      person1 . Pack (Buff);
      //Buff . Print (cout);
      recaddr1 = Buff . Write (outfile);
      cout << "write at "<<recaddr1<<endl;
      person2. Pack (Buff);
      //Buff . Print (cout);
      recaddr2 = Buff . Write (outfile);
      cout << "write at "<<recaddr2<<endl;
      person3. Pack (Buff);
      //Buff . Print (cout);
      recaddr3 = Buff . Write (outfile);
      cout << "write at "<<recaddr3<<endl;
      outfile . close ();

      // test reading

      ifstream infile (myfile, ios::in);
      result = Buff . ReadHeader (infile);

      cout <<"read header "<<result<<endl;

Buff . DRead (infile,recaddr3);
      person . Unpack (Buff);
      person . Print (cout, "First record:");
      Buff . DRead (infile, recaddrw);
      person . Unpack (Buff);
      person . Print (cout, "Second record:");      
0
Comment
Question by:Tim_Heldberg
  • 4
  • 3
  • 2
9 Comments
 
LVL 13

Expert Comment

by:SteH
Comment Utility
What is the runtime error and at which line does it occur?

From the code snippets it is not clear ie how long Buffer is. There might be a write out of bounds in Pack.
If Buffer in Unpack is a file buffer why can you be sure that no record will start in one buffer but end in another?
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Have you tried running that under a debugger?
0
 
LVL 4

Author Comment

by:Tim_Heldberg
Comment Utility
Here is where it is first crashing

    memcpy (field,&Buffer[start] , len);  in the unpack function
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
OK, then let's take a look at that code:

    int len = -1; // length of packed string
    int start = NextByte; // first character to be unpacked  <---- where does 'NextByte' come from?
    for (int i = start; i < BufferSize; i++)
         if (Buffer[i] == Delim)
               {len = i - start; break;}
    if (len == -1) return -1; // delimeter not found
    NextByte += len + 1;
    if (NextByte > BufferSize) return -1;
    // check for maxBytes

    memcpy (field,&Buffer[start] , len);    // what are len and start at this point? What is 'field' pointing to?

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 4

Author Comment

by:Tim_Heldberg
Comment Utility
NextByte is the index of the next byte to be unpacked

len is three, which is the length of the next field to be unpacked
and field is pointing to the next person object field to be unpacked into
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>> NextByte is the index of the next byte to be unpacked

Yes, but is it a member, a globa or a static variable? And, is it valid at that moment?
0
 
LVL 4

Author Comment

by:Tim_Heldberg
Comment Utility
NextByte is an inheirited member from an iobuffer class that is used for different types of buffers, at this point it is four
0
 
LVL 13

Expert Comment

by:SteH
Comment Utility
And what is the value of BufferSize. And how long is Buffer? (I guess BufferSize) Can you post the address of field and Buffer.
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
Comment Utility
Actually, it'd be more helpful to post the whole code so one can run it using a debugger :o)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now