Skip to content

Conversation

@cjihrig
Copy link
Contributor

@cjihrig cjihrig commented Aug 25, 2017

Prior to this change, several of the fs functions checked for invalid arguments before initializing the fs request. If a consumer received a UV_EINVAL from one of these functions, and then called uv_fs_req_cleanup(), the application would crash, as the pointer being freed was not allocated. This commit makes sure that all fs functions initialize the request before returning.

Fixes: #1508

int r;

r = uv_fs_read(NULL, NULL, 0, NULL, 0, -1, NULL);
r = uv_fs_read(NULL, &read_req, 0, NULL, 0, -1, NULL);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hum, what should happen now if someone passes a NULL pointer then?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess nothing? Since uv_fs_cleanup would also do nothing if a NULL pointer is passed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It crashes because none of the req init logic checks for NULL. That would happen for other calls too though. I think we should either crash consistently or update the init logic to handle NULL.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the more I think about this, the more I think we should just assert on req for these functions. If we return UV_EINVAL in these cases, there is no way for the consumer to know if they can cleanup the request or not. It also seems pretty nonsensical to call any of these without a request.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@saghul I added a second commit to support NULL requests for all of the fs functions. The new logic is "if the request is NULL, immediately return UV_EINVAL. Otherwise, initialize the request before doing anything else." I think that should allow all requests to be cleaned up. PTAL

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add some tests which use a NULL req.

@cjihrig
Copy link
Contributor Author

cjihrig commented Aug 25, 2017

@cjihrig
Copy link
Contributor Author

cjihrig commented Aug 30, 2017

@saghul ping

Copy link
Member

@saghul saghul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay Colin! Left some comments.


#define INIT(subtype) \
do { \
if (req == NULL) \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same on Windows.

src/win/fs.c Outdated


int uv_fs_close(uv_loop_t* loop, uv_fs_t* req, uv_file fd, uv_fs_cb cb) {
if (req == NULL)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see every function looks like this now on Windows:

if (req == NULL)
  return UV_EINVAL;

 uv_fs_req_init(loop, req, UV_FS_FOO, cb);

Maybe use a macro to save some lines?

int r;

r = uv_fs_read(NULL, NULL, 0, NULL, 0, -1, NULL);
r = uv_fs_read(NULL, &read_req, 0, NULL, 0, -1, NULL);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add some tests which use a NULL req.

@cjihrig
Copy link
Contributor Author

cjihrig commented Sep 2, 2017

Copy link
Member

@saghul saghul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. One minor nit (which you are free to ignore): I think I'd combine the last and 2nd commits into a single one.

@cjihrig cjihrig force-pushed the einvals branch 2 times, most recently from 149d609 to d70385f Compare September 2, 2017 22:58
Prior to this change, several of the fs functions checked for
invalid arguments before initializing the fs request. If a
consumer received a UV_EINVAL from one of these functions, and
then called uv_fs_req_cleanup(), the application would crash, as
the pointer being freed was not allocated. This commit makes
sure that all fs functions initialize the request before returning.

Fixes: libuv#1508
PR-URL: libuv#1509
Reviewed-By: Bartosz Sosnowski <[email protected]>
Reviewed-By: Saúl Ibarra Corretgé <[email protected]>
This commit introduces an INIT macro to file system functions on
Windows, similar to the one used on Unix platforms. The macro
checks for NULL requests, and returns UV_EINVAL in such
scenarios. This commit also adds support for passing NULL to
uv_fs_req_cleanup(). In this scenario, the function is a
no-op.

Fixes: libuv#1508
PR-URL: libuv#1509
Reviewed-By: Bartosz Sosnowski <[email protected]>
Reviewed-By: Saúl Ibarra Corretgé <[email protected]>
This commit adds a POST macro to the Windows fs functions,
similar to the one used on Unix platforms.

PR-URL: libuv#1509
Reviewed-By: Saúl Ibarra Corretgé <[email protected]>
cjihrig added a commit that referenced this pull request Sep 2, 2017
Prior to this change, several of the fs functions checked for
invalid arguments before initializing the fs request. If a
consumer received a UV_EINVAL from one of these functions, and
then called uv_fs_req_cleanup(), the application would crash, as
the pointer being freed was not allocated. This commit makes
sure that all fs functions initialize the request before returning.

Fixes: #1508
PR-URL: #1509
Reviewed-By: Bartosz Sosnowski <[email protected]>
Reviewed-By: Saúl Ibarra Corretgé <[email protected]>
cjihrig added a commit that referenced this pull request Sep 2, 2017
This commit introduces an INIT macro to file system functions on
Windows, similar to the one used on Unix platforms. The macro
checks for NULL requests, and returns UV_EINVAL in such
scenarios. This commit also adds support for passing NULL to
uv_fs_req_cleanup(). In this scenario, the function is a
no-op.

Fixes: #1508
PR-URL: #1509
Reviewed-By: Bartosz Sosnowski <[email protected]>
Reviewed-By: Saúl Ibarra Corretgé <[email protected]>
cjihrig added a commit that referenced this pull request Sep 2, 2017
This commit adds a POST macro to the Windows fs functions,
similar to the one used on Unix platforms.

PR-URL: #1509
Reviewed-By: Saúl Ibarra Corretgé <[email protected]>
@cjihrig
Copy link
Contributor Author

cjihrig commented Sep 2, 2017

Squashed and landed as 7a0e64d, e539fc4, 9a4468f. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants