Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 16 additions & 25 deletions src/libltfs/arch/ltfs_arch_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,6 @@ extern "C" {
} \
}while(0)

static inline void arch_strcpy_limited(char *dest, const char *src, int count)
{
int i;
for (i = 0; i < (count) && (src)[i] != '\0'; i++)
(dest)[i] = (src)[i];
if (i < (count))
(dest)[i] = '\0';
}



#ifdef _MSC_VER
#include <libxml/xmlmemory.h>
Expand Down Expand Up @@ -114,18 +104,18 @@ static inline void arch_strcpy_limited(char *dest, const char *src, int count)

#define arch_fopen(file, mode, file_ptr) fopen_s(&(file_ptr), file, mode)

#define arch_ctime(buf, time_ptr) ctime_s(buf, sizeof(buf), time_ptr)

#define arch_getenv(buf, name) do { size_t len; _dupenv_s(&(buf), &(len), name); } while (0)

#define arch_strtok(str, delm, ctxt) strtok_s((str), (delm), &(ctxt))

#define arch_strcpy(dest, size, src) strcpy_s((dest), (size), (src))

#define arch_strncpy(dest, src, size, cnt) strncpy_s((dest), (size), (src), (cnt))

#define arch_strcat(dest, size, src) strcat_s((dest), (size), (src))

#define arch_strtok(str, delm, ctxt) strtok_s((str), (delm), &(ctxt))

#define arch_ctime(buf, time_ptr) ctime_s(buf, sizeof(buf), time_ptr)

#define arch_unlink _unlink

#define arch_write _write
Expand Down Expand Up @@ -165,18 +155,18 @@ static inline void arch_strcpy_limited(char *dest, const char *src, int count)

#define arch_fopen(file, mode, file_ptr) do {file_ptr = fopen(file, mode);}while(0)

#define arch_ctime(buf ,time_ptr) do { buf = ctime(time_ptr); } while (0)

#define arch_getenv(buf ,name) do { buf = getenv(name); } while (0)

#define arch_strcpy(dest, unused, src) ({if(unused || !unused) {strcpy(dest, src);}})
#define arch_strcpy(dest, unused, src) ((void)(unused), strcpy(dest, src))

#define arch_strncpy(dest, src, unused, cnt) strncpy(dest, src, cnt)
#define arch_strncpy(dest, src, destSize, cnt) strncpy(dest, src, (cnt))

#define arch_strcat(dest, unused, src)( {if(unused || !unused){ strcat(dest, src);}})
#define arch_strcat(dest, unused, src) ((void)(unused), strcat(dest, src))

#define arch_strtok(str, delim, unused) ((void)(unused), strtok(str, delim))

#define arch_ctime(buf ,time_ptr) do { buf = ctime(time_ptr); } while (0)

#define arch_unlink unlink

#define arch_write write
Expand All @@ -198,14 +188,15 @@ static inline void arch_strcpy_limited(char *dest, const char *src, int count)

#endif /* _MSC_VER */

/* These needs to be declared at the end to avoid redefinition and to avoid code replication */
#define arch_vsprintf_auto( buffer, fmt, ...) arch_vsprintf(buffer,sizeof(buffer),fmt,__VA_ARGS__)

/*
These needs to be declared at the end to avoid redefinition and to avoid code replication
When using them, dest or buffer needs to be a fixed size array since it will calculate it
with the sizeof.
*/

#define arch_strcpy_auto(dest, src) arch_strcpy(dest, sizeof(dest), src);

#define arch_strncpy_auto(dest, src, destSize) arch_strncpy(dest, src, destSize, destSize);

#define arch_strcat_auto(dest,src) arch_strcat(dest, sizeof(dest), src);
#define arch_strncpy_auto(dest, src, count) arch_strncpy(dest, src, sizeof(dest), count);

#define arch_sprintf_auto(buffer, fmt, ...) arch_sprintf(buffer,sizeof(buffer),fmt, __VA_ARGS__)

Expand Down
13 changes: 8 additions & 5 deletions src/libltfs/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ int fs_hash_sort_by_uid(struct name_list *a, struct name_list *b)
static char* generate_hash_key_name(const char *src_str, int *rc)
{
char *key_name = NULL;
key_name = malloc(sizeof(char*));
if (key_name == NULL) return NULL;
#ifdef mingw_PLATFORM
UChar *uchar_name;

Expand All @@ -89,11 +87,16 @@ static char* generate_hash_key_name(const char *src_str, int *rc)

if (*rc != 0) {
key_name = NULL;
} else
free(uchar_name);
} else{
arch_safe_free(uchar_name);
}
#else
key_name = arch_strdup(src_str);
*rc = 0;
if (key_name){
*rc = 0;
}else{
*rc = -LTFS_NO_MEMORY;
}
#endif

