From 8123b61c014b8d045008b4ce5b78f902bf8e2d1d Mon Sep 17 00:00:00 2001 From: DarkWyrm Date: Fri, 3 Oct 2003 23:28:46 +0000 Subject: [PATCH] Eliminated a memory allocation bug Added a sensible workaround for BSession message code packaging git-svn-id: file:///srv/svn/repos/haiku/trunk/current@4937 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/kits/app/PortLink.cpp | 15 ++++++++++----- src/kits/app/PortMessage.cpp | 24 +++++++++++++++++++++--- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/kits/app/PortLink.cpp b/src/kits/app/PortLink.cpp index 0e0abe2f61..fd94bc1ba1 100644 --- a/src/kits/app/PortLink.cpp +++ b/src/kits/app/PortLink.cpp @@ -1,4 +1,3 @@ -#include #include #include "PortLink.h" #include "PortMessage.h" @@ -12,7 +11,7 @@ PortLink::PortLink(port_id port) fReceivePort=create_port(30,"PortLink reply port"); fSendCode=0; - fSendBuffer=(char*)malloc(4096); + fSendBuffer=new char[4096]; fSendPosition=4; } @@ -24,10 +23,15 @@ PortLink::PortLink( const PortLink &link ) fReceivePort=create_port(30,"PortLink reply port"); fSendCode = 0; - fSendBuffer = (char*)malloc(4096); + fSendBuffer=new char[4096]; fSendPosition = 4; } +PortLink::~PortLink(void) +{ + delete [] fSendBuffer; +} + void PortLink::SetOpCode( int32 code ) { fSendCode=code; @@ -104,7 +108,8 @@ status_t PortLink::FlushWithReply( PortMessage *msg,bigtime_t timeout=B_INFINITE // We got this far, so we apparently have some data msg->SetCode(rcode); msg->SetBuffer(rbuffer,rbuffersize,false); - + msg->BSessionWorkaround(); + return B_OK; } @@ -123,6 +128,6 @@ status_t PortLink::Attach(const void *data, size_t size) void PortLink::MakeEmpty() { - fSendPosition = 4; + fSendPosition=4; } diff --git a/src/kits/app/PortMessage.cpp b/src/kits/app/PortMessage.cpp index 696a242c46..5ab9ebd8db 100644 --- a/src/kits/app/PortMessage.cpp +++ b/src/kits/app/PortMessage.cpp @@ -34,6 +34,7 @@ PortMessage::PortMessage(const int32 &code, const void *buffer, const ssize_t &b { _code=code; SetBuffer(buffer,buffersize,copy); + is_session_msg=false; } PortMessage::PortMessage(void) @@ -42,6 +43,7 @@ PortMessage::PortMessage(void) _buffer=NULL; _buffersize=0; _index=NULL; + is_session_msg=false; } PortMessage::~PortMessage(void) @@ -59,6 +61,7 @@ status_t PortMessage::ReadFromPort(const port_id &port, const bigtime_t &timeout _index=NULL; _buffersize=0; + is_session_msg=false; if(timeout==B_INFINITE_TIMEOUT) { _buffersize=port_buffer_size(port); @@ -81,6 +84,7 @@ status_t PortMessage::ReadFromPort(const port_id &port, const bigtime_t &timeout read_port(port, &_code, _buffer, _buffersize); } _index=_buffer; + BSessionWorkaround(); return B_OK; } @@ -89,6 +93,7 @@ status_t PortMessage::WriteToPort(const port_id &port) { // Check port validity port_info pi; + is_session_msg=false; if(get_port_info(port,&pi)!=B_OK) return B_BAD_VALUE; @@ -138,8 +143,7 @@ status_t PortMessage::Read(void *data, ssize_t size) if(!data || size<1) return B_BAD_VALUE; - if( !_buffer || - _buffersize(_buffer+_buffersize)) ) return B_NO_MEMORY; @@ -151,6 +155,20 @@ status_t PortMessage::Read(void *data, ssize_t size) void PortMessage::Rewind(void) { - _index=_buffer; + _index=(is_session_msg)?_buffer+4:_buffer; } + +void PortMessage::BSessionWorkaround(void) +{ + // Do some stuff to work around BSession stupidity with packaging the message code as + // the first attachment in the message's data buffer. + if(_code==AS_SERVER_SESSION) + { + is_session_msg=true; + _index+=4; + _code=*((int32*)_buffer); + } + else + is_session_msg=false; +}