Developers relax restrictions on a type to reuse methods with other types. While type casts are prevalent, in weakly typed languages such as C++, they are also extremely permissive. Assignments where a source expression is cast into a new type and assigned to a target variable of the new type, can lead to software bugs if performed without care. In this paper, we propose an information-theoretic approach to identify poor implementations of explicit cast operations. Our approach measures accord between the source expression and the target variable using conditional entropy. We collect casts from 34 components of the Chromium project, which collectively account for 27MLOC and random-uniformly sample this dataset to create a manually labelled dataset of 271 casts. Information-theoretic vetting of these 271 casts achieves a peak precision of 81% and a recall of 90%. We additionally present the findings of an in-depth investigation of notable explicit casts, two of which were fixed in recent releases of the Chromium project.
翻译:开发者通过放宽类型的限制来复用其他类型的方法。类型转换在弱类型语言(如C++)中普遍存在,且极为宽松。若不加谨慎地将源表达式转换为新类型并赋值给该新类型的目标变量,可能导致软件缺陷。本文提出一种基于信息论的方法来识别显式转换操作中的不良实现。该方法利用条件熵衡量源表达式与目标变量之间的一致性。我们从Chromium项目的34个组件中收集转换操作(总计2700万行代码),并随机均匀采样该数据集,构建了一个包含271个转换的人工标注数据集。对这271个转换进行信息论审查后,达到81%的峰值精确率和90%的召回率。此外,我们深入调查了若干值得关注的显式转换案例,其中两个已在Chromium项目的最新版本中修复。