Bug 1388

Summary: LTE module doesn't build in optimized mode with GCC 4.7
Product: ns-3 Reporter: Vedran Miletić <vedran>
Component: lteAssignee: Giuseppe Piro <peppe>
Status: RESOLVED FIXED    
Severity: normal CC: ns-bugs
Priority: P5    
Version: pre-release   
Hardware: All   
OS: Linux   
Attachments: Introduce break in while loop

Description Vedran Miletić 2012-03-08 18:59:35 UTC
Created attachment 1352 [details]
Introduce break in while loop

[ 738/1225] cxx: src/lte/model/amc-module.cc -> build/optimized/src/lte/model/amc-module.cc.1.o
../../src/lte/model/amc-module.cc: In member function 'int ns3::AmcModule::GetCqiFromSpectralEfficiency(double)':
../../src/lte/model/amc-module.cc:123:43: error: array subscript is above array bounds [-Werror=array-bounds]
cc1plus: all warnings being treated as errors

To the best of my understanding, this patch produces functionality wise eqivalent code that builds with GCC 4.7. All tests pass.
Comment 1 Vedran Miletić 2012-03-15 08:33:16 UTC
Giuseppe, is this code OK for inclusion?
Comment 2 Giuseppe Piro 2012-03-15 10:35:59 UTC
The patch is OK for me.
Comment 3 Tommaso Pecorella 2012-03-15 18:13:15 UTC
I'm not happy with the patch... sorry if I'm pedantic.

The problem seems to be more subtle, and silencing the compiler by obfuscating the code isn't very wise. I have to confess that the compiler is smart indeed, and it found an obscure vector boundary issue.

  while (SpectralEfficiencyForCqiIndex[cqi] < s && cqi <= 14)
  { [...] }
Problem: the check is done sequentially (it's an and), so when cqi is 15 the first vector will go out of boundaries. The second check will return false and the while will stop.

A smarter solution is to invert the checks.
  while ( cqi <= 14 && SpectralEfficiencyForCqiIndex[cqi] < s )
  { [...] }

This should make the compiler happy again. I can't check, but if the error goes away, this is way cleaner.

Cheers,

T.
Comment 4 Vedran Miletić 2012-03-16 12:39:09 UTC
Tommaso, you are right, just tested it and it works. Talk about commutativity of && operation.

While at being pedantic about this while (pun intended), let's go a bit further and make it like:

  while (cqi < 15 && SpectralEfficiencyForCqiIndex[cqi] < s)

which is somewhat more C++ style.

Whichever way, let's commit this.
Comment 5 Tommaso Pecorella 2012-03-16 16:08:30 UTC
+1, go ahead.

T.
Comment 6 Vedran Miletić 2012-03-18 15:45:21 UTC
changeset:   7768:7d43ccc63a96
tag:         tip
user:        Vedran Miletić <rivanvx@gmail.com>
date:        Sun Mar 18 20:43:18 2012 +0100
summary:     Bug 1388 - LTE module doesn't build in optimized mode with GCC 4.7