Avoided to call calloc() for 0 bytes when reading Linux xattr.

This commit is contained in:
Thomas Schmitt 2011-08-22 12:37:11 +02:00
parent ce19db5e19
commit 8438db02cf

View File

@ -186,16 +186,16 @@ int aaip_get_attr_list(char *path, size_t *num_attrs, char ***names,
size_t **value_lengths, char ***values, int flag) size_t **value_lengths, char ***values, int flag)
{ {
int ret; int ret;
char *list= NULL; ssize_t i, num_names= 0;
ssize_t list_size= 0, i, num_names= 0;
unsigned char *acl= NULL;
char *a_acl_text= NULL, *d_acl_text= NULL;
#ifdef Libisofs_with_aaip_acL #ifdef Libisofs_with_aaip_acL
unsigned char *acl= NULL;
char *a_acl_text= NULL, *d_acl_text= NULL;
size_t acl_len= 0; size_t acl_len= 0;
#endif #endif
#ifdef Libisofs_with_aaip_xattR #ifdef Libisofs_with_aaip_xattR
ssize_t value_ret, retry= 0; char *list= NULL;
ssize_t value_ret, retry= 0, list_size= 0;
#endif #endif
if(flag & (1 << 15)) { /* Free memory */ if(flag & (1 << 15)) { /* Free memory */
@ -208,39 +208,38 @@ int aaip_get_attr_list(char *path, size_t *num_attrs, char ***names,
*values= NULL; *values= NULL;
/* Set up arrays */ /* Set up arrays */
if(!(flag & 4)) { /* Get xattr names */
#ifdef Libisofs_with_aaip_xattR #ifdef Libisofs_with_aaip_xattR
if(!(flag & 4)) { /* Get xattr names */
if(flag & 32) if(flag & 32)
list_size= listxattr(path, list, 0); list_size= listxattr(path, list, 0);
else else
list_size= llistxattr(path, list, 0); list_size= llistxattr(path, list, 0);
if(list_size == -1) if(list_size == -1)
{ret= -1; goto ex;} {ret= -1; goto ex;}
list= calloc(list_size, 1); if(list_size > 0) {
if(list == NULL) list= calloc(list_size, 1);
{ret= -1; goto ex;} if(list == NULL)
if(flag & 32) {ret= -1; goto ex;}
list_size= listxattr(path, list, list_size); if(flag & 32)
else list_size= listxattr(path, list, list_size);
list_size= llistxattr(path, list, list_size); else
if(list_size == -1) list_size= llistxattr(path, list, list_size);
{ret= -1; goto ex;} if(list_size == -1)
{ret= -1; goto ex;}
#else /* Libisofs_with_aaip_xattR */ }
list= strdup("");
#endif /* ! Libisofs_with_aaip_xattR */
for(i= 0; i < list_size; i+= strlen(list + i) + 1) for(i= 0; i < list_size; i+= strlen(list + i) + 1)
num_names++; num_names++;
} }
#endif /* ! Libisofs_with_aaip_xattR */
#ifdef Libisofs_with_aaip_acL #ifdef Libisofs_with_aaip_acL
if(flag & 1) if(flag & 1)
num_names++; num_names++;
#endif #endif
if(num_names == 0) if(num_names == 0)
@ -256,7 +255,10 @@ int aaip_get_attr_list(char *path, size_t *num_attrs, char ***names,
(*values)[i]= NULL; (*values)[i]= NULL;
(*value_lengths)[i]= 0; (*value_lengths)[i]= 0;
} }
if(!(flag & 4)) {
#ifdef Libisofs_with_aaip_xattR
if(!(flag & 4)) { /* Get xattr values */
for(i= 0; i < list_size && (size_t) num_names > *num_attrs; for(i= 0; i < list_size && (size_t) num_names > *num_attrs;
i+= strlen(list + i) + 1) { i+= strlen(list + i) + 1) {
if(!(flag & 8)) if(!(flag & 8))
@ -266,11 +268,6 @@ int aaip_get_attr_list(char *path, size_t *num_attrs, char ***names,
if((*names)[(*num_attrs) - 1] == NULL) if((*names)[(*num_attrs) - 1] == NULL)
{ret= -1; goto ex;} {ret= -1; goto ex;}
} }
}
#ifdef Libisofs_with_aaip_xattR
if(!(flag & 4)) { /* Get xattr values */
for(i= 0; (size_t) i < *num_attrs; i++) { for(i= 0; (size_t) i < *num_attrs; i++) {
if(!(flag & 8)) if(!(flag & 8))
if(strncmp((*names)[i], "user.", 5)) if(strncmp((*names)[i], "user.", 5))
@ -328,12 +325,19 @@ int aaip_get_attr_list(char *path, size_t *num_attrs, char ***names,
ret= 1; ret= 1;
ex:; ex:;
#ifdef Libisofs_with_aaip_acL
if(a_acl_text != NULL) if(a_acl_text != NULL)
aaip_get_acl_text("", &a_acl_text, 1 << 15); /* free */ aaip_get_acl_text("", &a_acl_text, 1 << 15); /* free */
if(d_acl_text != NULL) if(d_acl_text != NULL)
aaip_get_acl_text("", &d_acl_text, 1 << 15); /* free */ aaip_get_acl_text("", &d_acl_text, 1 << 15); /* free */
if(acl != NULL)
free(acl);
#endif
#ifdef Libisofs_with_aaip_xattR
if(list != NULL) if(list != NULL)
free(list); free(list);
#endif
if(ret <= 0 || (flag & (1 << 15))) { if(ret <= 0 || (flag & (1 << 15))) {
if(*names != NULL) { if(*names != NULL) {
for(i= 0; (size_t) i < *num_attrs; i++) for(i= 0; (size_t) i < *num_attrs; i++)
@ -349,8 +353,6 @@ ex:;
free((*values)[i]); free((*values)[i]);
free(*values); free(*values);
} }
if(acl != NULL)
free(acl);
*values= NULL; *values= NULL;
*num_attrs= 0; *num_attrs= 0;
} }