Skip to main content

Improper resource allocation - Memory leak

Need

Proper resource management and deallocation

Context

  • Usage of Go 1.16 for building high-performance and scalable applications
  • Usage of gin-gonic/gin for building web applications and APIs in Go
  • Usage of net/http for creating HTTP servers in a Node.js application

Description

Non compliant code

package main

import (
"github.com/gin-gonic/gin"
"net/http"
)

func main() {
r := gin.Default()
r.GET("/leaky-endpoint", func(c *gin.Context) {
data := make([]byte, 1024)
c.Writer.Write(data)
})
r.Run()
}

In the above Go code snippet, we have a Gin web server with a single endpoint "/leaky-endpoint". When a GET request is made to this endpoint, it creates a new byte array of size 1024 and writes it to the response without properly releasing the memory.

The data byte array is created for each request to the "/leaky-endpoint" but never explicitly released. This is a memory leak because the memory allocated for data is not being deallocated after it's used, causing the application to consume more and more memory over time as more requests are made to the "/leaky-endpoint". This could potentially lead to the application running out of memory and crashing, especially under high load.

This is a classic example of a memory leak caused by improper resource allocation and deallocation. It's important to always release any resources that are no longer needed to prevent memory leaks and other related issues. In Go, this is typically done using the defer statement, which ensures that a function call is made later in a program's execution, usually for purposes of cleanup.

Steps

  • Identify all resources that need to be explicitly closed, such as network connections, file handles, or database connections.
  • Use defer statements or explicit close() calls to ensure that resources are properly released.
  • In the given code, the resource that needs to be closed is the Writer object obtained from the gin.Context. Use the defer statement to close the Writer after writing the data.
  • Modify the code to add a defer statement to close the Writer object: defer c.Writer.Close().
  • Test the modified code to ensure that the resources are properly released and there are no memory leaks.

Compliant code

package main

import (
"github.com/gin-gonic/gin"
"net/http"
"io"
)

func main() {
r := gin.Default()
r.GET("/leaky-endpoint", func(c *gin.Context) {
data := make([]byte, 1024)
c.Writer.Write(data)
defer c.Writer.Close()
})
r.Run()
}

The original code had a memory leak vulnerability due to the lack of explicit closure of the Writer object obtained from the gin.Context. This could potentially lead to the system not being able to release inbound and outbound flow resources that have been opened, causing a memory leak.

The fixed code addresses this issue by adding a defer statement to close the Writer object after writing the data. The defer keyword in Go ensures that the function call it precedes is executed just before the function where the defer statement is present completes its execution. This means that even if the function encounters an error or returns early, the defer statement will still be executed, ensuring that the Writer object is properly closed and the resources are released.

In the fixed code, after the data is written to the Writer object with c.Writer.Write(data), the Writer object is closed with defer c.Writer.Close(). This ensures that the Writer object is properly closed and the resources are released, preventing a memory leak.

It's important to test the modified code to ensure that the resources are properly released and there are no memory leaks.

References