mirror of
https://github.com/projectacrn/acrn-hypervisor.git
synced 2025-06-29 00:37:15 +00:00
dm: remove unsafe apis in dm log
sprintf/vsnprintf are not safe, so use snprintf instead of sprintf, use vasprintf instead of vsnprintf. Tracked-On: #3394 Signed-off-by: Tianhua Sun <tianhuax.s.sun@intel.com> Reviewed-by: Yonghua Huang <yonghua.huang@intel.com> Reviewed-by: Minggui Cao <minggui.cao@intel.com>
This commit is contained in:
parent
d8b752c4ee
commit
e749ced4a0
@ -139,7 +139,8 @@ static void write_to_disk(const char *fmt, va_list args)
|
|||||||
{
|
{
|
||||||
char buffer[DISK_LOG_MAX_LEN];
|
char buffer[DISK_LOG_MAX_LEN];
|
||||||
char *file_name = buffer;
|
char *file_name = buffer;
|
||||||
int len1, len2;
|
char *buf;
|
||||||
|
int len;
|
||||||
int write_cnt;
|
int write_cnt;
|
||||||
struct timespec times = {0, 0};
|
struct timespec times = {0, 0};
|
||||||
|
|
||||||
@ -155,11 +156,23 @@ static void write_to_disk(const char *fmt, va_list args)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
clock_gettime(CLOCK_MONOTONIC, ×);
|
len = vasprintf(&buf, fmt, args);
|
||||||
len1 = sprintf(buffer, "[%5lu.%06lu] ", times.tv_sec, times.tv_nsec / 1000);
|
if (len < 0)
|
||||||
len2 = vsnprintf(buffer + len1, MAX_ONE_LOG_SIZE, fmt, args);
|
return;
|
||||||
|
|
||||||
write_cnt = write(disk_fd, buffer, len1 + len2);
|
clock_gettime(CLOCK_MONOTONIC, ×);
|
||||||
|
len = snprintf(buffer, DISK_LOG_MAX_LEN, "[%5lu.%06lu] ", times.tv_sec, times.tv_nsec / 1000);
|
||||||
|
if (len < 0 || len >= DISK_LOG_MAX_LEN) {
|
||||||
|
free(buf);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
len = strnlen(buffer, DISK_LOG_MAX_LEN);
|
||||||
|
|
||||||
|
strncpy(buffer + len, buf, DISK_LOG_MAX_LEN - len);
|
||||||
|
buffer[DISK_LOG_MAX_LEN - 1] = '\0';
|
||||||
|
free(buf);
|
||||||
|
|
||||||
|
write_cnt = write(disk_fd, buffer, strnlen(buffer, DISK_LOG_MAX_LEN));
|
||||||
if (write_cnt < 0) {
|
if (write_cnt < 0) {
|
||||||
perror(DISK_PREFIX"write disk failed");
|
perror(DISK_PREFIX"write disk failed");
|
||||||
close(disk_fd);
|
close(disk_fd);
|
||||||
|
@ -69,17 +69,21 @@ static void deinit_kmsg(void)
|
|||||||
|
|
||||||
static void write_to_kmsg(const char *fmt, va_list args)
|
static void write_to_kmsg(const char *fmt, va_list args)
|
||||||
{
|
{
|
||||||
|
char *buf;
|
||||||
char kmsg_buf[KMSG_MAX_LEN] = KMSG_PREFIX;
|
char kmsg_buf[KMSG_MAX_LEN] = KMSG_PREFIX;
|
||||||
int len1, len2;
|
int len, write_cnt;
|
||||||
int write_cnt;
|
|
||||||
|
|
||||||
if (kmsg_fd < 0)
|
if (kmsg_fd < 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
len1 = strlen(KMSG_PREFIX);
|
len = vasprintf(&buf, fmt, args);
|
||||||
len2 = vsnprintf(kmsg_buf + len1, MAX_ONE_LOG_SIZE, fmt, args);
|
if (len < 0)
|
||||||
|
return;
|
||||||
|
strncpy(kmsg_buf + strlen(KMSG_PREFIX), buf, KMSG_MAX_LEN - strlen(KMSG_PREFIX));
|
||||||
|
kmsg_buf[KMSG_MAX_LEN - 1] = '\0';
|
||||||
|
free(buf);
|
||||||
|
|
||||||
write_cnt = write(kmsg_fd, kmsg_buf, len1 + len2);
|
write_cnt = write(kmsg_fd, kmsg_buf, strnlen(kmsg_buf, KMSG_MAX_LEN));
|
||||||
if (write_cnt < 0) {
|
if (write_cnt < 0) {
|
||||||
perror(KMSG_PREFIX"write kmsg failed");
|
perror(KMSG_PREFIX"write kmsg failed");
|
||||||
close(kmsg_fd);
|
close(kmsg_fd);
|
||||||
|
Loading…
Reference in New Issue
Block a user