Segmentation Fault when using Strlen()

3,029 views
Skip to first unread message

Rex Stefan

unread,
Sep 6, 2016, 6:45:39 PM9/6/16
to ns-3-users
Good Day NS-3 users,

In a part of my code, I have used strlen() function to find the length of the char* datatype. I have included string.h header file as well in the code but I come across the following error while debugging. Could someone please help me out why this error has been thrown. I have provided a partial part of my code as well below.

GoosePduHeader::Serialize (Buffer::Iterator start) const
{

uint16_t x;
Buffer::Iterator i = start;
for (x=0;x<strlen(gocbRef);x++)
    {
    i.WriteU8 (gocbRef[x]);
    }
}

Error:

Program received signal SIGSEGV, Segmentation fault.
strlen () at ../sysdeps/x86_64/strlen.S:106
106 ../sysdeps/x86_64/strlen.S: No such file or directory.


Regards,
Rex

Tommaso Pecorella

unread,
Sep 6, 2016, 7:53:07 PM9/6/16
to ns-3-users
Strlen assumes that your "string" is null terminated. If it isn't, then you'll get a segmentation fault (most probably).

T.

Rex Stefan

unread,
Sep 6, 2016, 7:57:51 PM9/6/16
to ns-3-users
Dear Tommaso,

Thanks a lot for your reply. Not terminating the string might be the reason for the error. I'll rectify it and will get back to you if I have any more queries related to this.

Thanks,
Rex

Rex Stefan

unread,
Sep 6, 2016, 11:15:33 PM9/6/16
to ns-3-users
Dear Tommaso,

As you suggested, I have terminated the strings. After doing so, I am getting the same error particularly at the Deserialize function. I have provided the code below. If you have any idea why I am particularly getting at Deserialize function, it would be really helpful for me to proceed further.

uint32_t
GoosePduHeader::Deserialize (Buffer::Iterator start)
{

uint16_t x;
Buffer::Iterator i = start;
{
//uint8_t m_gocbRef = strlen(gocbRef);
for (x=0;x<strlen(gocbRef);x++)
{
gocbRef[x]=i.ReadU8();
}
}
}

Thanks a lot once again.

Regards,
Rex

Tommaso Pecorella

unread,
Sep 7, 2016, 8:41:07 AM9/7/16
to ns-3-users
Please use some logic in what you do.
If you're reading gocbRef, how could you expect to know (in advance) its length ?

This topic (how to serialize and deserialize a variable-length field) has been discussed in the past. Please check the past discussions.

T.

JaNa

unread,
Sep 7, 2016, 9:07:51 PM9/7/16
to ns-3-...@googlegroups.com
Hi,

Agree with T. "If you're reading gocbRef, how could you expect to know (in advance) its length ?" unless you send it beforehand.

So, why don't you send the size of the string first and read according to that size.

Hope this might help you.

Cheers
JaNa


--
Posting to this group should follow these guidelines https://www.nsnam.org/wiki/Ns-3-users-guidelines-for-posting
---
You received this message because you are subscribed to the Google Groups "ns-3-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ns-3-users+unsubscribe@googlegroups.com.
To post to this group, send email to ns-3-...@googlegroups.com.
Visit this group at https://groups.google.com/group/ns-3-users.
For more options, visit https://groups.google.com/d/optout.



--
Best Regards..

    """""""""""
:)   JaNa     :) 
    """""""""""
Reply all
Reply to author
Forward
0 new messages