LibRaw, Coverity SCAN, PVS-Studio 
Author: Andrey Karpov 
Date: 07.02.2014 
I read a post recently about a check of the LibRaw project performed by Coverity SCAN. It stated that 
nothing interesting had been found. So I decided to try our analyzer PVS-Studio on it. 
LibRaw 
LibRaw is a library for reading RAW-files obtained from digital photo cameras (CRW/CR2, NEF, RAF, 
DNG, and others). Website: http://www.libraw.org/ 
A check by Coverity SCAN 
The article that induced me to check the project with PVS-Studio can be found here: "On Static Analysis 
of C++" (RU). Let me quote a short extract from the preface part of the article: 
Coverity SCAN: 107 warnings, about a third of which are of High Impact level. 
Out of the High Impact level warnings: 
About 10 referring to Microsoft STL 
Others are of the following kind: 
int variable; 
if(layout==Layout1) variable=value1; 
if(layout==Layout2) variable=value2; 
On this code, I got a warning saying about carelessness in the code - an uninitialized variable. I agree 
with it as such, because it's no good doing things like that. But in real life, there are two types of layout - 
and this is explicitly specified in the calling code. That is, the analyzer has enough data to figure out that 
this is not a 'High impact' error but just a carelessly written code.
Some warnings like about unsigned short when extending to 32-64 bits may be painful. I won't argue 
with that - the analyzer is right de jure, but de facto these unsigned short's store picture sizes which are 
not going to grow up to 32767 in the near future. 
That is, again, no fixing is needed - in this particular case. 
All the rest 'High Impact' issues are simply false positives. Well, the code is not perfect, that's right (I wish 
you could see it from dcraw !), but all the issues found are not bugs. 
A check by PVS-Studio 
Now let's see if the PVS-Studio analyzer managed to find anything worthy after Coverity. I didn't have 
any expectations of catching super-bugs, of course, but it still was interesting to try. 
PVS-Studio generated 46 general warnings (of the first and second severity levels). 
Welcome to have a look at the code fragments I found interesting. 
Typos 
void DHT::hide_hots() { 
.... 
for (int k = -2; k < 3; k += 2) 
for (int m = -2; m < 3; m += 2) 
if (m == 0 && m == 0) 
continue; 
else 
avg += nraw[nr_offset(y + k, x + m)][kc]; 
.... 
} 
PVS-Studio's warning: V501 There are identical sub-expressions to the left and to the right of the '&&' 
operator: m == 0 && m == 0 dht_demosaic.cpp 260 
I guess we are dealing with a typo here. The check should probably look like this: 
if (k == 0 && m == 0) 
A similar fragment can be found in the file aahd_demosaic.cpp (line 199). 
Operation priorities 
int main(int argc, char *argv[]) 
{
int ret; 
.... 
if( (ret = RawProcessor.open_buffer(iobuffer,st.st_size) 
!= LIBRAW_SUCCESS)) 
{ 
fprintf(stderr,"Cannot open_buffer %s: %sn", 
argv[arg],libraw_strerror(ret)); 
free(iobuffer); 
continue; 
} 
.... 
} 
PVS-Studio's warning: V593 Consider reviewing the expression of the 'A = B != C' kind. The expression is 
calculated as following: 'A = (B != C)'. dcraw_emu.cpp 468 
This is a bug related to operation priorities. The "RawProcessor.open_buffer(iobuffer,st.st_size) != 
LIBRAW_SUCCESS" comparison is executed first, then its result is written into the 'ret' variable. If an 
error occurs, it will cause printing of an incorrect error code into the file. This is not a crucial defect, but 
it is still worth mentioning. 
Negative number shifts 
unsigned CLASS pana_bits (int nbits) 
{ 
.... 
return (buf[byte] | buf[byte+1] << 8) >> 
(vbits & 7) & ~(-1 << nbits); 
.... 
} 
PVS-Studio's warning: V610 Undefined behavior. Check the shift operator '<<. The left operand '-1' is 
negative. dcraw_common.cpp 1827 
Shifting negative numbers causes undefined behavior. Programmers often use such tricks and get the 
program pretend to work well. But actually you cannot rely on such a code. To learn more about 
negative number shifts, see the article "Wade not in unknown waters. Part three". 
Similar issues can be found in the following fragments:
• dcraw_common.cpp 1851 
• dcraw_common.cpp 2085 
• dcraw_common.cpp 2814 
• dcraw_common.cpp 6644 
Strange fragments 
void DHT::illustrate_dline(int i) { 
.... 
int l = ndir[nr_offset(y, x)] & 8; 
l >>= 3; 
l = 1; 
.... 
} 
PVS-Studio's warning: V519 The 'l' variable is assigned values twice successively. Perhaps this is a 
mistake. Check lines: 671, 672. dht_demosaic.cpp 672 
Perhaps this is not a bug and the "l = 1" statement is written deliberately. But the code does look 
suspicious. 
Here's one more suspicious piece of code: 
void CLASS identify() 
{ 
.... 
if (!load_raw && (maximum = 0xfff)) 
.... 
} 
PVS-Studio's warning: V560 A part of conditional expression is always true: ((imgdata.color.maximum) = 
0xfff). dcraw_common.cpp 8496 
Conclusion 
Both analyzers have found very few defects. It's natural for small projects. However, it was an 
interesting experiment checking LibRaw. By the way, our new lightweight analyzer CppCat ($250) would 
produce absolutely the same results as I left only first- and second-level warnings turned on in PVS-Studio 
for this check.

