Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 37 ยท Getting Started

Bugs, Vulnerabilities and Code Smells

Learn the issue types SonarQube reports, how severity and ratings work, and how a project gets its A to E reliability, security and maintainability grades.

Every finding in SonarQube is an issue of some kind, and the kind tells you how urgent it is and who should care. This lesson explains the classic categories (bug, vulnerability, code smell and security hotspot), then shows how the project-level ratings from A to E are derived from them.

Newer SonarQube versions describe issues with a Clean Code taxonomy (software qualities, clean code attributes and impact severities). The classic types still exist in the data model and in many reports, and the ratings below still work the same way. The newer vocabulary is covered in Clean Code Taxonomy.

The four classic types

Bug (reliability). Code that is demonstrably wrong or very likely to fail at runtime. Examples: dereferencing a value that may be null, comparing a string to itself, an infinite loop, a resource that is never closed. A bug is something you would fix even if no one has complained yet.

Java
String name = findUser(id);          // may return null
if (name.length() > 0) { ... }      // possible NullPointerException

Vulnerability (security). Code an attacker can exploit directly: SQL built by concatenating user input, a hard-coded password, disabled certificate validation.

Python
query = "SELECT * FROM users WHERE id = " + request.args["id"]   # injection

Security hotspot. Security-sensitive code that is not necessarily wrong. SonarQube cannot decide alone, so a person reviews it and marks it safe or fixes it. Examples: a random number generator, a regular expression, an HTTP cookie setting. Hotspots have their own review workflow and their own chapter later in this track.

Code smell (maintainability). Code that works today but makes future change harder: very long functions, deep nesting, duplicated blocks, unused variables, confusing names. Smells are the cost of maintaining the code.

Severity

Each issue also has a severity that expresses impact. In the classic scheme the levels were Blocker, Critical, Major, Minor and Info. In current versions, impact severities are High, Medium and Low (plus Info for some items), assigned per software quality. Either way, the rule is: fix the highest first, and treat anything on new code as a must-fix.

From issues to ratings

A project page shows three letter grades. They are computed from the issues, not guessed:

  • Reliability rating is the worst bug: A means no bugs; B at least one low-severity bug; C at least one medium; D at least one high; E at least one blocker-level bug.
  • Security rating works the same way using vulnerabilities.
  • Maintainability rating uses the technical debt ratio: estimated remediation time divided by the estimated time to write the code from scratch. A is up to 5%, B up to 10%, C up to 20%, D up to 50%, E above 50%.

The debt ratio assumes a cost per line of code (the default is 30 minutes of development per line). The remediation time of each smell comes from its rule, for example 5 minutes for a simple rename.

Python Compute the three ratings (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Try changing lines to 500. The same debt on a smaller project drops the grade, because debt is judged relative to size.

Classifying a rule

When you read a new rule, ask three questions: Will it break at runtime? (bug) Can someone attack it? (vulnerability) Does it only hurt readability or change cost? (smell). The next stand-in classifies sample findings with those questions, so you can see the logic written down.

Python Classify findings with three questions (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Working with issues in practice

  • Fix bugs and vulnerabilities on new code immediately; they are the reason the gate exists.
  • Do not chase 100% smell removal. Smells are guidance, and some rules may not suit your team (you can turn rules off in a quality profile, see Rules and Quality Profiles).
  • Use the rule description: every rule has Why is this an issue?, How can I fix it? and compliant and non-compliant examples.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which issue type is code that is likely to fail at runtime?

    A bug is code that is wrong or very likely to produce a failure or wrong result.
  2. 2. Why is a security hotspot different from a vulnerability?

    Hotspots are sensitive code that may or may not be safe; a reviewer decides.
  3. 3. Your project has one medium-severity bug and no other bugs. What is the reliability rating?

    The rating follows the worst bug: low gives B, medium gives C, high gives D, blocker gives E.
  4. 4. What determines the maintainability rating?

    It is based on remediation time compared with the estimated cost of developing the code.

Summary

  • Bugs threaten correctness, vulnerabilities threaten security, hotspots need a human decision, smells hurt maintainability.
  • Severity says how urgent an issue is.
  • Reliability and security ratings follow the worst issue; maintainability follows the technical debt ratio (A up to 5%).
  • Judge a rule by its description and examples, and adjust the profile if it does not fit.

Next: Editions and Hosting