1
0
mirror of https://github.com/golang/go synced 2024-11-19 15:54:46 -07:00

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
This commit is contained in:
Dmitriy Vyukov 2011-07-12 01:21:06 -04:00 committed by Russ Cox
parent 8ed9fc600c
commit f9f21aa1fb
2 changed files with 8 additions and 4 deletions

View File

@ -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);

View File

@ -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;
} }