Skip to content

Commit

Permalink
Merge pull request #96 from remicollet/issue-xattr
Browse files Browse the repository at this point in the history
Issue xattr
  • Loading branch information
eduardok authored Apr 1, 2023
2 parents b019c41 + c8e8001 commit 66c2c41
Showing 1 changed file with 37 additions and 12 deletions.
49 changes: 37 additions & 12 deletions smbclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -1663,10 +1663,15 @@ PHP_FUNCTION(smbclient_getxattr)
char *url, *name;
strsize_t url_len, name_len;
int retsize;
char values[1000];
int xattr_size;
zval *zstate;
smbc_getxattr_fn smbc_getxattr;
php_smbclient_state *state;
#if PHP_MAJOR_VERSION >= 7
zend_string *svalues = NULL;
#else
char *values = NULL;
#endif

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &zstate, &url, &url_len, &name, &name_len) == FAILURE) {
return;
Expand All @@ -1676,20 +1681,40 @@ PHP_FUNCTION(smbclient_getxattr)
if ((smbc_getxattr = smbc_getFunctionGetxattr(state->ctx)) == NULL) {
RETURN_FALSE;
}
/* TODO: 1000 chars should be enough for everyone...?
* However, doing an initial blank call to determine the response size
* seems wasteful, and vulnerable to a time-of-check, time-of-use
* error. */
if ((retsize = smbc_getxattr(state->ctx, url, name, values, sizeof(values))) >= 0) {
if (retsize > sizeof(values)) {
retsize = sizeof(values);
}

xattr_size = smbc_getxattr(state->ctx, url, name, NULL, 0);

if (xattr_size < 0) {
goto fail;
}

if (xattr_size == 0) {
RETURN_EMPTY_STRING();
}

#if PHP_MAJOR_VERSION >= 7
RETURN_STRINGL(values, retsize);
svalues = zend_string_alloc(xattr_size, 0);
retsize = smbc_getxattr(state->ctx, url, name, ZSTR_VAL(svalues), xattr_size + 1);
if (retsize > xattr_size) { /* time-of-check, time-of-use error */
retsize = xattr_size;
} else if (retsize < 0) {
zend_string_release(svalues);
goto fail;
}
RETURN_STR(svalues);
#else
RETURN_STRINGL(values, retsize, 1);
values = emalloc(xattr_size + 1);
retsize = smbc_getxattr(state->ctx, url, name, values, xattr_size + 1);
if (retsize > xattr_size) { /* time-of-check, time-of-use error */
retsize = xattr_size;
} else if (retsize < 0) {
efree(values);
goto fail;
}
RETURN_STRINGL(values, retsize, 0);
#endif
}

fail:
hide_password(url, url_len);
switch (state->err = errno) {
case EINVAL: php_error(E_WARNING, "Couldn't get xattr for %s: library not initialized or incorrect parameter", url); break;
Expand Down

0 comments on commit 66c2c41

Please sign in to comment.