Skip to content

Commit

Permalink
Merge pull request #222 from Robo210/develop
Browse files Browse the repository at this point in the history
Fix crash when adding an invalid torrent file
  • Loading branch information
Robo210 committed Feb 1, 2016
2 parents 794f4ac + f6e29ed commit 9ae4e7b
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions src/scripting/modules/bittorrent/torrent_info_wrapper.cpp
@@ -1,4 +1,5 @@
#include <hadouken/scripting/modules/bittorrent/torrent_info_wrapper.hpp>
#include <boost/log/trivial.hpp>

#include <libtorrent/torrent_info.hpp>

Expand All @@ -13,25 +14,36 @@ duk_ret_t torrent_info_wrapper::construct(duk_context* ctx)
int t = duk_get_type(ctx, 0);
libtorrent::torrent_info* info;

if (t == DUK_TYPE_STRING)
try
{
std::string file(duk_require_string(ctx, 0));
// TODO: error handling
info = new libtorrent::torrent_info(file);
if (t == DUK_TYPE_STRING)
{
std::string file(duk_require_string(ctx, 0));
info = new libtorrent::torrent_info(file);
}
else if (t == DUK_TYPE_BUFFER)
{
duk_size_t size;
const char* buffer = static_cast<const char*>(duk_require_buffer(ctx, 0, &size));
info = new libtorrent::torrent_info(buffer, size);
}

duk_push_this(ctx);
common::set_pointer<libtorrent::torrent_info>(ctx, -2, info);

duk_push_c_function(ctx, finalize, 1);
duk_set_finalizer(ctx, -2);
}
else if (t == DUK_TYPE_BUFFER)
catch (const libtorrent::libtorrent_exception& ex)
{
duk_size_t size;
const char* buffer = static_cast<const char*>(duk_require_buffer(ctx, 0, &size));
// TODO: error handling
info = new libtorrent::torrent_info(buffer, size);
BOOST_LOG_TRIVIAL(warning) << "Invalid torrent file: " << ex.what();
return DUK_RET_UNSUPPORTED_ERROR;
}
catch (const std::exception& ex)
{
BOOST_LOG_TRIVIAL(warning) << "Error adding torrent: " << ex.what();
return DUK_RET_ERROR;
}

duk_push_this(ctx);
common::set_pointer<libtorrent::torrent_info>(ctx, -2, info);

duk_push_c_function(ctx, finalize, 1);
duk_set_finalizer(ctx, -2);

return 0;
}
Expand Down

0 comments on commit 9ae4e7b

Please sign in to comment.