More Related Content

PDF
Analyzing the Dolphin-emu project
PDF
Rechecking TortoiseSVN with the PVS-Studio Code Analyzer
PDF
Picking Mushrooms after Cppcheck
PDF
Linux version of PVS-Studio couldn't help checking CodeLite
PDF
Dusting the globe: analysis of NASA World Wind project
PDF
The CppCat Analyzer Checks TortoiseGit
PDF
A Spin-off: CryEngine 3 SDK Checked with CppCat
PDF
Top 10 bugs in C++ open source projects, checked in 2016
Analyzing the Dolphin-emu project
Rechecking TortoiseSVN with the PVS-Studio Code Analyzer
Picking Mushrooms after Cppcheck
Linux version of PVS-Studio couldn't help checking CodeLite
Dusting the globe: analysis of NASA World Wind project
The CppCat Analyzer Checks TortoiseGit
A Spin-off: CryEngine 3 SDK Checked with CppCat
Top 10 bugs in C++ open source projects, checked in 2016

What's hot (20)

PDF
Checking OpenCV with PVS-Studio
PDF
100 bugs in Open Source C/C++ projects
PDF
Top 10 C# projects errors found in 2016
PDF
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
PDF
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
PDF
PVS-Studio vs Chromium - Continuation
PDF
A few words about OpenSSL
PDF
Intel IPP Samples for Windows - error correction
PDF
Intel IPP Samples for Windows - error correction
PDF
Waiting for the Linux-version: Checking the Code of Inkscape Graphics Editor
PDF
Analysis of the Trans-Proteomic Pipeline (TPP) project
PDF
How to find 56 potential vulnerabilities in FreeBSD code in one evening
PDF
Linux Kernel, tested by the Linux-version of PVS-Studio
PDF
CppCat Static Analyzer Review
PDF
Re-checking the ReactOS project - a large report
PDF
Mathematicians: Trust, but Verify
PDF
The Little Unicorn That Could
PDF
CppCat Checks OpenMW: Not All is Fine in the Morrowind Universe
PDF
Tesseract. Recognizing Errors in Recognition Software
PDF
Analysis of Godot Engine's Source Code
Checking OpenCV with PVS-Studio
100 bugs in Open Source C/C++ projects
Top 10 C# projects errors found in 2016
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
PVS-Studio vs Chromium - Continuation
A few words about OpenSSL
Intel IPP Samples for Windows - error correction
Intel IPP Samples for Windows - error correction
Waiting for the Linux-version: Checking the Code of Inkscape Graphics Editor
Analysis of the Trans-Proteomic Pipeline (TPP) project
How to find 56 potential vulnerabilities in FreeBSD code in one evening
Linux Kernel, tested by the Linux-version of PVS-Studio
CppCat Static Analyzer Review
Re-checking the ReactOS project - a large report
Mathematicians: Trust, but Verify
The Little Unicorn That Could
CppCat Checks OpenMW: Not All is Fine in the Morrowind Universe
Tesseract. Recognizing Errors in Recognition Software
Analysis of Godot Engine's Source Code
Ad

Viewers also liked (12)

