Main Content

CWE Rule 474

Use of Function with Inconsistent Implementations

Since R2023a

Description

Rule Description

The code uses a function that has inconsistent implementations across operating systems and versions.

Polyspace Implementation

The rule checker checks for these issues:

  • Signal call from within signal handler

  • Use of obsolete standard function

Examples

expand all

Issue

This issue occurs when you call the function signal() from a signal handler on Windows® platforms.

The defect is detected only if you specify a Visual Studio compiler. See Compiler (-compiler).

Risk

The function signal() associates a signal with a signal handler function. On platforms such as Windows, which removes this association after receiving the signal, you might call the function signal() again within the signal handler to re-establish the association.

However, this attempt to make a signal handler persistent is prone to race conditions. On Windows platforms, from the time the signal handler begins execution to when the signal function is called again, it is the default signal handling, SIG_DFL, that is active. If a second signal is received within this time window, you see the default signal handling and not the custom signal handler, but you might expect otherwise.

Fix

Do not call signal() from a signal handler on Windows platforms.

Example — signal() Called from Signal Handler
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>



volatile sig_atomic_t e_flag = 0;

void sig_handler(int signum)
{
    int s0 = signum;
    e_flag = 1;
	
	/* Call signal() to reestablish sig_handler 
	upon receiving SIG_ERR. */
   
    if (signal(s0, sig_handler) == SIG_ERR)  //Noncompliant
    {
        /* Handle error */       
    }
}

void func(void)
{
        if (signal(SIGINT, sig_handler) == SIG_ERR)
        {
            /* Handle error */
            
        }
  /* more code */
}        
      

In this example, the definition of sig_handler() includes a call to signal() when the handler catches SIG_ERR. On Windows platforms, signal handlers are nonpersistent. This code can result in a race condition.

The issue is detected only if you specify a compiler such as visual15.x for the analysis.

Correction — Do Not Call signal() from Signal Handler

Avoid attempting to make a signal handler persistent on Windows. If your code requires the use of a persistent signal handler on a Windows platform, use a persistent signal handler after performing a thorough risk analysis.

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>



volatile sig_atomic_t e_flag = 0;


void sig_handler(int signum)
{
    int s0 = signum;
    e_flag = 1;
    /* No call to signal() */
}

int main(void)
{
    
        if (signal(SIGINT, sig_handler) == SIG_ERR)
        {
            /* Handle error */
            
        }
}
 
Issue

This issue occurs when you use standard function routines that are considered legacy, removed, deprecated, or obsolete by C/C++ coding standards.

Obsolete FunctionStandardsRiskReplacement Function
asctimeDeprecated in POSIX.1-2008Not thread-safe.strftime or asctime_s
asctime_rDeprecated in POSIX.1-2008Implementation based on unsafe function sprintf.strftime or asctime_s
bcmp

Deprecated in 4.3BSD

Marked as legacy in POSIX.1-2001.

Returns from function after finding the first differing byte, making it vulnerable to timing attacks. memcmp
bcopy

Deprecated in 4.3BSD

Marked as legacy in POSIX.1-2001.

