Bug 238

Summary: missing break statement
Product: ns-3 Reporter: Mathieu Lacage <mathieu.lacage>
Component: internetAssignee: ns-bugs <ns-bugs>
Status: RESOLVED INVALID    
Severity: normal    
Priority: P1    
Version: pre-release   
Hardware: All   
OS: All   
Attachments: detect bug
add a non-violable assertion AND fix the issue

Description Mathieu Lacage 2008-06-25 17:58:26 UTC
there is obviously a missing break statement in TcpSocketImpl::Recv line 519 but I have never ever seen a testcase hitting that block of code which would not exit the outer loop immediately so, there is never any bad behavior.

I would like to propose the attached patch to detect the bad behavior which will hopefully allow us to obtain a testcase if there is one and, if there is none, will allow us to document this issue.
Comment 1 Mathieu Lacage 2008-06-25 17:59:25 UTC
Created attachment 184 [details]
detect bug
Comment 2 Rajib Bhattacharjea 2008-06-25 23:19:30 UTC
Created attachment 186 [details]
add a non-violable assertion AND fix the issue

I'm not sure patch 184 will correctly detect the issue.  Once we break into "bad" territory in this loop (after the else is hit for the first time and doesn't break the loop), it will only enter the else repeatedly, making the assertion added in the if never get hit.

Anyway, I think correct thing to do is add an assertion on the fundamental condition which cannot be violated by this code, and that is that this loop shouldn't continue to tack on data to the return packet beyond the maxSize parameter.  Adding this assertion would detect the issue. 

Beyond detection, as the writer of this code, I know I made a mistake and missed a break statement.  This is easy to correct.  I don't see the need to "document" a what's tantamount to a typo...unless a bug is suspected in the else block in question.  Is this the case?

Mathieu's patch attempting to detect but not fix the issue indicates that he is reluctant to check in the fix without a test case.  This bug's P1 status would then mean that until a test is provided, ns3.1 is deferred?  This seems like an awful lot of hassle over a typo I checked in.  I propose an added assertion and fix be checked in simultaneously, and ASAP.
Comment 3 Mathieu Lacage 2008-06-26 11:02:26 UTC
(In reply to comment #2)
> Created an attachment (id=186) [edit]
> add a non-violable assertion AND fix the issue
> 
> I'm not sure patch 184 will correctly detect the issue.  Once we break into
> "bad" territory in this loop (after the else is hit for the first time and
> doesn't break the loop), it will only enter the else repeatedly, making the
> assertion added in the if never get hit.
> 
> Anyway, I think correct thing to do is add an assertion on the fundamental
> condition which cannot be violated by this code, and that is that this loop
> shouldn't continue to tack on data to the return packet beyond the maxSize
> parameter.  Adding this assertion would detect the issue. 
> 
> Beyond detection, as the writer of this code, I know I made a mistake and
> missed a break statement.  This is easy to correct.  I don't see the need to
> "document" a what's tantamount to a typo...unless a bug is suspected in the
> else block in question.  Is this the case?
> 
> Mathieu's patch attempting to detect but not fix the issue indicates that he is
> reluctant to check in the fix without a test case.  This bug's P1 status would
> then mean that until a test is provided, ns3.1 is deferred?  This seems like an
> awful lot of hassle over a typo I checked in.  I propose an added assertion and
> fix be checked in simultaneously, and ASAP.

This is your call but, again, I think that if you really understand this code, you should be able to demonstrate a testcase which hits this bug.

Comment 4 Rajib Bhattacharjea 2008-06-27 00:29:27 UTC
After very carefully reviewing this code with a test case, there is no crash when this code is hit, and no logical mistake whatsoever.  In other words, the code that is checked in does exactly what it is supposed to do, although it is not obvious, and I forgot the subtleties of how I intended it to work.  The logic in TcpSocketImpl::Recv may seem convoluted, but it is doing the right thing without the break.  Adding the break here would actually cause m_rxAvailable to be incorrect when this method exits, which you can verify for yourself.  I can explain the code in more detail if requested.

Test case at: 
http://code.nsnam.org/raj/ns-3-dev-http/
./waf --run=simple-http

This code is very non-optimal anyway in terms of efficiency, so it needs a reworking eventually; hopefully it will become more understandable at that time.
Comment 5 Craig Dowell 2008-06-27 12:15:54 UTC
Shall I mark this bug as INVALID and close it?
Comment 6 Mathieu Lacage 2008-06-27 12:38:18 UTC
looks like we should, yes.