Install Infer Static Analysis Tool on Ubuntu 24.04

Install Infer Static Analysis Tool on Ubuntu 24.04

Infer is a static analysis tool for Java, C, C++, Objective-C code. It analyzes code without running it (i.e., statically) and is particularly known for catching null pointer exceptions, memory leaks, and concurrency issues. This tutorial explains how to install Infer static analysis tool on Ubuntu 24.04.

Install Infer

Fetch the latest Infer release tag from GitHub and store it in a variable:

INFER_VERSION=$(curl -s "https://api.github.com/repos/facebook/infer/releases/latest" | grep -Po '"tag_name": "v\K[0-9.]+')

Download Infer from GitHub repository:

wget -qO infer.tar.xz https://github.com/facebook/infer/releases/latest/download/infer-linux-x86_64-v${INFER_VERSION}.tar.xz

Make a new directory for Infer, and unpack the tar.gz file into it:

sudo mkdir /opt/infer
sudo tar xf infer.tar.xz --strip-components=1 -C /opt/infer

Add /opt/infer/bin directory to the PATH environment variable:

echo 'export PATH=$PATH:/opt/infer/bin' | sudo tee -a /etc/profile.d/infer.sh

Apply the changes by logging out and logging back in, or run this command to activate them right away:

source /etc/profile

After completing the installation, confirm the Infer version by running the following command:

infer --version

Remove unnecessary archive file:

rm -rf infer.tar.xz

Testing Infer

Consider the following code written in main.c:

#include <stdio.h>

int main() {
    int *val = NULL;
    *val = 100;

    return 0;
}

This code intentionally causes a null pointer dereference to test Infer ability to detect such issues.

Run Infer with the following command:

infer run -- gcc -c main.c

Infer will analyze the file and report a null dereference error, as shown below:

Capturing in make/cc mode...
Found 1 source file to analyze in /home/ubuntu/infer-out
2/2 [###################################################################] 100% 130ms

main.c:5: error: Null Dereference
  `val` could be null (null value originating from line 4) and is dereferenced. 
  3. int main() {
  4.     int *val = NULL;
  5.     *val = 100;
         ^
  6. 
  7.     return 0;


Found 1 issue
             Issue Type(ISSUED_TYPE_ID): #
  Null Dereference(NULLPTR_DEREFERENCE): 1

Uninstall Infer

If you want to remove Infer entirely, simply delete the directory where it was installed:

sudo rm -rf /opt/infer

Delete the environment configuration script:

sudo rm -rf /etc/profile.d/infer.sh

Leave a Comment

Cancel reply

Your email address will not be published.