Fixed LinkMsgReader::ReadString() for empty strings I just broke before.

Extended the PortLinkTest application a bit (and updated it).


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@13003 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2005-06-08 03:29:10 +00:00
parent 0aa69a9cd4
commit be835bdf0b
2 changed files with 55 additions and 21 deletions
+7 -5
View File
@@ -254,11 +254,13 @@ LinkMsgReader::ReadString(char **_string)
return B_NO_MEMORY; return B_NO_MEMORY;
} }
status = Read(string, length); if (length > 0) {
if (status < B_OK) { status = Read(string, length);
free(string); if (status < B_OK) {
fRecvPosition -= sizeof(int32); // rewind the transaction free(string);
return status; fRecvPosition -= sizeof(int32); // rewind the transaction
return status;
}
} }
// make sure the string is null terminated // make sure the string is null terminated
+48 -16
View File
@@ -5,6 +5,24 @@
#include <string.h> #include <string.h>
const int32 kBufferSize = 2048;
void
get_next_message(BPortLink &link, int32 expectedCode)
{
int32 code;
if (link.GetNextReply(code) != B_OK) {
fprintf(stderr, "get message failed!\n");
exit(-1);
}
if (code != expectedCode) {
fprintf(stderr, "code is wrong (%ld)!\n", code);
exit(-1);
}
}
int int
main() main()
{ {
@@ -21,6 +39,22 @@ main()
sender.AttachString(""); sender.AttachString("");
sender.AttachString("Gurkensalat"); sender.AttachString("Gurkensalat");
sender.StartMessage('tst3', 100000);
sender.Attach(&port, 100000);
if (sender.EndMessage() == B_OK) {
fprintf(stderr, "attaching huge message succeeded!\n");
return -1;
}
// force overlap
char test[kBufferSize + 2048];
sender.StartMessage('tst4');
sender.Attach(test, kBufferSize - 40);
// force buffer grow
sender.StartMessage('tst5');
sender.Attach(test, sizeof(test));
status_t status = sender.Flush(); status_t status = sender.Flush();
if (status != B_OK) { if (status != B_OK) {
fprintf(stderr, "flushing messages failed: %ld, %s!\n", fprintf(stderr, "flushing messages failed: %ld, %s!\n",
@@ -28,13 +62,7 @@ main()
return -1; return -1;
} }
int32 code; get_next_message(receiver, 'tst1');
if (receiver.GetNextReply(&code) != B_OK) {
fprintf(stderr, "get message failed!\n");
return -1;
}
if (code != 'tst1')
fprintf(stderr, "code is wrong (%ld)!\n", code);
int32 value; int32 value;
if (receiver.Read<int32>(&value) != B_OK) { if (receiver.Read<int32>(&value) != B_OK) {
@@ -42,15 +70,12 @@ main()
return -1; return -1;
} }
if (value != 42) if (value != 42) {
fprintf(stderr, "value is wrong: %ld!\n", value); fprintf(stderr, "value is wrong: %ld!\n", value);
if (receiver.GetNextReply(&code) != B_OK) {
fprintf(stderr, "get message failed!\n");
return -1; return -1;
} }
if (code != 'tst2')
fprintf(stderr, "code is wrong (%ld)!\n", code); get_next_message(receiver, 'tst2');
for (int32 i = 0; i < 4; i++) { for (int32 i = 0; i < 4; i++) {
char *string; char *string;
@@ -67,9 +92,16 @@ main()
free(string); free(string);
} }
status = receiver.GetNextReply(&code, 0); get_next_message(receiver, 'tst4');
if (status != B_WOULD_BLOCK) get_next_message(receiver, 'tst5');
fprintf(stderr, "reading would not block!\n");
int32 code;
status = receiver.GetNextReply(code, 0);
if (status != B_WOULD_BLOCK) {
fprintf(stderr, "reading would not block!\n");
return -1;
}
puts("All OK!");
return 0; return 0;
} }