Semmle 1.20
Skip to end of metadata
Go to start of metadata

Name: Use of potentially dangerous function

Description: Certain standard library functions are dangerous to call.

ID: cpp/potentially-dangerous-function

Kind: problem

Severity: error

Precision: high

Query: PotentiallyDangerousFunction.ql
 * @name Use of potentially dangerous function
 * @description Certain standard library functions are dangerous to call.
 * @kind problem
 * @problem.severity error
 * @precision high
 * @id cpp/potentially-dangerous-function
 * @tags reliability
 *       security
 *       external/cwe/cwe-242
import cpp

predicate potentiallyDangerousFunction(Function f, string message) {
    f.getQualifiedName() = "gmtime" and
    message = "Call to gmtime is potentially dangerous"
  ) or (
    f.getQualifiedName() = "gets" and
    message = "gets does not guard against buffer overflow"

from FunctionCall call, Function target, string message
  call.getTarget() = target and
  potentiallyDangerousFunction(target, message)
select call, message

This rule finds calls to functions that are dangerous to use. Currently, it checks for calls to gets and gmtime. See Related rules below for rules that identify other dangerous functions.

The gets function is one of the vulnerabilities exploited by the Internet Worm of 1988, one of the first computer worms to spread through the Internet. The gets function provides no way to limit the amount of data that is read and stored, so without prior knowledge of the input it is impossible to use it safely with any size of buffer.

The gmtime function fills data into a tm struct in shared memory and then returns a pointer to that struct. If the function is called from multiple places in the same program, and especially if it is called from multiple threads in the same program, then the calls will overwrite each other's data.


Replace calls to gets with fgets, specifying the maximum length to copy. This will prevent the buffer overflow.

Replace calls to gmtime with gmtime_r. With gmtime_r, the application code manages allocation of the tm struct. That way, separate calls to the function can use their own storage.


The following example checks the local time in two ways:

// BAD: using gmtime
int is_morning_bad() {
    const time_t now_seconds = time(NULL);
    struct tm *now = gmtime(&now_seconds);
    return (now->tm_hour < 12);

// GOOD: using gmtime_r
int is_morning_good() {
    const time_t now_seconds = time(NULL);
    struct tm now;
    gmtime_r(&now_seconds, &now);
    return (now.tm_hour < 12);

The first version uses gmtime, so it is vulnerable to its data being overwritten by another thread. Even if this code is not used in a multi-threaded context right now, future changes may make the program multi-threaded. The second version of the code uses gmtime_r. Since it allocates a new tm struct on every call, it is immune to other calls to gmtime or gmtime_r.

Related rules

Other dangerous functions identified by CWE-676 ("Use of Potentially Dangerous Function") include strcpy and strcat. Use of these functions is highlighted by rules for the following CWEs:

  • CWE-120 Classic Buffer Overflow
  • CWE-131 Incorrect Calculation of Buffer Size