PPTX
Queen Tiara
PPTX
Maravilhas
PDF
March 19 Letter from Will Morey
PPTX
PDF
Ambiance Ventures - Access to Emerging Markets
PDF
PPTX
Rúbrica de evaluación para la participación en foros
PDF
Study of 16S rDNA Sequence of S. Marcescens
DOCX
Journal_Internal conference John Walsh
PDF
Actividad colaborativa presentacion mirs (3)
PPTX
Exposicion arte en las culturas
Queen Tiara
Maravilhas
March 19 Letter from Will Morey
Ambiance Ventures - Access to Emerging Markets
Rúbrica de evaluación para la participación en foros
Study of 16S rDNA Sequence of S. Marcescens
Journal_Internal conference John Walsh
Actividad colaborativa presentacion mirs (3)
Exposicion arte en las culturas
Ad

Similar to LibRaw, Coverity SCAN, PVS-Studio (20)

PDF
Can We Trust the Libraries We Use?
PDF
Static analysis should be used regularly
PDF
A fresh eye on Oracle VM VirtualBox
PDF
Checking the code of Valgrind dynamic analyzer by a static analyzer
PDF
War of the Machines: PVS-Studio vs. TensorFlow
PDF
Checking Bitcoin
PDF
Analyzing the Blender project with PVS-Studio
PDF
Handling False Positives in PVS-Studio and CppCat
PDF
Checking PVS-Studio with Clang
PPTX
PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017
PDF
PVS-Studio delved into the FreeBSD kernel
PDF
Anomalies in X-Ray Engine
PDF
PVS-Studio for Linux Went on a Tour Around Disney
PDF
Errors that static code analysis does not find because it is not used
PDF
Checking the Source SDK Project
PDF
PVS-Studio and CppCat: An Interview with Andrey Karpov, the Project CTO and D...
PDF
HPX and PVS-Studio
PDF
Searching for bugs in Mono: there are hundreds of them!
PDF
A Unicorn Seeking Extraterrestrial Life: Analyzing SETI@home's Source Code
PDF
How to make fewer errors at the stage of code writing. Part N1
Can We Trust the Libraries We Use?
Static analysis should be used regularly
A fresh eye on Oracle VM VirtualBox
Checking the code of Valgrind dynamic analyzer by a static analyzer
War of the Machines: PVS-Studio vs. TensorFlow
Checking Bitcoin
Analyzing the Blender project with PVS-Studio
Handling False Positives in PVS-Studio and CppCat
Checking PVS-Studio with Clang
PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017
PVS-Studio delved into the FreeBSD kernel
Anomalies in X-Ray Engine
PVS-Studio for Linux Went on a Tour Around Disney
Errors that static code analysis does not find because it is not used
Checking the Source SDK Project
PVS-Studio and CppCat: An Interview with Andrey Karpov, the Project CTO and D...
HPX and PVS-Studio
Searching for bugs in Mono: there are hundreds of them!
A Unicorn Seeking Extraterrestrial Life: Analyzing SETI@home's Source Code
How to make fewer errors at the stage of code writing. Part N1

More from Andrey Karpov (20)