Returns from function after finding the first differing byte, making it vulnerable to timing attacks. memcpy or memmove
brk and sbrkMarked as legacy in SUSv2 and POSIX.1-2001. malloc
bsd_signalRemoved in POSIX.1-2008 sigaction
bzeroMarked as legacy in POSIX.1-2001. Removed in POSIX.1-2008. memset
ctimeDeprecated in POSIX.1-2008Not thread-safe.strftime or asctime_s
ctime_rDeprecated in POSIX.1-2008Implementation based on unsafe function sprintf.strftime or asctime_s
cuseridRemoved in POSIX.1-2001.Not reentrant. Precise functionality not standardized causing portability issues.getpwuid
ecvt and fcvtMarked as legacy in POSIX.1-2001. Removed in POSIX.1-2008Not reentrantsnprintf
ecvt_r and fcvt_rMarked as legacy in POSIX.1-2001. Removed in POSIX.1-2008 snprintf
ftimeRemoved in POSIX.1-2008 time, gettimeofday, clock_gettime
gamma, gammaf, gammalFunction not specified in any standard because of historical variationsPortability issues.tgamma, lgamma
gcvtMarked as legacy in POSIX.1-2001. Removed in POSIX.1-2008. snprintf
getcontextRemoved in POSIX.1-2008.Portability issues. Use POSIX thread instead.
getdtablesizeBSD API function not included in POSIX.1-2001Portability issues.sysconf( _SC_OPEN_MAX )
gethostbyaddrRemoved in POSIX.1-2008Not reentrantgetaddrinfo
gethostbynameRemoved in POSIX.1-2008Not reentrantgetnameinfo
getpagesizeBSD API function not included in POSIX.1-2001Portability issues.sysconf( _SC_PAGESIZE )
getpassRemoved in POSIX.1-2001.Not reentrant.getpwuid
getwNot present in POSIX.1-2001. fread
getwdMarked legacy in POSIX.1-2001. Removed in POSIX.1-2008. getcwd
indexMarked as legacy in POSIX.1-2001. Removed in POSIX.1-2008. strchr
makecontextRemoved in POSIX.1-2008.Portability issues. Use POSIX thread instead.
memalignAppears in SunOS 4.1.3. Not in 4.4 BSD or POSIX.1-2001 posix_memalign
mktempRemoved in POSIX.1-2008.Generated names are predictable and can cause a race condition.mkstemp removes race risk
pthread_attr_getstackaddr and pthread_attr_setstackaddr Ambiguities in the specification of the stackaddr attribute cause portability issuespthread_attr_getstack and pthread_attr_setstack
putwNot present in POSIX.1-2001.Portability issues.fwrite
qecvt and qfcvtMarked as legacy in POSIX.1-2001, removed in POSIX.1-2008 snprintf
qecvt_r and qfcvt_rMarked as legacy in POSIX.1-2001, removed in POSIX.1-2008 snprintf
rand_rMarked as obsolete in POSIX.1-2008  
re_compBSD API functionPortability issuesregcomp
re_exesBSD API functionPortability issuesregexec
rindexMarked as legacy in POSIX.1-2001. Removed in POSIX.1-2008. strrchr
scalbRemoved in POSIX.1-2008 scalbln, scalblnf, or scalblnl
sigblock4.3BSD signal API whose origin is unclear sigprocmask
sigmask4.3BSD signal API whose origin is unclear sigprocmask
sigsetmask4.3BSD signal API whose origin is unclear sigprocmask
sigstackInterface is obsolete and not implemented on most platforms.Portability issues.sigaltstack
sigvec4.3BSD signal API whose origin is unclear sigaction
swapcontextRemoved in POSIX.1-2008Portability issues. Use POSIX threads.
tmpnam and tmpnam_rMarked as obsolete in POSIX.1-2008.This function generates a different string each time it is called, up to TMP_MAX times. If it is called more than TMP_MAX times, the behavior is implementation-defined. mkstemp, tmpfile
ttyslotRemoved in POSIX.1-2001.  
ualarmMarked as legacy in POSIX.1-2001. Removed in POSIX.1-2008.Errors are under-specifiedsetitimer or POSIX timer_create
usleepRemoved in POSIX.1-2008. nanosleep
utimeSVr4, POSIX.1-2001. POSIX.1-2008 marks as obsolete.  
valloc

Marked as obsolete in 4.3BSD.

Marked as legacy in SUSv2.

Removed from POSIX.1-2001

 posix_memalign
vfork

Removed from POSIX.1-2008

Under-specified in previous standards.fork
wcswcsThis function was not included in the final ISO/IEC 9899:1990/Amendment 1:1995 (E).  wcsstr
WinExecWinAPI provides this function only for 16-bit Windows compatibility. CreateProcess
LoadModuleWinAPI provides this function only for 16-bit Windows compatibility. CreateProcess
Fix

The fix depends on the root cause of the defect. See fixes in the table above and code examples with fixes below.

If you do not want to fix the issue, add comments to your result or code to avoid another review. See:

Example — Printing Out Time
#include <stdio.h>
#include <time.h> 

void timecheck_bad(int argc, char *argv[])
{
    time_t ticks; 

    ticks = time(NULL);
    printf("%.24s\r\n", ctime(&ticks));  //Noncompliant
}

In this example, the function ctime formats the current time and prints it out. However, ctime was removed after C99 because it does not work on multithreaded programs.

Correction — Different Time Function

One possible correction is to use strftime instead because this function uses a set buffer size.

#include <stdio.h>
#include <string.h>
#include <time.h> 

void timecheck_good(int argc, char *argv[])
{
    char outBuff[1025];
    time_t ticks; 
    struct tm * timeinfo;
    
    memset(outBuff, 0, sizeof(outBuff)); 
    
    ticks = time(NULL);
    timeinfo = localtime(&ticks);
    strftime(outBuff,sizeof(outBuff),"%I:%M%p.",timeinfo);
    fprintf(stdout, outBuff);
}

Check Information

Category: API / Function Errors

Version History

Introduced in R2023a