Skip to content
Browse files

string_read(): do not destroy string length on read errors

  • Loading branch information...
1 parent 935637d commit a82e0df887bc9f2bb27aa016996c7dbd9a7e0c43 Sven-Haegar Koch committed May 26, 2011
Showing with 6 additions and 3 deletions.
  1. +1 −1 string/string.h
  2. +5 −2 string/string_read.c
View
2 string/string.h
@@ -36,7 +36,7 @@ void string_move(struct string*, struct string*);
int string_equals(struct string*, struct string*);
void string_lazyinit(struct string*, uintptr_t);
int string_initfromstringz(struct string*, const char *);
-int string_read(struct string*, int, uintptr_t, intptr_t*);
+int string_read(struct string*, const int, const size_t, intptr_t*);
static inline uintptr_t string_length(struct string *s) {
View
7 string/string_read.c
@@ -7,7 +7,7 @@
#include "string.h"
int
-string_read(struct string* s, int fd, uintptr_t len, intptr_t* bytes_read)
+string_read(struct string* s, const int fd, const size_t len, intptr_t* bytes_read)
{
uintptr_t growby;
char* buf;
@@ -24,7 +24,10 @@ string_read(struct string* s, int fd, uintptr_t len, intptr_t* bytes_read)
s->_u._s.size += growby;
s->s = buf;
}
+
*bytes_read = read(fd, s->s + s->_u._s.length, len);
- s->_u._s.length += *bytes_read;
+
+ if (*bytes_read > 0)
+ s->_u._s.length += *bytes_read;
return 0;
}

0 comments on commit a82e0df

Please sign in to comment.
Something went wrong with that request. Please try again.