![Sergei Trofimovich](/assets/img/avatar_default.png)
valgrind detected an error in completion path: ==2962== Source and destination overlap in strcpy(0x459F068, 0x459F06A) ==2962== at 0x4026056: strcpy (mc_replace_strmem.c:268) ==2962== by 0x808F70B: canonicalize_pathname (string3.h:106) ==2962== by 0x805ECBA: filename_completion_function (complete.c:125) ==2962== by 0x805FB35: command_completion_function (complete.c:448) ==2962== by 0x805EA34: completion_matches (complete.c:552) ==2962== by 0x8060454: complete (complete.c:735) ==2962== by 0x809AAC4: handle_char (widget.c:1545) ==2962== by 0x807867E: midnight_callback (dialog.h:201) ==2962== by 0x8061B27: dlg_process_event (dialog.c:664) ==2962== by 0x8061ECE: run_dlg (dialog.c:786) ==2962== by 0x807996C: main (main.c:1674) Snippet of man strcpy: DESCRIPTION The strcpy() function copies the string pointed to by src, including the terminating null byte ('\0'), to the buffer pointed to by dest. ___The strings may not overlap___, and the destination string dest must be large enough to receive the copy. We used strcpy to move data chunk in memory: "./foo" -> "foo", etc. This patch introduces mhl_strmove and fixed canonicalize_pathname. Signed-off-by: Sergei Trofimovich <st@anti-virus.by>
140 строки
3.2 KiB
C
140 строки
3.2 KiB
C
#ifndef __MHL_STRING_H
|
|
#define __MHL_STRING_H
|
|
|
|
#include <ctype.h>
|
|
#include <stdarg.h>
|
|
#include <assert.h>
|
|
#include <mhl/memory.h>
|
|
|
|
#define mhl_str_dup(str) ((str ? strdup(str) : strdup("")))
|
|
#define mhl_str_ndup(str,len) ((str ? strndup(str,len) : strdup("")))
|
|
#define mhl_str_len(str) ((str ? strlen(str) : 0))
|
|
|
|
static inline char * mhl_str_dup_range(const char * s_start, const char * s_bound)
|
|
{
|
|
return mhl_str_ndup(s_start, s_bound - s_start);
|
|
}
|
|
|
|
static inline char* mhl_str_trim(char* str)
|
|
{
|
|
if (!str) return NULL; // NULL string ?! bail out.
|
|
|
|
// find the first non-space
|
|
char* start; for (start=str; ((*str) && (!isspace(*str))); str++);
|
|
|
|
// only spaces ?
|
|
if (!(*str)) { *str = 0; return str; }
|
|
|
|
// get the size (cannot be empty - catched above)
|
|
size_t _sz = strlen(str);
|
|
|
|
// find the proper end
|
|
char* end;
|
|
for (end=(str+_sz-1); ((end>str) && (isspace(*end))); end--);
|
|
end[1] = 0; // terminate, just to be sure
|
|
|
|
// if we have no leading spaces, just trucate
|
|
if (start==str) { end++; *end = 0; return str; }
|
|
|
|
|
|
// if it' only one char, dont need memmove for that
|
|
if (start==end) { str[0]=*start; str[1]=0; return str; }
|
|
|
|
// by here we have a (non-empty) region between start end end
|
|
memmove(str,start,(end-start+1));
|
|
return str;
|
|
}
|
|
|
|
static inline void mhl_str_toupper(char* str)
|
|
{
|
|
if (str)
|
|
for (;*str;str++)
|
|
*str = toupper(*str);
|
|
}
|
|
|
|
#define __STR_CONCAT_MAX 32
|
|
/* _NEVER_ call this function directly ! */
|
|
static inline char* __mhl_str_concat_hlp(const char* base, ...)
|
|
{
|
|
static const char* arg_ptr[__STR_CONCAT_MAX];
|
|
static size_t arg_sz[__STR_CONCAT_MAX];
|
|
int count = 0;
|
|
size_t totalsize = 0;
|
|
|
|
if (base)
|
|
{
|
|
arg_ptr[0] = base;
|
|
arg_sz[0] = totalsize = strlen(base);
|
|
count = 1;
|
|
}
|
|
|
|
va_list args;
|
|
va_start(args,base);
|
|
char* a;
|
|
// note: we use ((char*)(1)) as terminator - NULL is a valid argument !
|
|
while ((a = va_arg(args, char*))!=(char*)1)
|
|
{
|
|
// printf("a=%u\n", a);
|
|
if (a)
|
|
{
|
|
arg_ptr[count] = a;
|
|
arg_sz[count] = strlen(a);
|
|
totalsize += arg_sz[count];
|
|
count++;
|
|
}
|
|
}
|
|
|
|
if (!count)
|
|
return mhl_str_dup("");
|
|
|
|
// now as we know how much to copy, allocate the buffer
|
|
char* buffer = (char*)mhl_mem_alloc_u(totalsize+2);
|
|
char* current = buffer;
|
|
int x=0;
|
|
for (x=0; x<count; x++)
|
|
{
|
|
memcpy(current, arg_ptr[x], arg_sz[x]);
|
|
current += arg_sz[x];
|
|
}
|
|
|
|
*current = 0;
|
|
return buffer;
|
|
}
|
|
|
|
#define mhl_str_concat(...) (__mhl_str_concat_hlp(__VA_ARGS__, (char*)(1)))
|
|
|
|
static inline char* mhl_str_reverse(char* ptr)
|
|
{
|
|
if (!ptr) return NULL; // missing string
|
|
if (!(ptr[0] && ptr[1])) return ptr; // empty or 1-ch string
|
|
|
|
size_t _sz = strlen(ptr);
|
|
char* start = ptr;
|
|
char* end = ptr+_sz-1;
|
|
|
|
while (start<end)
|
|
{
|
|
char c = *start;
|
|
*start = *end;
|
|
*end = c;
|
|
start++;
|
|
end--;
|
|
}
|
|
|
|
return ptr;
|
|
}
|
|
|
|
/*
|
|
* strcpy is unsafe on overlapping memory areas, so define memmove-alike
|
|
* string function. Has sense only when dest <= src.
|
|
*/
|
|
static inline char * mhl_strmove(char * dest, const char * src)
|
|
{
|
|
size_t n = strlen (src) + 1; /* + '\0' */
|
|
|
|
assert (dest<=src);
|
|
|
|
return memmove(dest, src, n);
|
|
}
|
|
#endif /* __MHL_STRING_H */
|