acrn-hypervisor/tools/acrn-crashlog/common/strutils.c
xiaojin2 cb39badf82 tools: acrn-crashlog: fix potential issues under common and usercrash
This patch is to fix buffer overflow, return value not unified and
variable type not matched issues. And add some judge logic to improve
code quality.

Changes:
1. Handle the fd properly in the failing case.
2. Fix buffer overflow issues and null pointer access issues.
3. Fix the format issue in log_sys.c.
4. Remove the useless branch and adjust the function logic.
5. Add some checks for the string length before using strcpy/strcat/memcpy.
6. Fix strncpy null-terminated issues.
7. Change the return value to unify the return type.

Signed-off-by: CHEN Gang <gang.c.chen@intel.com>
Signed-off-by: xiaojin2 <xiaojing.liu@intel.com>
Reviewed-by: Zhi Jin <zhi.jin@intel.com>
Reviewed-by: Liu Xinwu <xinwu.liu@intel.com>
Acked-by: Zhang Di <di.zhang@intel.com>
2018-06-21 11:29:20 +08:00

118 lines
1.7 KiB
C

/*
* Copyright (C) 2018 Intel Corporation
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <string.h>
#include <errno.h>
/**
* Get the length of line.
*
* @param str Start address of line.
*
* @return the length of line if successful, or -1 if not.
* This function return length of string if string doesn't contain \n.
*/
int strlinelen(char *str)
{
char *tag;
if (!str)
return -1;
tag = strchr(str, '\n');
if (tag)
return tag - str + 1;
return strlen(str);
}
/**
* Find the last occurrence of the substring str in the string s.
*
* @param s Range of search.
* @param substr String to be found.
*
* @return a pointer to the beginning of the substring,
* or NULL if the substring is not found.
*/
char *strrstr(char *s, char *substr)
{
char *found;
char *p = s;
while ((found = strstr(p, substr)))
p = found + 1;
if (p != s)
return p - 1;
return NULL;
}
char *next_line(char *buf)
{
char *p;
p = strchr(buf, '\n');
/* if meet end of buf, the return value is also NULL */
if (p)
return p + 1;
return NULL;
}
static char *strtriml(char *str)
{
char *p = str;
while (*p == ' ')
p++;
return memmove(str, p, strlen(p) + 1);
}
static char *strtrimr(char *str)
{
size_t len;
char *end;
len = strlen(str);
if (len > 0) {
end = str + strlen(str) - 1;
while (*end == ' ' && end >= str) {
*end = 0;
end--;
}
}
return str;
}
char *strtrim(char *str)
{
if (str) {
strtrimr(str);
return strtriml(str);
}
return NULL;
}
int strcnt(char *str, char c)
{
int cnt = 0;
char *p = str;
char *found;
if (!str)
return -EINVAL;
while ((found = strchr(p, c))) {
p = found + 1;
cnt++;
}
return cnt;
}