Skip to content

Instantly share code, notes, and snippets.

@powertomato
Last active September 25, 2018 11:17
Show Gist options
  • Save powertomato/341a32754b1c8702a98c8f5f15534a9e to your computer and use it in GitHub Desktop.
Save powertomato/341a32754b1c8702a98c8f5f15534a9e to your computer and use it in GitHub Desktop.
#include "netLink.h"
#include "StreamManager.h"
#include <iostream>
#include <sstream>
#include <string>
#include <memory>
static const int NUM_MAPS = 1;
static const int NUM_ELEMENTS = 1;
using MsgPackElement = std::unique_ptr<MsgPack::Element>;
using MsgPackArray = std::vector<std::unique_ptr<MsgPack::Element>>;
using MsgPackMap = std::map<std::string, std::unique_ptr<MsgPack::Element>>;
int main() {
netLink::init();
netLink::SocketManager socketManager;
std::shared_ptr<netLink::Socket> server = socketManager.newMsgPackSocket();
std::shared_ptr<netLink::Socket> client = nullptr;
server->setInputBufferSize(2);
server->setOutputBufferSize(2);
socketManager.onConnectRequest = [&client](netLink::SocketManager* manager, std::shared_ptr<netLink::Socket> serverSocket, std::shared_ptr<netLink::Socket> clientSocket) {
if(!client){
client = clientSocket;
netLink::MsgPackSocket& msgsocket = *static_cast<netLink::MsgPackSocket*>(client.get());
msgsocket.setInputBufferSize(2);
msgsocket.setOutputBufferSize(2);
std::cout << "client connected '" << msgsocket.hostRemote << "'" << std::endl;
return true;
}
return false;
};
bool run = true;
socketManager.onStatusChange = [&run, &client](netLink::SocketManager* manager, std::shared_ptr<netLink::Socket> socket, netLink::Socket::Status prev) {
if( socket->getStatus() == netLink::Socket::Status::NOT_CONNECTED && prev != netLink::Socket::Status::CONNECTING) {
client->setInputBufferSize(0);
client->setOutputBufferSize(0);
client.reset();
run = false;
}
};
socketManager.onReceiveMsgPack = [&run, &client](netLink::SocketManager *manager, std::shared_ptr<netLink::Socket> socket, MsgPackElement element) {
std::cout << "received: " << std::endl
<< *element << std::endl;
netLink::MsgPackSocket& msgsocket = *static_cast<netLink::MsgPackSocket*>(client.get());
msgsocket << MsgPack__Factory(ArrayHeader(1));
msgsocket << MsgPack__Factory(MapHeader(1));
msgsocket << MsgPack::Factory("fo");
msgsocket << MsgPack::Factory((uint64_t) 0);
};
try {
server->initAsTcpServer("*", 9998);
} catch (netLink::Exception & e){
std::cout << "Error " << e.code << std::endl;
run = false;
}
while(run){
socketManager.listen();
}
server->setInputBufferSize(0);
server->setOutputBufferSize(0);
return 0;
}
Dr. Memory version 1.11.0 build 2 built on Aug 29 2016 02:41:18
Dr. Memory results for pid 8344: "msgtest.exe"
Application cmdline: "bin\msgtest.exe"
Recorded 115 suppression(s) from default D:\STUFF\tools\DrMemory-Windows-1.11.0-2\bin64\suppress-default.txt
Error #1: UNADDRESSABLE ACCESS beyond heap bounds: writing 0x0000000003cbc752-0x0000000003cbc753 1 byte(s)
# 0 replace_memmove [d:\drmemory_package\drmemory\replace.c:774]
# 1 std::__get_helper [C:/msys64/mingw64/include/c++/7.3.0/tuple:1309]
# 2 netLink::Socket::getOutputBufferSize [C:/msys64/home/power/ziesel/msgtest/Socket.cpp:477]
# 3 netLink::Socket::xsputn [C:/msys64/home/power/ziesel/msgtest/Socket.cpp:167]
# 4 std::unique_ptr<>::get [C:/msys64/mingw64/include/c++/7.3.0/bits/unique_ptr.h:588]
# 5 MsgPack::Data::serialize [C:/msys64/home/power/ziesel/msgtest/MsgPack.cpp:248]
# 6 MsgPack::Serializer::serialize [C:/msys64/home/power/ziesel/msgtest/MsgPack.cpp:1009]
# 7 _ZNSt14_Function_base13_Base_managerIZN7MsgPack10Serializer9serializeERSt10unique_ptrINS1_7ElementESt14default_deleteIS4_EExEUlvE_E15_M_init_functorERSt9_Any_dataOS9_ [C:/msys64/mingw64/include/c++/7.3.0/bits/std_function.h:239]
# 8 MsgPack::Serializer::serialize [C:/msys64/home/power/ziesel/msgtest/MsgPack.cpp:1060]
# 9 std::deque<>::size [C:/msys64/mingw64/include/c++/7.3.0/bits/stl_deque.h:1272]
#10 MsgPack::Serializer::operator<< [C:/msys64/home/power/ziesel/msgtest/StreamManager.h:56]
#11 netLink::SocketManager::listen [C:/msys64/home/power/ziesel/msgtest/SocketManager.cpp:124]
#12 libstdc++-6.dll!? +0x0 (0x000000006fce2853 <libstdc++-6.dll+0xa2853>)
#13 libstdc++-6.dll!? +0x0 (0x000000006fce4f7a <libstdc++-6.dll+0xa4f7a>)
#14 WS2_32.dll!freeaddrinfo +0x48 (0x00007ffbcde837a9 <WS2_32.dll+0x137a9>)
#15 netLink::Socket::AddrinfoDestructor::operator() [C:/msys64/home/power/ziesel/msgtest/Socket.h:32]
#16 WS2_32.dll!getsockname +0x9a (0x00007ffbcde7e96b <WS2_32.dll+0xe96b>)
#17 atexit [C:/repo/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crtexe.c:431]
#18 __tmainCRTStartup [C:/repo/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crtexe.c:331]
#19 .l_start [C:/repo/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crtexe.c:215]
Note: @0:00:09.511 in thread 13300
Note: refers to 0 byte(s) beyond last valid byte in prior malloc
Note: prev lower malloc: 0x0000000003cbc750-0x0000000003cbc752
Note: allocated here:
Note: # 0 replace_operator_new_array [d:\drmemory_package\common\alloc_replace.c:2928]
Note: # 1 std::_Rb_tree<>::_Alloc_node::operator()<> [C:/msys64/mingw64/include/c++/7.3.0/bits/stl_tree.h:556]
Note: # 2 std::_Rb_tree<>::_M_insert_<> [C:/msys64/mingw64/include/c++/7.3.0/bits/stl_tree.h:1758]
Note: # 3 __gnu_cxx::__atomic_add_dispatch [C:/msys64/mingw64/include/c++/7.3.0/ext/atomicity.h:96]
Note: # 4 std::_Sp_counted_base<>::_M_add_ref_copy [C:/msys64/mingw64/include/c++/7.3.0/bits/shared_ptr_base.h:138]
Note: # 5 std::__shared_ptr<>::operator= [C:/msys64/mingw64/include/c++/7.3.0/bits/shared_ptr_base.h:1034]
Note: # 6 netLink::Socket::setOutputBufferSize [C:/msys64/home/power/ziesel/msgtest/Socket.cpp:495]
Note: # 7 _ZZ4mainENKUlPN7netLink13SocketManagerESt10shared_ptrINS_6SocketEES4_E_clES1_S4_S4_ [C:/msys64/home/power/ziesel/msgtest/msgtest.cpp:32]
Note: # 8 _ZNSt17_Function_handlerIFbPN7netLink13SocketManagerESt10shared_ptrINS0_6SocketEES5_EZ4mainEUlS2_S5_S5_E_E9_M_invokeERKSt9_Any_dataOS2_OS5_SD_ [C:/msys64/mingw64/include/c++/7.3.0/bits/std_function.h:302]
Note: # 9 std::function<>::operator() [C:/msys64/mingw64/include/c++/7.3.0/bits/std_function.h:706]
Note: #10 netLink::SocketManager::listen [C:/msys64/home/power/ziesel/msgtest/SocketManager.cpp:152]
Note: #11 libstdc++-6.dll!? +0x0 (0x000000006fce2853 <libstdc++-6.dll+0xa2853>)
Note: instruction: mov %cl -> (%rax)
Error #2: UNADDRESSABLE ACCESS beyond heap bounds: writing 0x0000000003cbc753-0x0000000003cbc754 1 byte(s)
# 0 replace_memmove [d:\drmemory_package\drmemory\replace.c:774]
# 1 _ZNSt11_Tuple_implILy0EJPN7MsgPack7ElementESt14default_deleteIS1_EEE7_M_headERS5_ [C:/msys64/mingw64/include/c++/7.3.0/tuple:195]
# 2 netLink::Socket::getOutputBufferSize [C:/msys64/home/power/ziesel/msgtest/Socket.cpp:477]
# 3 netLink::Socket::xsputn [C:/msys64/home/power/ziesel/msgtest/Socket.cpp:167]
# 4 _ZNSt6vectorISt4pairIPN7MsgPack7ElementExESaIS4_EE12emplace_backIJS4_EEEvDpOT_ [C:/msys64/mingw64/include/c++/7.3.0/bits/vector.tcc:100]
# 5 MsgPack::Number::serialize [C:/msys64/home/power/ziesel/msgtest/MsgPack.cpp:604]
# 6 MsgPack::Serializer::serialize [C:/msys64/home/power/ziesel/msgtest/MsgPack.cpp:1009]
# 7 _ZNSt14_Function_base13_Base_managerIZN7MsgPack10Serializer9serializeERSt10unique_ptrINS1_7ElementESt14default_deleteIS4_EExEUlvE_E15_M_init_functorERSt9_Any_dataOS9_ [C:/msys64/mingw64/include/c++/7.3.0/bits/std_function.h:239]
# 8 MsgPack::Serializer::serialize [C:/msys64/home/power/ziesel/msgtest/MsgPack.cpp:1060]
# 9 std::deque<>::size [C:/msys64/mingw64/include/c++/7.3.0/bits/stl_deque.h:1272]
#10 MsgPack::Serializer::operator<< [C:/msys64/home/power/ziesel/msgtest/StreamManager.h:56]
#11 netLink::SocketManager::listen [C:/msys64/home/power/ziesel/msgtest/SocketManager.cpp:124]
#12 libstdc++-6.dll!? +0x0 (0x000000006fce2853 <libstdc++-6.dll+0xa2853>)
#13 libstdc++-6.dll!? +0x0 (0x000000006fce4f7a <libstdc++-6.dll+0xa4f7a>)
#14 WS2_32.dll!freeaddrinfo +0x48 (0x00007ffbcde837a9 <WS2_32.dll+0x137a9>)
#15 netLink::Socket::AddrinfoDestructor::operator() [C:/msys64/home/power/ziesel/msgtest/Socket.h:32]
#16 WS2_32.dll!getsockname +0x9a (0x00007ffbcde7e96b <WS2_32.dll+0xe96b>)
#17 atexit [C:/repo/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crtexe.c:431]
#18 __tmainCRTStartup [C:/repo/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crtexe.c:331]
#19 .l_start [C:/repo/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crtexe.c:215]
Note: @0:00:09.517 in thread 13300
Note: refers to 1 byte(s) beyond last valid byte in prior malloc
Note: prev lower malloc: 0x0000000003cbc750-0x0000000003cbc752
Note: allocated here:
Note: # 0 replace_operator_new_array [d:\drmemory_package\common\alloc_replace.c:2928]
Note: # 1 std::_Rb_tree<>::_Alloc_node::operator()<> [C:/msys64/mingw64/include/c++/7.3.0/bits/stl_tree.h:556]
Note: # 2 std::_Rb_tree<>::_M_insert_<> [C:/msys64/mingw64/include/c++/7.3.0/bits/stl_tree.h:1758]
Note: # 3 __gnu_cxx::__atomic_add_dispatch [C:/msys64/mingw64/include/c++/7.3.0/ext/atomicity.h:96]
Note: # 4 std::_Sp_counted_base<>::_M_add_ref_copy [C:/msys64/mingw64/include/c++/7.3.0/bits/shared_ptr_base.h:138]
Note: # 5 std::__shared_ptr<>::operator= [C:/msys64/mingw64/include/c++/7.3.0/bits/shared_ptr_base.h:1034]
Note: # 6 netLink::Socket::setOutputBufferSize [C:/msys64/home/power/ziesel/msgtest/Socket.cpp:495]
Note: # 7 _ZZ4mainENKUlPN7netLink13SocketManagerESt10shared_ptrINS_6SocketEES4_E_clES1_S4_S4_ [C:/msys64/home/power/ziesel/msgtest/msgtest.cpp:32]
Note: # 8 _ZNSt17_Function_handlerIFbPN7netLink13SocketManagerESt10shared_ptrINS0_6SocketEES5_EZ4mainEUlS2_S5_S5_E_E9_M_invokeERKSt9_Any_dataOS2_OS5_SD_ [C:/msys64/mingw64/include/c++/7.3.0/bits/std_function.h:302]
Note: # 9 std::function<>::operator() [C:/msys64/mingw64/include/c++/7.3.0/bits/std_function.h:706]
Note: #10 netLink::SocketManager::listen [C:/msys64/home/power/ziesel/msgtest/SocketManager.cpp:152]
Note: #11 libstdc++-6.dll!? +0x0 (0x000000006fce2853 <libstdc++-6.dll+0xa2853>)
Note: instruction: mov %cl -> (%rax)
===========================================================================
FINAL SUMMARY:
DUPLICATE ERROR COUNTS:
SUPPRESSIONS USED:
ERRORS FOUND:
2 unique, 2 total unaddressable access(es)
0 unique, 0 total invalid heap argument(s)
0 unique, 0 total GDI usage error(s)
0 unique, 0 total handle leak(s)
0 unique, 0 total warning(s)
0 unique, 0 total, 0 byte(s) of leak(s)
0 unique, 0 total, 0 byte(s) of possible leak(s)
ERRORS IGNORED:
8 potential error(s) (suspected false positives)
(details: D:\STUFF\tools\DrMemory-Windows-1.11.0-2\drmemory\logs\DrMemory-msgtest.exe.8344.000\potential_errors.txt)
1 potential leak(s) (suspected false positives)
(details: D:\STUFF\tools\DrMemory-Windows-1.11.0-2\drmemory\logs\DrMemory-msgtest.exe.8344.000\potential_errors.txt)
37 unique, 62 total, 25800 byte(s) of still-reachable allocation(s)
(re-run with "-show_reachable" for details)
Details: D:\STUFF\tools\DrMemory-Windows-1.11.0-2\drmemory\logs\DrMemory-msgtest.exe.8344.000\results.txt
diff --git a/src/Socket.cpp b/src/Socket.cpp
index 5998881..bdf942a 100755
--- a/src/Socket.cpp
+++ b/src/Socket.cpp
@@ -139,7 +139,7 @@ int Socket::sync() {
if(pptr() == pbase()) // Allready in sync
return 0;
try {
- setp(pbase(), pptr()+send(pbase(), pptr()-pbase()));
+ setp(pbase(), pbase()+send(pbase(), pptr()-pbase()));
return 0;
} catch(Exception err) {
return EOF;
@@ -163,8 +163,11 @@ Socket::int_type Socket::underflow() {
}
std::streamsize Socket::xsputn(const char_type* buffer, std::streamsize size) {
- if(getOutputBufferSize()) // Write into buffer
+ std::streamsize avail = getOutputBufferSize();
+ if (avail >= size) // Write into buffer
return super::xsputn(buffer, size);
+ else
+ return super::xsputn(buffer, avail);
try {
return send(buffer, size);
} catch(Exception err) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment