2019-11-15 21:38:20 +01:00
|
|
|
#!/usr/bin/env python3
|
2015-12-07 19:54:41 +01:00
|
|
|
#
|
|
|
|
# cppcheck addon for Y2038 safeness detection
|
|
|
|
#
|
|
|
|
# Detects:
|
|
|
|
#
|
|
|
|
# 1. _TIME_BITS being defined to something else than 64 bits
|
|
|
|
# 2. _USE_TIME_BITS64 being defined when _TIME_BITS is not
|
2016-06-29 09:17:50 +02:00
|
|
|
# 3. Any Y2038-unsafe symbol when _USE_TIME_BITS64 is not defined.
|
2015-12-07 19:54:41 +01:00
|
|
|
#
|
|
|
|
# Example usage:
|
2019-07-26 06:38:03 +02:00
|
|
|
# $ cppcheck --dump path-to-src/test.c
|
|
|
|
# $ y2038.py path-to-src/test.c.dump
|
2015-12-07 19:54:41 +01:00
|
|
|
#
|
|
|
|
# y2038.py will walk the source tree for .dump files.
|
2019-07-26 06:38:03 +02:00
|
|
|
from __future__ import print_function
|
2015-12-07 19:54:41 +01:00
|
|
|
|
2019-12-30 09:13:04 +01:00
|
|
|
from . import cppcheckdata
|
2015-12-07 19:54:41 +01:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
2017-06-04 22:51:48 +02:00
|
|
|
# --------------------------------------------
|
2015-12-07 19:54:41 +01:00
|
|
|
# #define/#undef detection regular expressions
|
2017-06-04 22:51:48 +02:00
|
|
|
# --------------------------------------------
|
2015-12-07 19:54:41 +01:00
|
|
|
|
|
|
|
# test for '#define _TIME_BITS 64'
|
|
|
|
re_define_time_bits_64 = re.compile(r'^\s*#\s*define\s+_TIME_BITS\s+64\s*$')
|
|
|
|
|
|
|
|
# test for '#define _TIME_BITS ...' (combine w/ above to test for 'not 64')
|
|
|
|
re_define_time_bits = re.compile(r'^\s*#\s*define\s+_TIME_BITS\s+.*$')
|
|
|
|
|
|
|
|
# test for '#undef _TIME_BITS' (if it ever happens)
|
|
|
|
re_undef_time_bits = re.compile(r'^\s*#\s*undef\s+_TIME_BITS\s*$')
|
|
|
|
|
|
|
|
# test for '#define _USE_TIME_BITS64'
|
|
|
|
re_define_use_time_bits64 = re.compile(r'^\s*#\s*define\s+_USE_TIME_BITS64\s*$')
|
|
|
|
|
|
|
|
# test for '#undef _USE_TIME_BITS64' (if it ever happens)
|
|
|
|
re_undef_use_time_bits64 = re.compile(r'^\s*#\s*undef\s+_USE_TIME_BITS64\s*$')
|
|
|
|
|
2017-06-04 22:51:48 +02:00
|
|
|
# --------------------------------
|
2016-06-29 09:17:50 +02:00
|
|
|
# List of Y2038-unsafe identifiers
|
2017-06-04 22:51:48 +02:00
|
|
|
# --------------------------------
|
2015-12-07 19:54:41 +01:00
|
|
|
|
|
|
|
# This is WIP. Eventually it should contain all identifiers (types
|
|
|
|
# and functions) which would be affected by the Y2038 bug.
|
|
|
|
|
2017-06-04 22:51:48 +02:00
|
|
|
id_Y2038 = {
|
2016-06-29 09:17:50 +02:00
|
|
|
# Y2038-unsafe types by definition
|
|
|
|
'time_t'
|
|
|
|
# Types using Y2038-unsafe types
|
|
|
|
'lastlog',
|
|
|
|
'msqid_ds',
|
|
|
|
'semid_ds',
|
|
|
|
'timeb',
|
2015-12-07 19:54:41 +01:00
|
|
|
'timespec',
|
2016-06-29 09:17:50 +02:00
|
|
|
'timeval',
|
|
|
|
'utimbuf',
|
|
|
|
'itimerspec',
|
|
|
|
'stat',
|
|
|
|
'clnt_ops',
|
|
|
|
'elf_prstatus',
|
|
|
|
'itimerval',
|
|
|
|
'ntptimeval',
|
|
|
|
'rusage',
|
|
|
|
'timex',
|
|
|
|
'utmp',
|
|
|
|
'utmpx',
|
|
|
|
# APIs using 2038-unsafe types
|
|
|
|
'ctime',
|
|
|
|
'ctime_r',
|
|
|
|
'difftime',
|
|
|
|
'gmtime',
|
|
|
|
'gmtime_r',
|
|
|
|
'localtime',
|
|
|
|
'localtime_r',
|
|
|
|
'mktime',
|
|
|
|
'stime',
|
|
|
|
'timegm',
|
|
|
|
'timelocal',
|
|
|
|
'time',
|
|
|
|
'msgctl',
|
|
|
|
'ftime',
|
|
|
|
'aio_suspend',
|
|
|
|
'clock_getres',
|
|
|
|
'clock_gettime',
|
|
|
|
'clock_nanosleep',
|
|
|
|
'clock_settime',
|
|
|
|
'futimens',
|
|
|
|
'mq_timedreceive',
|
|
|
|
'mq_timedsend',
|
|
|
|
'nanosleep',
|
|
|
|
'pselect',
|
|
|
|
'pthread_cond_timedwait',
|
|
|
|
'pthread_mutex_timedlock',
|
|
|
|
'pthread_rwlock_timedrdlock',
|
|
|
|
'pthread_rwlock_timedwrlock',
|
|
|
|
'sched_rr_get_interval',
|
|
|
|
'sem_timedwait',
|
|
|
|
'sigtimedwait',
|
|
|
|
'timespec_get',
|
|
|
|
'utimensat',
|
|
|
|
'adjtime',
|
|
|
|
'pmap_rmtcall',
|
|
|
|
'clntudp_bufcreate',
|
|
|
|
'clntudp_create',
|
|
|
|
'futimes',
|
|
|
|
'gettimeofday',
|
|
|
|
'lutimes',
|
|
|
|
'select',
|
|
|
|
'settimeofday',
|
|
|
|
'utimes',
|
|
|
|
'utime',
|
|
|
|
'timerfd_gettime',
|
|
|
|
'timerfd_settime',
|
|
|
|
'timer_gettime',
|
|
|
|
'timer_settime',
|
|
|
|
'fstatat',
|
|
|
|
'fstat',
|
|
|
|
'__fxstatat',
|
|
|
|
'__fxstat',
|
|
|
|
'lstat',
|
|
|
|
'__lxstat',
|
|
|
|
'stat',
|
|
|
|
'__xstat',
|
|
|
|
'struct itimerval',
|
|
|
|
'setitimer',
|
|
|
|
'getitimer',
|
|
|
|
'ntp_gettime',
|
|
|
|
'getrusage',
|
|
|
|
'wait3',
|
|
|
|
'wait4',
|
|
|
|
'adjtimex',
|
|
|
|
'ntp_adjtime',
|
|
|
|
'getutent_r',
|
|
|
|
'getutent',
|
|
|
|
'getutid_r',
|
|
|
|
'getutid',
|
|
|
|
'getutline_r',
|
|
|
|
'getutline',
|
|
|
|
'login',
|
|
|
|
'pututline',
|
|
|
|
'updwtmp',
|
|
|
|
'getutxent',
|
|
|
|
'getutxid',
|
|
|
|
'getutxline',
|
|
|
|
'pututxline'
|
2017-06-04 22:51:48 +02:00
|
|
|
}
|
2015-12-07 19:54:41 +01:00
|
|
|
|
|
|
|
|
2019-07-26 06:38:03 +02:00
|
|
|
def check_y2038_safe(dumpfile, quiet=False):
|
2019-12-03 18:49:11 +01:00
|
|
|
# Assume that the code is Y2038 safe until proven otherwise
|
|
|
|
y2038safe = True
|
2015-12-07 19:54:41 +01:00
|
|
|
# load XML from .dump file
|
2019-12-27 08:50:56 +01:00
|
|
|
data = cppcheckdata.CppcheckData(dumpfile)
|
2019-07-26 06:38:03 +02:00
|
|
|
|
|
|
|
# Convert dump file path to source file in format generated by cppcheck.
|
|
|
|
# For example after the following call:
|
|
|
|
# cppcheck ./src/my-src.c --dump
|
|
|
|
# We got 'src/my-src.c' value for 'file' field in cppcheckdata.
|
|
|
|
srcfile = dumpfile.rstrip('.dump')
|
|
|
|
srcfile = os.path.expanduser(srcfile)
|
|
|
|
srcfile = os.path.normpath(srcfile)
|
|
|
|
|
2015-12-07 19:54:41 +01:00
|
|
|
# go through each configuration
|
2019-12-27 08:50:56 +01:00
|
|
|
for cfg in data.iterconfigurations():
|
2019-07-26 06:38:03 +02:00
|
|
|
if not quiet:
|
2019-12-27 08:50:56 +01:00
|
|
|
print('Checking %s, config %s...' % (srcfile, cfg.name))
|
2015-12-07 19:54:41 +01:00
|
|
|
safe_ranges = []
|
|
|
|
safe = -1
|
|
|
|
time_bits_defined = False
|
2017-04-19 06:46:53 +02:00
|
|
|
srclinenr = '0'
|
2019-07-26 06:38:03 +02:00
|
|
|
|
2015-12-07 19:54:41 +01:00
|
|
|
for directive in cfg.directives:
|
|
|
|
# track source line number
|
|
|
|
if directive.file == srcfile:
|
|
|
|
srclinenr = directive.linenr
|
|
|
|
# check for correct _TIME_BITS if present
|
|
|
|
if re_define_time_bits_64.match(directive.str):
|
|
|
|
time_bits_defined = True
|
|
|
|
elif re_define_time_bits.match(directive.str):
|
2019-07-26 06:38:03 +02:00
|
|
|
cppcheckdata.reportError(directive, 'error',
|
|
|
|
'_TIME_BITS must be defined equal to 64',
|
|
|
|
'y2038',
|
|
|
|
'type-bits-not-64')
|
2015-12-07 19:54:41 +01:00
|
|
|
time_bits_defined = False
|
2019-07-26 06:38:03 +02:00
|
|
|
y2038safe = False
|
2015-12-07 19:54:41 +01:00
|
|
|
elif re_undef_time_bits.match(directive.str):
|
|
|
|
time_bits_defined = False
|
|
|
|
# check for _USE_TIME_BITS64 (un)definition
|
|
|
|
if re_define_use_time_bits64.match(directive.str):
|
|
|
|
safe = int(srclinenr)
|
|
|
|
# warn about _TIME_BITS not being defined
|
2017-06-04 22:51:48 +02:00
|
|
|
if not time_bits_defined:
|
2019-07-26 06:38:03 +02:00
|
|
|
cppcheckdata.reportError(directive, 'warning',
|
|
|
|
'_USE_TIME_BITS64 is defined but _TIME_BITS was not',
|
|
|
|
'y2038',
|
|
|
|
'type-bits-undef')
|
2015-12-07 19:54:41 +01:00
|
|
|
elif re_undef_use_time_bits64.match(directive.str):
|
|
|
|
unsafe = int(srclinenr)
|
|
|
|
# do we have a safe..unsafe area?
|
2017-07-22 11:05:50 +02:00
|
|
|
if unsafe > safe > 0:
|
2015-12-07 19:54:41 +01:00
|
|
|
safe_ranges.append((safe, unsafe))
|
|
|
|
safe = -1
|
2019-07-26 06:38:03 +02:00
|
|
|
|
2015-12-07 19:54:41 +01:00
|
|
|
# check end of source beyond last directive
|
|
|
|
if len(cfg.tokenlist) > 0:
|
|
|
|
unsafe = int(cfg.tokenlist[-1].linenr)
|
2017-07-22 11:05:50 +02:00
|
|
|
if unsafe > safe > 0:
|
2015-12-07 19:54:41 +01:00
|
|
|
safe_ranges.append((safe, unsafe))
|
2019-07-26 06:38:03 +02:00
|
|
|
|
2015-12-07 19:54:41 +01:00
|
|
|
# go through all tokens
|
|
|
|
for token in cfg.tokenlist:
|
|
|
|
if token.str in id_Y2038:
|
|
|
|
if not any(lower <= int(token.linenr) <= upper
|
|
|
|
for (lower, upper) in safe_ranges):
|
2019-07-26 06:38:03 +02:00
|
|
|
cppcheckdata.reportError(token, 'warning',
|
|
|
|
token.str + ' is Y2038-unsafe',
|
|
|
|
'y2038',
|
|
|
|
'unsafe-call')
|
|
|
|
y2038safe = False
|
2015-12-07 19:54:41 +01:00
|
|
|
token = token.next
|
|
|
|
|
2019-07-26 06:38:03 +02:00
|
|
|
return y2038safe
|
|
|
|
|
|
|
|
|
|
|
|
def get_args():
|
|
|
|
parser = cppcheckdata.ArgumentParser()
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
args = get_args()
|
|
|
|
|
|
|
|
exit_code = 0
|
|
|
|
quiet = not any((args.quiet, args.cli))
|
|
|
|
|
2019-11-15 20:14:30 +01:00
|
|
|
if not args.dumpfile:
|
|
|
|
if not args.quiet:
|
|
|
|
print("no input files.")
|
|
|
|
sys.exit(0)
|
2019-07-26 06:38:03 +02:00
|
|
|
|
2019-11-15 20:14:30 +01:00
|
|
|
for dumpfile in args.dumpfile:
|
|
|
|
if not os.path.isfile(dumpfile):
|
|
|
|
print("Error: File not found: %s" % dumpfile)
|
|
|
|
sys.exit(127)
|
|
|
|
if not os.access(dumpfile, os.R_OK):
|
|
|
|
print("Error: Permission denied: %s" % dumpfile)
|
|
|
|
sys.exit(13)
|
|
|
|
if not args.quiet:
|
|
|
|
print('Checking ' + dumpfile + '...')
|
|
|
|
|
|
|
|
y2038safe = check_y2038_safe(dumpfile, quiet)
|
|
|
|
if not y2038safe and exit_code == 0:
|
|
|
|
exit_code = 1
|
2019-07-26 06:38:03 +02:00
|
|
|
|
|
|
|
sys.exit(exit_code)
|