diff src/share/vm/runtime/os.cpp @ 2469:677234770800

7017193: Small memory leak in get_stack_bounds os::create_stack_guard_pages Summary: getline() returns -1 but still allocate memory for str Reviewed-by: dcubed, coleenp
author dsamersoff
date Wed, 30 Mar 2011 19:38:07 +0400
parents fc416c2556ec
children 0654ee04b214 bf6481e5f96d
line wrap: on
line diff
--- a/src/share/vm/runtime/os.cpp	Mon Mar 28 12:48:08 2011 +0200
+++ b/src/share/vm/runtime/os.cpp	Wed Mar 30 19:38:07 2011 +0400
@@ -1291,3 +1291,41 @@
   }
   return result;
 }
+
+// Read file line by line, if line is longer than bsize,
+// skip rest of line.
+int os::get_line_chars(int fd, char* buf, const size_t bsize){
+  size_t sz, i = 0;
+
+  // read until EOF, EOL or buf is full
+  while ((sz = (int) read(fd, &buf[i], 1)) == 1 && i < (bsize-1) && buf[i] != '\n') {
+     ++i;
+  }
+
+  if (buf[i] == '\n') {
+    // EOL reached so ignore EOL character and return
+
+    buf[i] = 0;
+    return (int) i;
+  }
+
+  buf[i+1] = 0;
+
+  if (sz != 1) {
+    // EOF reached. if we read chars before EOF return them and
+    // return EOF on next call otherwise return EOF
+
+    return (i == 0) ? -1 : (int) i;
+  }
+
+  // line is longer than size of buf, skip to EOL
+  int ch;
+  while (read(fd, &ch, 1) == 1 && ch != '\n') {
+    // Do nothing
+  }
+
+  // return initial part of line that fits in buf.
+  // If we reached EOF, it will be returned on next call.
+
+  return (int) i;
+}