Commit f9f21aa1 authored by Dmitriy Vyukov's avatar Dmitriy Vyukov Committed by Russ Cox

runtime: fix data race on runtime·maxstring

The data race can lead to erroneous output of
"[invalid string]" instead of a string.

R=golang-dev
CC=golang-dev
https://golang.org/cl/4678049
parent 8ed9fc60
...@@ -320,7 +320,7 @@ runtime·printpointer(void *p) ...@@ -320,7 +320,7 @@ runtime·printpointer(void *p)
void void
runtime·printstring(String v) runtime·printstring(String v)
{ {
extern int32 runtime·maxstring; extern uint32 runtime·maxstring;
if(v.len > runtime·maxstring) { if(v.len > runtime·maxstring) {
runtime·write(2, "[invalid string]", 16); runtime·write(2, "[invalid string]", 16);
......
...@@ -32,19 +32,23 @@ runtime·findnullw(uint16 *s) ...@@ -32,19 +32,23 @@ runtime·findnullw(uint16 *s)
return l; return l;
} }
int32 runtime·maxstring = 256; uint32 runtime·maxstring = 256;
String String
runtime·gostringsize(int32 l) runtime·gostringsize(int32 l)
{ {
String s; String s;
uint32 ms;
if(l == 0) if(l == 0)
return runtime·emptystring; return runtime·emptystring;
s.str = runtime·mal(l+1); // leave room for NUL for C runtime (e.g., callers of getenv) s.str = runtime·mal(l+1); // leave room for NUL for C runtime (e.g., callers of getenv)
s.len = l; s.len = l;
if(l > runtime·maxstring) for(;;) {
runtime·maxstring = l; ms = runtime·maxstring;
if((uint32)l <= ms || runtime·cas(&runtime·maxstring, ms, (uint32)l))
break;
}
return s; return s;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment