Skip to content

Commit 57cf068

Browse files
committed
fix: 2 memory leaks
1 parent 2a41f00 commit 57cf068

File tree

10 files changed

+150
-115
lines changed

10 files changed

+150
-115
lines changed

src/libltfs/arch/ltfs_arch_ops.h

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,6 @@ extern "C" {
7474
} \
7575
}while(0)
7676

77-
static inline void arch_strcpy_limited(char *dest, const char *src, int count)
78-
{
79-
int i;
80-
for (i = 0; i < (count) && (src)[i] != '\0'; i++)
81-
(dest)[i] = (src)[i];
82-
if (i < (count))
83-
(dest)[i] = '\0';
84-
}
85-
86-
8777

8878
#ifdef _MSC_VER
8979
#include <libxml/xmlmemory.h>
@@ -114,8 +104,6 @@ static inline void arch_strcpy_limited(char *dest, const char *src, int count)
114104

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

117-
#define arch_ctime(buf, time_ptr) ctime_s(buf, sizeof(buf), time_ptr)
118-
119107
#define arch_getenv(buf, name) do { size_t len; _dupenv_s(&(buf), &(len), name); } while (0)
120108

121109
#define arch_strtok(str, delm, ctxt) strtok_s((str), (delm), &(ctxt))
@@ -165,15 +153,13 @@ static inline void arch_strcpy_limited(char *dest, const char *src, int count)
165153

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

168-
#define arch_ctime(buf ,time_ptr) do { buf = ctime(time_ptr); } while (0)
169-
170156
#define arch_getenv(buf ,name) do { buf = getenv(name); } while (0)
171157

172-
#define arch_strcpy(dest, unused, src) ({if(unused || !unused) {strcpy(dest, src);}})
158+
#define arch_strcpy(dest, unused, src) ((void)(unused), strcpy(dest, src))
173159

174-
#define arch_strncpy(dest, src, unused, cnt) strncpy(dest, src, cnt)
160+
#define arch_strncpy(dest, src, destSize, cnt) strncpy(dest, src, (cnt))
175161

176-
#define arch_strcat(dest, unused, src)( {if(unused || !unused){ strcat(dest, src);}})
162+
#define arch_strcat(dest, unused, src) ((void)(unused), strcat(dest, src))
177163

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

@@ -198,14 +184,15 @@ static inline void arch_strcpy_limited(char *dest, const char *src, int count)
198184

199185
#endif /* _MSC_VER */
200186

201-
/* These needs to be declared at the end to avoid redefinition and to avoid code replication */
202-
#define arch_vsprintf_auto( buffer, fmt, ...) arch_vsprintf(buffer,sizeof(buffer),fmt,__VA_ARGS__)
203-
187+
/*
188+
These needs to be declared at the end to avoid redefinition and to avoid code replication
189+
When using them, dest or buffer needs to be a fixed size array since it will calculate it
190+
with the sizeof.
191+
*/
192+
204193
#define arch_strcpy_auto(dest, src) arch_strcpy(dest, sizeof(dest), src);
205194

206-
#define arch_strncpy_auto(dest, src, destSize) arch_strncpy(dest, src, destSize, destSize);
207-
208-
#define arch_strcat_auto(dest,src) arch_strcat(dest, sizeof(dest), src);
195+
#define arch_strncpy_auto(dest, src, count) arch_strncpy(dest, src, sizeof(dest), count);
209196

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

src/libltfs/fs.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ int fs_hash_sort_by_uid(struct name_list *a, struct name_list *b)
7777
static char* generate_hash_key_name(const char *src_str, int *rc)
7878
{
7979
char *key_name = NULL;
80-
key_name = malloc(sizeof(char*));
81-
if (key_name == NULL) return NULL;
8280
#ifdef mingw_PLATFORM
8381
UChar *uchar_name;
8482

@@ -89,15 +87,16 @@ static char* generate_hash_key_name(const char *src_str, int *rc)
8987

9088
if (*rc != 0) {
9189
key_name = NULL;
92-
} else
93-
free(uchar_name);
90+
} else{
91+
arch_safe_free(uchar_name);
92+
}
9493
#else
9594
key_name = arch_strdup(src_str);
96-
if (!key_name) {
97-
*rc = -LTFS_NO_MEMORY;
98-
} else {
95+
if (key_name){
9996
*rc = 0;
100-
}
97+
}else{
98+
*rc = -LTFS_NO_MEMORY;
99+
}
101100
#endif
102101

103102
return key_name;

src/libltfs/ltfs_fsops.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2020,12 +2020,14 @@ int ltfs_fsops_target_absolute_path(const char* link, const char* target, char*
20202020
len=strlen(link);
20212021
int work_buf_len = len + len2 + 1;
20222022
work_buf = malloc(work_buf_len);
2023+
memset(work_buf, '\0', work_buf_len);
20232024
if (!work_buf) {
20242025
ltfsmsg(LTFS_ERR, 10001E, "ltfs_fsops_target_absolute_path: work_buf");
20252026
return -LTFS_NO_MEMORY;
20262027
}
20272028
int target_buf_len = len2 + 1;
20282029
target_buf = malloc(target_buf_len);
2030+
memset(target_buf, '\0', target_buf_len);
20292031
if (!target_buf) {
20302032
free(work_buf);
20312033
ltfsmsg(LTFS_ERR, 10001E, "ltfs_fsops_target_absolute_path: target_buf");
@@ -2063,13 +2065,14 @@ int ltfs_fsops_target_absolute_path(const char* link, const char* target, char*
20632065
len -= strlen(temp_buf); /* length of "/aaa" */
20642066
} else if (strcmp(token, "." )) { /* have directory name */
20652067
work_buf[len] = '/'; /* put '/ 'as "/aaa/" */
2066-
arch_strncpy(work_buf+len+1, token, work_buf_len, strlen(token) + 1); /* "/aaa/ccc\0" */
2068+
arch_strncpy(work_buf+len+1, token, work_buf_len, strlen(token)); /* "/aaa/ccc\0" */
20672069
len = strlen(work_buf);
20682070
}
20692071
token = next_token;
20702072
}
20712073
work_buf[len] = '/'; /* put '/ 'as "/aaa/ccc/" */
2072-
arch_strncpy(work_buf+len+1, token, work_buf_len, strlen(token)+1); /* "/aaa/ccc/target.txt\0" */
2074+
if(token)
2075+
arch_strncpy(work_buf+len+1, token, work_buf_len, strlen(token)); /* "/aaa/ccc/target.txt\0" */
20732076

20742077
if (size < strlen(work_buf) + 1) {
20752078
free(work_buf);

src/libltfs/ltfslogging.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ int ltfsmsg_internal(bool print_id, int level, char **msg_out, const char *_id,
396396
goto internal_error;
397397

398398
if (idlen > 1 && _id[0] == '"' && _id[idlen - 1] == '"') {
399-
arch_strcpy_limited(id, _id + 1, idlen - 2);
399+
arch_strncpy_auto(id, _id + 1, idlen - 2);
400400
id[idlen - 2] = '\0';
401401
}
402402
else {

src/libltfs/tape.c

Lines changed: 92 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,7 +1782,7 @@ int tape_set_cart_coherency(struct device_data *dev, const tape_partition_t part
17821782
/* APPLICATION CLIENT SPECIFIC INFORMATION LENGTH */
17831783
coh_data[30] = 0; /* Size of APPLICATION CLIENT SPECIFIC INFORMATION (Byte 1) */
17841784
coh_data[31] = 43; /* Size of APPLICATION CLIENT SPECIFIC INFORMATION (Byte 0) */
1785-
arch_strcpy_auto((char *)coh_data + 32, "LTFS");
1785+
arch_strcpy((char *)coh_data + 32,5, "LTFS");
17861786
memcpy(coh_data + 37, coh->uuid, 37);
17871787
/*
17881788
Version field
@@ -3075,86 +3075,128 @@ void set_tape_attribute(struct ltfs_volume *vol, struct tape_attr *t_attr)
30753075
* @param set attribute type
30763076
* @return 0 positive : success, negative : cannot set value to Cartridge Memory
30773077
*/
3078-
int tape_set_attribute_to_cm(struct device_data *dev, struct tape_attr *t_attr, int type)
3078+
int tape_set_attribute_to_cm(struct device_data* dev,
3079+
struct tape_attr* t_attr,
3080+
int type)
30793081
{
3080-
30813082
int ret;
30823083
int attr_size;
30833084
uint8_t format;
3085+
unsigned char* attr_data = NULL;
3086+
unsigned char* data;
3087+
size_t len;
30843088

30853089
CHECK_ARG_NULL(dev, -LTFS_NULL_ARG);
30863090
CHECK_ARG_NULL(t_attr, -LTFS_NULL_ARG);
30873091

3088-
if ( type == TC_MAM_APP_VENDER ) {
3092+
switch (type) {
3093+
case TC_MAM_APP_VENDER:
30893094
attr_size = TC_MAM_APP_VENDER_SIZE;
30903095
format = ASCII_FORMAT;
3091-
} else if ( type == TC_MAM_APP_NAME) {
3096+
break;
3097+
case TC_MAM_APP_NAME:
30923098
attr_size = TC_MAM_APP_NAME_SIZE;
30933099
format = ASCII_FORMAT;
3094-
} else if ( type== TC_MAM_APP_VERSION ) {
3100+
break;
3101+
case TC_MAM_APP_VERSION:
30953102
attr_size = TC_MAM_APP_VERSION_SIZE;
30963103
format = ASCII_FORMAT;
3097-
} else if ( type == TC_MAM_USER_MEDIUM_LABEL ) {
3104+
break;
3105+
case TC_MAM_USER_MEDIUM_LABEL:
30983106
attr_size = TC_MAM_USER_MEDIUM_LABEL_SIZE;
30993107
format = TEXT_FORMAT;
3100-
} else if ( type == TC_MAM_TEXT_LOCALIZATION_IDENTIFIER ) {
3108+
break;
3109+
case TC_MAM_TEXT_LOCALIZATION_IDENTIFIER:
31013110
attr_size = TC_MAM_TEXT_LOCALIZATION_IDENTIFIER_SIZE;
31023111
format = BINARY_FORMAT;
3103-
} else if ( type == TC_MAM_BARCODE ) {
3112+
break;
3113+
case TC_MAM_BARCODE:
31043114
attr_size = TC_MAM_BARCODE_SIZE;
31053115
format = ASCII_FORMAT;
3106-
} else if ( type == TC_MAM_APP_FORMAT_VERSION ) {
3116+
break;
3117+
case TC_MAM_APP_FORMAT_VERSION:
31073118
attr_size = TC_MAM_APP_FORMAT_VERSION_SIZE;
31083119
format = ASCII_FORMAT;
3109-
} else if ( type == TC_MAM_LOCKED_MAM ) {
3120+
break;
3121+
case TC_MAM_LOCKED_MAM:
31103122
attr_size = TC_MAM_LOCKED_MAM_SIZE;
31113123
format = BINARY_FORMAT;
3112-
} else if ( type == TC_MAM_MEDIA_POOL ) {
3124+
break;
3125+
case TC_MAM_MEDIA_POOL:
31133126
attr_size = TC_MAM_MEDIA_POOL_SIZE;
31143127
format = TEXT_FORMAT;
3115-
} else {
3128+
break;
3129+
default:
31163130
ltfsmsg(LTFS_WARN, 17204W, type, "tape_set_attribute_to_cm");
31173131
return -1;
31183132
}
31193133

3120-
unsigned char *attr_data = NULL;
3121-
attr_data = (unsigned char*)malloc(sizeof(unsigned char*)*(attr_size +TC_MAM_PAGE_HEADER_SIZE));
3122-
if (attr_data == NULL) return -LTFS_NO_MEMORY;
3123-
ltfs_u16tobe(attr_data, type); /* set attribute type */
3134+
attr_data = calloc(1, attr_size + TC_MAM_PAGE_HEADER_SIZE);
3135+
if (!attr_data)
3136+
return -LTFS_NO_MEMORY;
3137+
3138+
ltfs_u16tobe(attr_data, type); /* set attribute type */
31243139
attr_data[2] = format; /* set data format type */
31253140
ltfs_u16tobe(attr_data + 3, attr_size); /* set data size */
31263141

3142+
data = attr_data + TC_MAM_PAGE_HEADER_SIZE;
3143+
31273144
/* set attribute data */
3128-
if ( type == TC_MAM_APP_VENDER ) {
3129-
arch_strncpy((char *)attr_data + 5, t_attr->vender, attr_size+ TC_MAM_PAGE_HEADER_SIZE , attr_size);
3130-
} else if ( type == TC_MAM_APP_NAME ) {
3131-
arch_strncpy((char *)attr_data + 5, t_attr->app_name, attr_size + TC_MAM_PAGE_HEADER_SIZE, attr_size);
3132-
} else if ( type == TC_MAM_APP_VERSION ) {
3133-
arch_strncpy((char *)attr_data + 5, t_attr->app_ver, attr_size + TC_MAM_PAGE_HEADER_SIZE, attr_size);
3134-
} else if ( type == TC_MAM_USER_MEDIUM_LABEL ) {
3135-
arch_strncpy((char *)attr_data + 5, t_attr->medium_label, attr_size + TC_MAM_PAGE_HEADER_SIZE, attr_size);
3136-
} else if ( type == TC_MAM_TEXT_LOCALIZATION_IDENTIFIER ) {
3137-
attr_data[5] = t_attr->tli;
3138-
} else if ( type == TC_MAM_BARCODE ) {
3139-
arch_strncpy((char *)attr_data + 5, t_attr->barcode, attr_size + TC_MAM_PAGE_HEADER_SIZE, attr_size);
3140-
} else if ( type == TC_MAM_APP_FORMAT_VERSION ) {
3141-
arch_strncpy((char *)attr_data + 5, t_attr->app_format_ver, attr_size + TC_MAM_PAGE_HEADER_SIZE, attr_size);
3142-
} else if ( type == TC_MAM_LOCKED_MAM ) {
3143-
attr_data[5] = t_attr->vollock;
3144-
} else if ( type == TC_MAM_MEDIA_POOL) {
3145-
arch_strncpy((char *)attr_data + 5, t_attr->media_pool, attr_size + TC_MAM_PAGE_HEADER_SIZE, attr_size);
3145+
switch (type) {
3146+
case TC_MAM_APP_VENDER:
3147+
len = strnlen(t_attr->vender, attr_size);
3148+
memcpy(data, t_attr->vender, len);
3149+
break;
3150+
3151+
case TC_MAM_APP_NAME:
3152+
len = strnlen(t_attr->app_name, attr_size);
3153+
memcpy(data, t_attr->app_name, len);
3154+
break;
3155+
3156+
case TC_MAM_APP_VERSION:
3157+
len = strnlen(t_attr->app_ver, attr_size);
3158+
memcpy(data, t_attr->app_ver, len);
3159+
break;
3160+
3161+
case TC_MAM_USER_MEDIUM_LABEL:
3162+
len = strnlen(t_attr->medium_label, attr_size);
3163+
memcpy(data, t_attr->medium_label, len);
3164+
break;
3165+
3166+
case TC_MAM_TEXT_LOCALIZATION_IDENTIFIER:
3167+
data[0] = t_attr->tli;
3168+
break;
3169+
3170+
case TC_MAM_BARCODE:
3171+
len = strnlen(t_attr->barcode, attr_size);
3172+
memcpy(data, t_attr->barcode, len);
3173+
break;
3174+
3175+
case TC_MAM_APP_FORMAT_VERSION:
3176+
len = strnlen(t_attr->app_format_ver, attr_size);
3177+
memcpy(data, t_attr->app_format_ver, len);
3178+
break;
3179+
3180+
case TC_MAM_LOCKED_MAM:
3181+
data[0] = t_attr->vollock;
3182+
break;
3183+
3184+
case TC_MAM_MEDIA_POOL:
3185+
len = strnlen(t_attr->media_pool, attr_size);
3186+
memcpy(data, t_attr->media_pool, len);
3187+
break;
31463188
}
31473189

31483190
ret = dev->backend->write_attribute(dev->backend_data,
3149-
0, /* partition */
3150-
attr_data,
3151-
(attr_size + TC_MAM_PAGE_HEADER_SIZE));
3191+
0, /* partition */
3192+
attr_data,
3193+
attr_size + TC_MAM_PAGE_HEADER_SIZE);
31523194

31533195
if (ret < 0)
31543196
ltfsmsg(LTFS_ERR, 17205E, type, "tape_set_attribute_to_cm");
3197+
31553198
free(attr_data);
31563199
return ret;
3157-
31583200
}
31593201

31603202
/**
@@ -3232,12 +3274,13 @@ int tape_get_attribute_from_cm(struct device_data *dev, struct tape_attr *t_attr
32323274
{
32333275
int ret;
32343276
int attr_len;
3277+
unsigned char* attr_data = NULL;
32353278

32363279
CHECK_ARG_NULL(dev, -LTFS_NULL_ARG);
32373280
CHECK_ARG_NULL(t_attr, -LTFS_NULL_ARG);
32383281

32393282
switch (type) {
3240-
case TC_MAM_APP_VENDER:
3283+
case TC_MAM_APP_VENDER:
32413284
attr_len = TC_MAM_APP_VENDER_SIZE;
32423285
break;
32433286
case TC_MAM_APP_NAME:
@@ -3270,14 +3313,14 @@ int tape_get_attribute_from_cm(struct device_data *dev, struct tape_attr *t_attr
32703313
break;
32713314
}
32723315

3273-
unsigned char *attr_data = NULL;
3274-
attr_data = malloc(sizeof(char*) * (attr_len + TC_MAM_PAGE_HEADER_SIZE));
3275-
if (attr_data == NULL) return -LTFS_NO_MEMORY;
3316+
int attr_size = sizeof(char) * (attr_len + TC_MAM_PAGE_HEADER_SIZE);
3317+
attr_data = (unsigned char*)malloc(attr_size);
3318+
if (!attr_data) return -LTFS_NO_MEMORY;
32763319
ret = dev->backend->read_attribute(dev->backend_data,
3277-
0, /* partition */
3278-
type,
3279-
attr_data,
3280-
sizeof(attr_data));
3320+
0, /* partition */
3321+
type,
3322+
attr_data,
3323+
attr_size);
32813324

32823325
if (ret == 0) {
32833326
uint16_t id = ltfs_betou16(attr_data);
@@ -3486,10 +3529,10 @@ int update_tape_attribute(struct ltfs_volume *vol, const char *new_value, int ty
34863529
if (ret < 0) {
34873530
if ( type == TC_MAM_USER_MEDIUM_LABEL ) {
34883531
memset(vol->t_attr->medium_label, '\0', TC_MAM_USER_MEDIUM_LABEL_SIZE + 1);
3489-
arch_strncpy_auto(vol->t_attr->medium_label, pre_attr, strlen(pre_attr));
3532+
arch_strcpy_auto(vol->t_attr->medium_label, pre_attr);
34903533
} else if (type == TC_MAM_BARCODE) {
34913534
memset(vol->t_attr->barcode, '\0', TC_MAM_BARCODE_SIZE + 1);
3492-
arch_strncpy_auto(vol->t_attr->barcode, pre_attr, strlen(pre_attr));
3535+
arch_strcpy_auto(vol->t_attr->barcode, pre_attr);
34933536
}
34943537
}
34953538

0 commit comments

Comments
 (0)