Null pointers should not be dereferenced

A reference to null should never be dereferenced/accessed. Doing so will cause a NullPointerException to be thrown. At best, such an exception will cause abrupt program termination. At worst, it could expose debugging information that would be useful to an attacker, or it could allow an attacker to bypass security measures.

Note that when they are present, this rule takes advantage of @CheckForNull and @Nonnull annotations defined in JSR-305 to understand which values are and are not nullable.

@Nullable denotes that, under some unspecified circumstances, the value might be null. To keep false positives low, this annotation is ignored. Whether an explicit test is required or not is left to the developer‘s discretion.

Noncompliant Code Example

Here are some examples of null pointer dereferences detected by this rule:

@CheckForNull
String getName(){...}

public boolean isNameEmpty() {
  return getName().length() == 0; // Noncompliant; the result of getName() could be null, but isn‘t null-checked
}
Connection conn = null;
Statement stmt = null;
try{
  conn = DriverManager.getConnection(DB_URL,USER,PASS);
  stmt = conn.createStatement();
  // ...

}catch(Exception e){
  e.printStackTrace();
}finally{
  stmt.close();   // Noncompliant; stmt could be null if an exception was thrown in the try{} block
  conn.close();  // Noncompliant; conn could be null if an exception was thrown
}
private void merge(@Nonnull Color firstColor, @Nonnull Color secondColor){...}

public  void append(@CheckForNull Color color) {
    merge(currentColor, color);  // Noncompliant; color should be null-checked because merge(...) doesn‘t accept nullable parameters
}
void paint(Color color) {
  if(color == null) {
    System.out.println("Unable to apply color " + color.toString());  // Noncompliant; NullPointerException will be thrown
    return;
  }
  ...
}
时间: 2024-08-11 09:41:30

Null pointers should not be dereferenced的相关文章

Sonar 规则

bug类型: 1.".equals()" should not be used to test the values of "Atomic" classes. bug 主要 不要使用equals方法对AtomicXXX进行是否相等的判断 Atomic变量永远只会和自身相等,Atomic变量没有覆写equals()方法.2."=+" should not be used instead of "+=" bug 主要 "

Null Object Design Pattern (Python recipe)

Null Object 个人感觉非常有用.也是在review公司其他同事写代码的时候看到. 当时使用了flask的request全局请求变量g,然后使用了g.x保存了一个东西. 当时在view代码读取g.x的时候震惊了,因为这一段代码并没有保存g.x,按道理来说应该是一个空值,当我拿着空值去调用其属性的时候应该会报AttributeError. 但是什么也没有发生,既没有报错,也没有发生什么,而且对其判断还是False,于是查看其实现才发现了这个.以下全部转自http://code.active

推荐一个优秀的c++源代码,TinyXml2

项目主页:http://grinninglizard.com/tinyxml2docs/index.html tinyxml2.h /* Original code by Lee Thomason (www.grinninglizard.com) This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for an

C++11 之 nullptr

C++11 中, nullptr 是空指针,可用来给 (指向任意对象类型的) 指针进行赋值 广义整型 (integral types) = char, short, int, long, long longnd and their unsigned counterparts, and bool, wchar_t, char16_t, and char32_ 1  调用重载函数 0 在 C++ 中,被首先视为 int 型.NULL 在 C++ 中,首先视为广义整型 (integral types)

FindBugs规则整理

FindBugs规则整理 FindBugs是基于Bug Patterns概念,查找javabytecode(.class文件)中的潜在bug,主要检查bytecode中的bug patterns,如NullPoint空指针检查.没有合理关闭资源.字符串相同判断错(==,而不是equals)等 一.Security 关于代码安全性防护 1.Dm: Hardcoded constant database password (DMI_CONSTANT_DB_PASSWORD) 代码中创建DB的密码时采

区分const指针

  Read it backwards... int* - pointer to int int const * - pointer to const int int * const - const pointer to int int const * const - const pointer to const int Now the first const can be on either side of the type so: const int * == int const * con

Memory Leak Detection in Embedded Systems

One of the problems with developing embedded systems is the detection of memory leaks; I've found three tools that are useful for this. These tools are used to detect application program errors, not kernel memory leaks. Two of these tools (mtrace and

Ten C++11 Features Every C++ Developer Should Use

原版:http://www.codeproject.com/Articles/570638/Ten-Cplusplus-Features-Every-Cplusplus-Developer 译版:http://blogs.ejb.cc/archives/7190/top-10-new-features-you-should-know-about-c-11 This article discusses a series of features new to C++11 that all devel

CVE-2013-2094 porting to x86-32 分析

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86