SDA SE Wiki

Software Engineering for Smart Data Analytics & Smart Data Analytics for Software Engineering

User Tools

Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
teaching:labs:mdse:2013:bug_descriptions:jt-bug-cor16 [2017/06/29 06:52]
Günter Kniesel
teaching:labs:mdse:2013:bug_descriptions:jt-bug-cor16 [2018/05/09 01:59] (current)
Line 1: Line 1:
  
 +====== Bug Cor016 ======
 +
 +/*Replace all placeholder words (written with underscores) by their suitable counterparts in the context of your Bug.*/
 +
 +===== Inspired by =====
 +
 +  * [[http://​findbugs.sourceforge.net/​bugDescriptions.html#​DLS_OVERWRITTEN_INCREMENT | DLS_OVERWRITTEN_INCREMENT
 +
 +]]
 +
 +===== Original Description =====
 +
 +"The code performs an increment operation (e.g., i++) and then immediately overwrites it. For example, i = i++ immediately overwrites the incremented value with the original value. ​ " -- 2013-10-14 ​
 +
 +
 +===== Detailed Description =====
 +
 +Java offers pre- and postfix incrementation operators ++i and i++ that can be used to shorten the code. In the case of prefix operators the value of the variable is changed immediately. A postfix operator on the other hand processes the unchanged value of the variable and updates it afterwards. E.g. a=i++; results in a being assigned i and i getting incremented right after. Curiously in Java i=i++ behaves in the same way; i is assigned i. Obviously this behavior is not intuitive since one would expect the incrementation to happen.
 +
 +This comes from the internal implementation of the operator. The value is changed immediately while the old one gets remembered and used to process the term. Hence i gets incremented at first but then the old remembered value is assigned right afterwards.
 + 
 +
 +===== Sample Problem Scenario =====
 +
 +The problem must be detected no matter what type of variable is incremented (field, local var, or parameter):
 +
 +<code Java>
 +public class OverwrittenIncrement {
 +
 + private int f;
 +        public void overwrittenIncrementOfField() {
 +               f = f++;
 +        }
 +        ​
 +        public int overwrittenIncrementOfLocalVar() {
 +        int l = 42;
 +        l = l++;
 +               ​return l;
 +        }
 +        ​
 +        public int overwrittenIncrementOfParameter(int p) {
 +        p = p++;
 +               ​return p;
 +        }
 +}
 +</​Code>​
 +
 +===== Sample Fix =====
 +
 +The solution is straightforward. Replace the nonsense expression i=i++ by i++.
 +
 +<code Java>
 +public class OverwrittenIncrement {
 +
 + private int f;
 +        public void overwrittenIncrementOfField() {
 +               f++;
 +        }
 +        ​
 +        public int overwrittenIncrementOfLocalVar() {
 +        int l = 42;
 +        l++;
 +               ​return l;
 +        }
 +        ​
 +        public int overwrittenIncrementOfParameter(int p) {
 +        p++;
 +               ​return p;
 +        }
 +}
 +</​Code>​
 +
 +/*
 +===== Evaluation Results =====
 +
 +^  Benchmark project ​ ^  Precision ​             ^^^  Recall ​               ^^^
 +|                     ​| ​ FB    |  JT    |  Delta  |  FB    |  JT    |  Delta |
 +|  Project ...        |  ...%  |  ...%  |  ...%   ​| ​ ...%  |  ...%  |  ...%  |  ​
 +|  Project ...        |  ...%  |  ...%  |  ...%   ​| ​ ...%  |  ...%  |  ...%  |   
 +FB = FindBugs, JT = JTransformer,​ Delta = JTransformer - FindBugs
 +*/
 + 
teaching/labs/mdse/2013/bug_descriptions/jt-bug-cor16.txt · Last modified: 2018/05/09 01:59 (external edit)

SEWiki, © 2019