Skip to content

Commit 0b9b293

Browse files
authored
nintendoswitch: Fix invalid memory read / write in print calls
1 parent f20c932 commit 0b9b293

File tree

2 files changed

+8
-17
lines changed

2 files changed

+8
-17
lines changed

src/runtime/runtime_nintendoswitch.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,18 @@ func ticks() timeUnit {
4040
return timeUnit(ticksToNanoseconds(timeUnit(getArmSystemTick())))
4141
}
4242

43-
var stdoutBuffer = make([]byte, 0, 120)
43+
var stdoutBuffer = make([]byte, 120)
44+
var position = 0
4445

4546
func putchar(c byte) {
46-
if c == '\n' || len(stdoutBuffer)+1 >= 120 {
47-
NxOutputString(string(stdoutBuffer))
48-
stdoutBuffer = stdoutBuffer[:0]
47+
if c == '\n' || position > len(stdoutBuffer) {
48+
nxOutputString(&stdoutBuffer[0], uint64(position))
49+
position = 0
4950
return
5051
}
5152

52-
stdoutBuffer = append(stdoutBuffer, c)
53-
}
54-
55-
func usleep(usec uint) int {
56-
sleepThread(uint64(usec) * 1000)
57-
return 0
53+
stdoutBuffer[position] = c
54+
position++
5855
}
5956

6057
func abort() {

src/runtime/runtime_nintendoswitch_heap.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,11 @@
44

55
package runtime
66

7-
import "unsafe"
8-
97
const heapSize = 0x2000000 * 16 // Default by libnx
108

11-
//go:extern _stack_top
12-
var stackTopSymbol [0]byte
13-
149
var (
1510
heapStart = uintptr(0)
1611
heapEnd = uintptr(0)
17-
stackTop = uintptr(unsafe.Pointer(&stackTopSymbol))
1812
)
1913

2014
//export setHeapSize
@@ -24,7 +18,7 @@ func preinit() {
2418
setHeapSize(&heapStart, heapSize)
2519

2620
if heapStart == 0 {
27-
panic("failed to allocate heap")
21+
runtimePanic("failed to allocate heap")
2822
}
2923

3024
heapEnd = heapStart + heapSize

0 commit comments

Comments
 (0)