Add new interceptor: fgetln(3)

Summary:
fgetln - get a line from a stream

Sponsored by <The NetBSD Foundation>

Reviewers: vitalybuka, joerg

Reviewed By: vitalybuka

Subscribers: llvm-commits, kubamracek, #sanitizers

Tags: #sanitizers

Differential Revision: https://reviews.llvm.org/D43482

llvm-svn: 325587
This commit is contained in:
Kamil Rytarowski 2018-02-20 15:52:08 +00:00
parent babcdb3a9a
commit 8317565532
3 changed files with 41 additions and 0 deletions

View File

@ -6807,6 +6807,22 @@ INTERCEPTOR(int, devname_r, u64 dev, u32 type, char *path, uptr len) {
#define INIT_DEVNAME_R
#endif
#if SANITIZER_INTERCEPT_FGETLN
INTERCEPTOR(char *, fgetln, __sanitizer_FILE *stream, SIZE_T *len) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, fgetln, stream, len);
char *str = REAL(fgetln)(stream, len);
if (str && len) {
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, *len);
}
return str;
}
#define INIT_FGETLN COMMON_INTERCEPT_FUNCTION(fgetln)
#else
#define INIT_FGETLN
#endif
static void InitializeCommonInterceptors() {
static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap();
@ -7035,6 +7051,7 @@ static void InitializeCommonInterceptors() {
INIT_STRLCPY;
INIT_DEVNAME;
INIT_DEVNAME_R;
INIT_FGETLN;
#if SANITIZER_NETBSD
COMMON_INTERCEPT_FUNCTION(__libc_mutex_lock);

View File

@ -459,5 +459,6 @@
#define SANITIZER_INTERCEPT_DEVNAME SI_NETBSD
#define SANITIZER_INTERCEPT_DEVNAME_R SI_NETBSD
#define SANITIZER_INTERCEPT_FGETLN SI_NETBSD
#endif // #ifndef SANITIZER_PLATFORM_INTERCEPTORS_H

View File

@ -0,0 +1,23 @@
// RUN: %clangxx -O0 -g %s -o %t && %run %t
#include <stdio.h>
#include <stdlib.h>
int main(void) {
FILE *fp;
size_t len;
char *s;
fp = fopen("/etc/hosts", "r");
if (!fp)
exit(1);
s = fgetln(fp, &len);
printf("%.*s\n", (int)len, s);
if (fclose(fp) == EOF)
exit(1);
return 0;
}