return key_name;
Expand Down
20 changes: 20 additions & 0 deletions src/libltfs/index_criteria.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,23 +309,43 @@ int index_criteria_parse_name(const char *criteria, size_t len, struct index_cri
*delim = '\0';
rule_ptr->percent_encode = fs_is_percent_encode_required(rule);
rule_ptr->name = arch_strdup(rule);
if (!rule_ptr->name) {
ltfsmsg(LTFS_ERR, 10001E, __FUNCTION__);
arch_safe_free(rulebuf);
return -LTFS_NO_MEMORY;
}
rule_ptr++;
rule = delim+1;
} else if (*delim == '/') {
*delim = '\0';
rule_ptr->percent_encode = fs_is_percent_encode_required(rule);
rule_ptr->name = arch_strdup(rule);
if (!rule_ptr->name) {
ltfsmsg(LTFS_ERR, 10001E, __FUNCTION__);
arch_safe_free(rulebuf);
return -LTFS_NO_MEMORY;
}
rule_ptr++;
} else if (*(delim+1) == '\0') {
rule_ptr->percent_encode = fs_is_percent_encode_required(rule);
rule_ptr->name = arch_strdup(rule);
if (!rule_ptr->name) {
ltfsmsg(LTFS_ERR, 10001E, __FUNCTION__);
arch_safe_free(rulebuf);
return -LTFS_NO_MEMORY;
}
rule_ptr++;
}
}

if (ic->glob_patterns == rule_ptr) {
rule_ptr->percent_encode = fs_is_percent_encode_required(rule);
rule_ptr->name = arch_strdup(rule);
if (!rule_ptr->name) {
ltfsmsg(LTFS_ERR, 10001E, __FUNCTION__);
arch_safe_free(rulebuf);
return -LTFS_NO_MEMORY;
}
}

/* Validate rules */
Expand Down
7 changes: 5 additions & 2 deletions src/libltfs/ltfs_fsops.c
Original file line number Diff line number Diff line change
Expand Up @@ -2020,12 +2020,14 @@ int ltfs_fsops_target_absolute_path(const char* link, const char* target, char*
len=strlen(link);
int work_buf_len = len + len2 + 1;
work_buf = malloc(work_buf_len);
memset(work_buf, '\0', work_buf_len);
if (!work_buf) {
ltfsmsg(LTFS_ERR, 10001E, "ltfs_fsops_target_absolute_path: work_buf");
return -LTFS_NO_MEMORY;
}
int target_buf_len = len2 + 1;
target_buf = malloc(target_buf_len);
memset(target_buf, '\0', target_buf_len);
if (!target_buf) {
free(work_buf);
ltfsmsg(LTFS_ERR, 10001E, "ltfs_fsops_target_absolute_path: target_buf");
Expand Down Expand Up @@ -2063,13 +2065,14 @@ int ltfs_fsops_target_absolute_path(const char* link, const char* target, char*
len -= strlen(temp_buf); /* length of "/aaa" */
} else if (strcmp(token, "." )) { /* have directory name */
work_buf[len] = '/'; /* put '/ 'as "/aaa/" */
arch_strncpy(work_buf+len+1, token, work_buf_len, strlen(token) + 1); /* "/aaa/ccc\0" */
arch_strncpy(work_buf+len+1, token, work_buf_len, strlen(token)); /* "/aaa/ccc\0" */
len = strlen(work_buf);
}
token = next_token;
}
work_buf[len] = '/'; /* put '/ 'as "/aaa/ccc/" */
arch_strncpy(work_buf+len+1, token, work_buf_len, strlen(token)+1); /* "/aaa/ccc/target.txt\0" */
if(token)
arch_strncpy(work_buf+len+1, token, work_buf_len, strlen(token)); /* "/aaa/ccc/target.txt\0" */

if (size < strlen(work_buf) + 1) {
free(work_buf);
Expand Down
10 changes: 10 additions & 0 deletions src/libltfs/ltfs_internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,11 @@ int ltfs_split_symlink(struct ltfs_volume *vol)

/* check lost_and_found directory and make if it doesn't exist */
int pathsize = asprintf( &lfdir, "/%s", LTFS_LOSTANDFOUND_DIR );
if (pathsize < 0) {
ltfsmsg(LTFS_ERR, 10001E, "_ltfs_recover_symlink: lfdir");
return -LTFS_NO_MEMORY;
}

ret = fs_path_lookup(lfdir, 0, &workd, vol->index);
if ( ret==-LTFS_NO_DENTRY ) {
ret = ltfs_fsops_create( lfdir, true, false, false, &workd, vol);
Expand All @@ -1313,6 +1318,11 @@ int ltfs_split_symlink(struct ltfs_volume *vol)
}
ret = ltfs_fsops_close( workd, true, true, use_iosche, vol);
path=arch_strdup(lfdir);
if (!path) {
ltfsmsg(LTFS_ERR, 10001E, "_ltfs_recover_symlink: path");
free(lfdir);
return -LTFS_NO_MEMORY;
}

/* loop for conflicted files */
for( i=0; i<(vol->index->symerr_count); i++ ){
Expand Down
2 changes: 1 addition & 1 deletion src/libltfs/ltfslogging.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ int ltfsmsg_internal(bool print_id, int level, char **msg_out, const char *_id,
goto internal_error;

if (idlen > 1 && _id[0] == '"' && _id[idlen - 1] == '"') {
arch_strcpy_limited(id, _id + 1, idlen - 2);
arch_strncpy_auto(id, _id + 1, idlen - 2);
id[idlen - 2] = '\0';
}
else {
Expand Down
Loading
Loading