PDF
60 антипаттернов для С++ программиста
PDF
60 terrible tips for a C++ developer
PPTX
Ошибки, которые сложно заметить на code review, но которые находятся статичес...
PDF
PVS-Studio in 2021 - Error Examples
PDF
PVS-Studio in 2021 - Feature Overview
PDF
PVS-Studio в 2021 - Примеры ошибок
PDF
PVS-Studio в 2021
PPTX
Make Your and Other Programmer’s Life Easier with Static Analysis (Unreal Eng...
PPTX
Best Bugs from Games: Fellow Programmers' Mistakes
PPTX
Does static analysis need machine learning?
PPTX
Typical errors in code on the example of C++, C#, and Java
PPTX
How to Fix Hundreds of Bugs in Legacy Code and Not Die (Unreal Engine 4)
PPTX
Game Engine Code Quality: Is Everything Really That Bad?
PPTX
C++ Code as Seen by a Hypercritical Reviewer
PPTX
The Use of Static Code Analysis When Teaching or Developing Open-Source Software
PPTX
Static Code Analysis for Projects, Built on Unreal Engine
PPTX
Safety on the Max: How to Write Reliable C/C++ Code for Embedded Systems
PPTX
The Great and Mighty C++
PPTX
Static code analysis: what? how? why?
PDF
Zero, one, two, Freddy's coming for you
60 антипаттернов для С++ программиста
60 terrible tips for a C++ developer
Ошибки, которые сложно заметить на code review, но которые находятся статичес...
PVS-Studio in 2021 - Error Examples
PVS-Studio in 2021 - Feature Overview
PVS-Studio в 2021 - Примеры ошибок
PVS-Studio в 2021
Make Your and Other Programmer’s Life Easier with Static Analysis (Unreal Eng...
Best Bugs from Games: Fellow Programmers' Mistakes
Does static analysis need machine learning?
Typical errors in code on the example of C++, C#, and Java
How to Fix Hundreds of Bugs in Legacy Code and Not Die (Unreal Engine 4)
Game Engine Code Quality: Is Everything Really That Bad?
C++ Code as Seen by a Hypercritical Reviewer
The Use of Static Code Analysis When Teaching or Developing Open-Source Software
Static Code Analysis for Projects, Built on Unreal Engine
Safety on the Max: How to Write Reliable C/C++ Code for Embedded Systems
The Great and Mighty C++
Static code analysis: what? how? why?
Zero, one, two, Freddy's coming for you

Recently uploaded (20)

DOCX
Modern SharePoint Intranet Templates That Boost Employee Engagement in 2025.docx
PPTX
GSA Content Generator Crack (2025 Latest)
PDF
Multiverse AI Review 2025: Access All TOP AI Model-Versions!
PDF
Wondershare Recoverit Full Crack New Version (Latest 2025)
PPTX
Weekly report ppt - harsh dattuprasad patel.pptx
PDF
AI Guide for Business Growth - Arna Softech
PPTX
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
PDF
Time Tracking Features That Teams and Organizations Actually Need
PDF
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
PDF
iTop VPN Crack Latest Version Full Key 2025
PDF
The Dynamic Duo Transforming Financial Accounting Systems Through Modern Expe...
PPTX
Computer Software - Technology and Livelihood Education
PDF
Top 10 Software Development Trends to Watch in 2025 🚀.pdf
PPTX
Cybersecurity: Protecting the Digital World
PDF
Autodesk AutoCAD Crack Free Download 2025
PPTX
Patient Appointment Booking in Odoo with online payment
PDF
DNT Brochure 2025 – ISV Solutions @ D365
PPTX
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
PDF
DuckDuckGo Private Browser Premium APK for Android Crack Latest 2025
PDF
Microsoft Office 365 Crack Download Free
Modern SharePoint Intranet Templates That Boost Employee Engagement in 2025.docx
GSA Content Generator Crack (2025 Latest)
Multiverse AI Review 2025: Access All TOP AI Model-Versions!
Wondershare Recoverit Full Crack New Version (Latest 2025)
Weekly report ppt - harsh dattuprasad patel.pptx
AI Guide for Business Growth - Arna Softech
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
Time Tracking Features That Teams and Organizations Actually Need
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
iTop VPN Crack Latest Version Full Key 2025
The Dynamic Duo Transforming Financial Accounting Systems Through Modern Expe...
Computer Software - Technology and Livelihood Education
Top 10 Software Development Trends to Watch in 2025 🚀.pdf
Cybersecurity: Protecting the Digital World
Autodesk AutoCAD Crack Free Download 2025
Patient Appointment Booking in Odoo with online payment
DNT Brochure 2025 – ISV Solutions @ D365
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
DuckDuckGo Private Browser Premium APK for Android Crack Latest 2025
Microsoft Office 365 Crack Download Free

LibRaw, Coverity SCAN, PVS-Studio

  • 1. LibRaw, Coverity SCAN, PVS-Studio Author: Andrey Karpov Date: 07.02.2014 I read a post recently about a check of the LibRaw project performed by Coverity SCAN. It stated that nothing interesting had been found. So I decided to try our analyzer PVS-Studio on it. LibRaw LibRaw is a library for reading RAW-files obtained from digital photo cameras (CRW/CR2, NEF, RAF, DNG, and others). Website: http://www.libraw.org/ A check by Coverity SCAN The article that induced me to check the project with PVS-Studio can be found here: "On Static Analysis of C++" (RU). Let me quote a short extract from the preface part of the article: Coverity SCAN: 107 warnings, about a third of which are of High Impact level. Out of the High Impact level warnings: About 10 referring to Microsoft STL Others are of the following kind: int variable; if(layout==Layout1) variable=value1; if(layout==Layout2) variable=value2; On this code, I got a warning saying about carelessness in the code - an uninitialized variable. I agree with it as such, because it's no good doing things like that. But in real life, there are two types of layout - and this is explicitly specified in the calling code. That is, the analyzer has enough data to figure out that this is not a 'High impact' error but just a carelessly written code.
  • 2. Some warnings like about unsigned short when extending to 32-64 bits may be painful. I won't argue with that - the analyzer is right de jure, but de facto these unsigned short's store picture sizes which are not going to grow up to 32767 in the near future. That is, again, no fixing is needed - in this particular case. All the rest 'High Impact' issues are simply false positives. Well, the code is not perfect, that's right (I wish you could see it from dcraw !), but all the issues found are not bugs. A check by PVS-Studio Now let's see if the PVS-Studio analyzer managed to find anything worthy after Coverity. I didn't have any expectations of catching super-bugs, of course, but it still was interesting to try. PVS-Studio generated 46 general warnings (of the first and second severity levels). Welcome to have a look at the code fragments I found interesting. Typos void DHT::hide_hots() { .... for (int k = -2; k < 3; k += 2) for (int m = -2; m < 3; m += 2) if (m == 0 && m == 0) continue; else avg += nraw[nr_offset(y + k, x + m)][kc]; .... } PVS-Studio's warning: V501 There are identical sub-expressions to the left and to the right of the '&&' operator: m == 0 && m == 0 dht_demosaic.cpp 260 I guess we are dealing with a typo here. The check should probably look like this: if (k == 0 && m == 0) A similar fragment can be found in the file aahd_demosaic.cpp (line 199). Operation priorities int main(int argc, char *argv[]) {
  • 3. int ret; .... if( (ret = RawProcessor.open_buffer(iobuffer,st.st_size) != LIBRAW_SUCCESS)) { fprintf(stderr,"Cannot open_buffer %s: %sn", argv[arg],libraw_strerror(ret)); free(iobuffer); continue; } .... } PVS-Studio's warning: V593 Consider reviewing the expression of the 'A = B != C' kind. The expression is calculated as following: 'A = (B != C)'. dcraw_emu.cpp 468 This is a bug related to operation priorities. The "RawProcessor.open_buffer(iobuffer,st.st_size) != LIBRAW_SUCCESS" comparison is executed first, then its result is written into the 'ret' variable. If an error occurs, it will cause printing of an incorrect error code into the file. This is not a crucial defect, but it is still worth mentioning. Negative number shifts unsigned CLASS pana_bits (int nbits) { .... return (buf[byte] | buf[byte+1] << 8) >> (vbits & 7) & ~(-1 << nbits); .... } PVS-Studio's warning: V610 Undefined behavior. Check the shift operator '<<. The left operand '-1' is negative. dcraw_common.cpp 1827 Shifting negative numbers causes undefined behavior. Programmers often use such tricks and get the program pretend to work well. But actually you cannot rely on such a code. To learn more about negative number shifts, see the article "Wade not in unknown waters. Part three". Similar issues can be found in the following fragments:
  • 4. • dcraw_common.cpp 1851 • dcraw_common.cpp 2085 • dcraw_common.cpp 2814 • dcraw_common.cpp 6644 Strange fragments void DHT::illustrate_dline(int i) { .... int l = ndir[nr_offset(y, x)] & 8; l >>= 3; l = 1; .... } PVS-Studio's warning: V519 The 'l' variable is assigned values twice successively. Perhaps this is a mistake. Check lines: 671, 672. dht_demosaic.cpp 672 Perhaps this is not a bug and the "l = 1" statement is written deliberately. But the code does look suspicious. Here's one more suspicious piece of code: void CLASS identify() { .... if (!load_raw && (maximum = 0xfff)) .... } PVS-Studio's warning: V560 A part of conditional expression is always true: ((imgdata.color.maximum) = 0xfff). dcraw_common.cpp 8496 Conclusion Both analyzers have found very few defects. It's natural for small projects. However, it was an interesting experiment checking LibRaw. By the way, our new lightweight analyzer CppCat ($250) would produce absolutely the same results as I left only first- and second-level warnings turned on in PVS-Studio for this check.