Questions tagged [unsafe]

In C# or Rust, the unsafe keyword marks code able to work directly with memory pointers, bypassing some of the language's safety checks. In Java, `sun.misc.Unsafe` is a special class performing low-level, unsafe operations.

In C# The unsafe keyword denotes an unsafe context, which is required for any operation involving pointers. You can use the unsafe modifier in the declaration of a type or a member. Once so declared, the entire textual extent of the type or member is considered an unsafe context.

For more information, see Unsafe Code and Pointers (C# Programming Guide)

The unsafe Keyword on MSDN
http://msdn.microsoft.com/en-us/library/chfa2zb8(v=vs.100).aspx


In Java, sun.misc.Unsafe is a special class performing low-level, unsafe operations. Though this is a private platform API, this class is used extensively in many projects and libraries including Hadoop, Spark, Guava, Cassandra, etc.


In Rust, the unsafe keyword denotes a function or block that is implemented outside some of the safeguards of the compiler. Thus it is as much an escape hatch as a device to easily document code that might require increased attention. More information can be found in the Rust Book


In Go, package unsafe contains operations that step around the type safety of Go programs. Packages that import unsafe may be non-portable and are not protected by the Go 1 compatibility guidelines.

990 questions
0
votes
2 answers

Use of pointer like structure in C#

We are transfering our code from C++ to C# and due to limited knowledge of C# we are stuck into strange situation. Our problem is: In c++ we have 2-3 types of class/structures which have pointers to property (std::string), purpose of pointer is to…
vrajs5
  • 4,066
  • 1
  • 27
  • 44
0
votes
1 answer

What are best practices for `unsafe` functions in which only a small part of the code is actually doing `unsafe` things?

In the crate I'm developing I have several unsafe functions, which are marked as such because of reasons explained in this answer. In unsafe functions, I can perform unsafe operations as if the full function body was wrapped in an unsafe { } block.…
Lukas Kalbertodt
  • 79,749
  • 26
  • 255
  • 305
0
votes
1 answer

Getting pointer by &str

Consider this pseudocode: let k = 10; let ptr = &k as *const k; println!("{:p}", ptr); // prints address of pointer let addr = format!("{:p}", ptr); super-unsafe { // this would obviously be super unsafe. It may even cause a…
Thomas Braun
  • 505
  • 3
  • 14
0
votes
0 answers

Getting AccessViolationException after migrating to .NET 4.7.2 framework from 4.0

We have an integration with a third party software that involves passing XML data back and forth. We have to use a DLL import for this process, as they don't have a .NET library or API. Our project has been working fine, targeting the .NET 4.0…
0
votes
1 answer

Is it possible to protect a non-pinned managed array?

I need to protect the managed array from writing. I can do this by calling VirtualProtect. But GC can execute compact managed memory and move a not fixed array to another location. Will the access rights be transferred, or does the GC know nothing…
Lunar Whisper
  • 210
  • 1
  • 6
0
votes
2 answers

C# Problem with class variable, unsafe/fixed pointer assignment

Ok, I have been into some circles now and though I might ask this at SO. I have a class lets say Class A with some member variables and functions. I have a portion of unsafe code to which I need to pass the member variable as a reference and assign…
user349026
0
votes
1 answer

Does casting pointers in Rust have the same behavior as reinterpret_cast in C++?

I have this struct defined in my code: #[repr(align(4))] struct MetaDataDefn { cncVersion: i32, toDriverBufferLength: i32, toClientsBufferLength: i32, counterMetadataBufferLength: i32, counterValuesBuferLength: i32, …
user2296145
  • 276
  • 2
  • 8
0
votes
1 answer

Unsafe queue implementation

I try to create an unsafe, but more performant ArrayQueue implementation. After I added test cases, one of them produces a segmentation fault. Here is my simple minimal implementation: use std::mem; pub struct ArrayQueue { buff: Vec, …
Marin Veršić
  • 404
  • 4
  • 10
0
votes
1 answer

How can I get permission to write an unsafe code in ashx file?

How can I get permission to write an unsafe code in ashx file? When I use unsafe code in asp i encounter the following error. unsafe code may only appear if compiling with /unsafe
marandiiii
  • 21
  • 10
0
votes
1 answer

What does transmute::<*mut c_void, Option ()>> mean?

Here's some code I generated using c2rust and then cleaned up a bit: #![feature(libc)] extern crate libc; use libc::*; use std::mem::transmute; extern "C" { #[no_mangle] fn read(__fd: c_int, __buf: *mut c_void, __nbytes: c_ulong) ->…
d33tah
  • 10,999
  • 13
  • 68
  • 158
0
votes
1 answer

How to fix a SIGSEGV in this simple doubly linked list implementation?

I get a SIGSEGV error when running this code. The code compiles, the debugger shows what looks like random addresses in pointers. use std::ptr; pub struct List { head: *mut Node, tail: *mut Node, } struct Node { data:…
0
votes
0 answers

Marshal struct with fixed size array for unmanaged api call

I am creating a managed wrapper in C# for the DHCP server API and have a problem with a unmanaged struct that defines a fixed size buffer. I want to add a filter on a DHCP server and the API (dhcpsapi.h) defines the following: #define…
0
votes
1 answer

finalize never called from the object by allocateInstance(Java)

pleaes refer to the following code, i just want to do something about unsafe. import sun.misc.Unsafe; import java.lang.reflect.Field; import java.util.*; public class A { public static void main(String[] args) throws Exception { Field…
yaoweijq
  • 263
  • 2
  • 12
0
votes
1 answer

Can't get the same result as the Delphi code, after I exclusive-OR an address of a char array in C#

Parameters: InLong = 0, Posit = 5, and from an ASCII file TmPChar{.,STX,NUL,NUL} Delphi code Procedure TForm1.GetLongFromBuf(Var InLong : Longint; Posit : Integer; ZRepB : ZrepBuf); Var TmpPChar : Array[0..3] Of Char; PLong :…
Adonis Pso
  • 25
  • 7
0
votes
1 answer

Can I convert a value of IntPtr to another IntPtr without unsafe in C#?

Suppose I have a IntPtr which was a void** get from native code. When I was going to deference it to a void* equivalence IntPtr I used such code: unsafe { var innerPtr = (IntPtr)outerPtr.ToPointer(); } To do this I need to enable the unsafe…
jayatubi
  • 1,972
  • 1
  • 21
  • 51