]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Avoid accessing elements of vectors beyond the end even if the result is not used...
authorWolfgang Bangerth <bangerth@math.tamu.edu>
Mon, 17 Mar 2008 05:39:51 +0000 (05:39 +0000)
committerWolfgang Bangerth <bangerth@math.tamu.edu>
Mon, 17 Mar 2008 05:39:51 +0000 (05:39 +0000)
git-svn-id: https://svn.dealii.org/trunk@15900 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/deal.II/source/grid/grid_generator.cc

index f365c61c9d060f600662fd8f9481b932e101135f..dae12f9c8595a58d67aa17569c5a49b57ca1ab8e 100644 (file)
@@ -591,9 +591,18 @@ subdivided_hyper_rectangle(Triangulation<dim>              &tria,
       case 1:
       {
        double x=0;
-       for (unsigned int i=0; i<=step_sizes[0].size(); ++i)
+       for (unsigned int i=0; ; ++i)
          { 
            points.push_back (Point<dim> (p1[0]+x));
+
+                                            // form partial sums. in
+                                            // the last run through
+                                            // avoid accessing
+                                            // non-existent values
+                                            // and exit early instead
+           if (i == step_sizes[0].size())
+             break;
+           
            x += step_sizes[0][i];
          }
        break;
@@ -602,15 +611,22 @@ subdivided_hyper_rectangle(Triangulation<dim>              &tria,
       case 2:
       {
        double y=0;
-       for (unsigned int j=0; j<=step_sizes[1].size(); ++j)
+       for (unsigned int j=0; ; ++j)
          {
            double x=0;
-           for (unsigned int i=0; i<=step_sizes[0].size(); ++i)
+           for (unsigned int i=0; ; ++i)
              {
                points.push_back (Point<dim> (p1[0]+x,
                                              p1[1]+y));
+               if (i == step_sizes[0].size())
+                 break;
+               
                x += step_sizes[0][i];
              }
+           
+           if (j == step_sizes[1].size())
+             break;
+           
            y += step_sizes[1][j];
          }
        break;
@@ -619,21 +635,32 @@ subdivided_hyper_rectangle(Triangulation<dim>              &tria,
       case 3:
       {
        double z=0;
-       for (unsigned int k=0; k<=step_sizes[2].size(); ++k)
+       for (unsigned int k=0; ; ++k)
          {
            double y=0;
-           for (unsigned int j=0; j<=step_sizes[1].size(); ++j)
+           for (unsigned int j=0; ; ++j)
              {
                double x=0;
-               for (unsigned int i=0; i<=step_sizes[0].size(); ++i)
+               for (unsigned int i=0; ; ++i)
                  {
                    points.push_back (Point<dim> (p1[0]+x,
                                                  p1[1]+y,
                                                  p1[2]+z));
+                   if (i == step_sizes[0].size())
+                     break;
+                   
                    x += step_sizes[0][i];
                  }
+
+               if (j == step_sizes[1].size())
+                 break;
+               
                y += step_sizes[1][j];
              }
+           
+           if (k == step_sizes[2].size())
+             break;
+           
            z += step_sizes[2][k];
          }
        break;

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.