Skip to content

Commit 2849681

Browse files
author
Зишан Мирза
committed
add tests for ctime and localtime
1 parent 3f1fd18 commit 2849681

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

clang/test/Analysis/Inputs/std-c-library-functions-POSIX.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,11 @@ int utimensat(int dirfd, const char *pathname, const struct timespec times[2], i
174174
int utimes(const char *filename, const struct timeval times[2]);
175175
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
176176
struct tm *localtime(const time_t *tp);
177-
struct tm *localtime_r(const time_t *restrict timer, struct tm *restrict result);
177+
struct tm *localtime_r(const time_t *timer, struct tm *result);
178+
struct tm *localtime_s(const time_t *restrict timer, struct tm *restrict result);
178179
char *asctime_r(const struct tm *restrict tm, char *restrict buf);
179180
char *ctime_r(const time_t *timep, char *buf);
181+
char *ctime_s(char *buf, rsize_t buf_size, const time_t *timep);
180182
struct tm *gmtime_r(const time_t *restrict timer, struct tm *restrict result);
181183
struct tm *gmtime(const time_t *tp);
182184
int clock_gettime(clockid_t clock_id, struct timespec *tp);

clang/test/Analysis/cert/env34-c.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ lconv *localeconv(void);
1616
typedef struct {
1717
} tm;
1818
char *asctime(const tm *timeptr);
19-
char *ctime(const tm *timeptr);
20-
struct tm *localtime(struct tm *tm);
19+
char *ctime(const time_t *time);
20+
struct tm *localtime(const time_t *time);
2121

2222
int strcmp(const char*, const char*);
2323
extern void foo(char *e);
@@ -315,6 +315,34 @@ void asctime_test(void) {
315315
// expected-note@-2{{dereferencing an invalid pointer}}
316316
}
317317

318+
void ctime_test(void) {
319+
const time_t *t;
320+
const time_t *tt;
321+
322+
char* p = ctime(t);
323+
// expected-note@-1{{previous function call was here}}
324+
char* pp = ctime(tt);
325+
// expected-note@-1{{'ctime' call may invalidate the result of the previous 'ctime'}}
326+
327+
*p;
328+
// expected-warning@-1{{dereferencing an invalid pointer}}
329+
// expected-note@-2{{dereferencing an invalid pointer}}
330+
}
331+
332+
void time_test(void) {
333+
const time_t *t;
334+
const time_t *tt;
335+
336+
struct tm* p = localtime(t);
337+
// expected-note@-1{{previous function call was here}}
338+
struct tm* pp = localtime(tt);
339+
// expected-note@-1{{'localtime' call may invalidate the result of the previous 'localtime'}}
340+
341+
*p;
342+
// expected-warning@-1{{dereferencing an invalid pointer}}
343+
// expected-note@-2{{dereferencing an invalid pointer}}
344+
}
345+
318346
void localeconv_test1(void) {
319347
lconv *lc1 = localeconv();
320348
// expected-note@-1{{previous function call was here}}

clang/test/Analysis/std-c-library-functions-POSIX.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,11 @@
129129
// CHECK: Loaded summary for: int utimes(const char *filename, const struct timeval times[2])
130130
// CHECK: Loaded summary for: int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
131131
// CHECK: Loaded summary for: struct tm *localtime(const time_t *tp)
132-
// CHECK: Loaded summary for: struct tm *localtime_r(const time_t *restrict timer, struct tm *restrict result)
132+
// CHECK: Loaded summary for: struct tm *localtime_r(const time_t *timer, struct tm *result)
133+
// CHECK: Loaded summary for: struct tm *localtime_s(const time_t *restrict timer, struct tm *restrict result)
133134
// CHECK: Loaded summary for: char *asctime_r(const struct tm *restrict tm, char *restrict buf)
134135
// CHECK: Loaded summary for: char *ctime_r(const time_t *timep, char *buf)
136+
// CHECK: Loaded summary for: char *ctime_s(char *buf, rsize_t buf_size, const time_t *timep)
135137
// CHECK: Loaded summary for: struct tm *gmtime_r(const time_t *restrict timer, struct tm *restrict result)
136138
// CHECK: Loaded summary for: struct tm *gmtime(const time_t *tp)
137139
// CHECK: Loaded summary for: int clock_gettime(clockid_t clock_id, struct timespec *tp)

0 commit comments

Comments
 (0)