The most popular example of Java code on StackOverflow turned out to have an error.

The Most Popular Example of Java Code, published on StackOverflow, turned out to be with an error that leads to incorrect output under certain conditions. The code in question was posted in 2010 and has gained over a thousand recommendations, and has also been copied into many projects and appears in repositories on GitHub about 7 thousand times. Notably, the error was found not by users copying this code into their projects, but by the original author of the advice.

The code in question converted byte sizes into a readable format, for example, converting 110592 into ‘110.6 kB’ or ‘108.0 KiB’. The code was proposed as an optimized version using logarithms of an earlier suggestion where the size was determined based on sequentially dividing the original value in a loop by 1018, 1015, 1012, 1019,
106, 103, and 100, until the divisor is greater than the original value in bytes. Due to inaccurate calculations in the optimized version (overflow of the long value), the result when processing very large numbers (exabytes) was incorrect.

The author of the advice also tried to highlight the issue of copying examples without attribution and without indicating the license. According to a previously conducted study , 46% of developers copied code from StackOverflow without indicating the author, 75% did not know that the code was distributed under the CC BY-SA license, and 67% were unaware that this implied the necessity to credit the author.

According to data According to another study, copying code examples is associated not only with the risk of containing errors in the code but also vulnerabilities. For instance, analyzing 72,483 code examples in C++ on StackOverflow, researchers uncovered serious vulnerabilities in 69 examples (which makes up 0.09%) that are among the most popular recommendations. Further analysis of the code on GitHub revealed that the vulnerable code copied from StackOverflow is present in 2,859 projects.

Source: opennet.ru

Buy reliable website hosting with DDoS protection, VPS VDS servers 🔥 Buy reliable website hosting with DDoS protection, VPS VDS servers | ProHoster