Skip to content

Commit 67eaa1d

Browse files
committed
fix: don't lint null as UB for volatile
Also remove a now-unneeded `allow` line.
1 parent 03e97d0 commit 67eaa1d

File tree

4 files changed

+17
-52
lines changed

4 files changed

+17
-52
lines changed

compiler/rustc_lint/src/ptr_nulls.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,10 @@ impl<'tcx> LateLintPass<'tcx> for PtrNullChecks {
160160
let (arg_indices, are_zsts_allowed): (&[_], _) = match diag_name {
161161
sym::ptr_read
162162
| sym::ptr_read_unaligned
163-
| sym::ptr_read_volatile
164163
| sym::ptr_replace
165164
| sym::ptr_write
166165
| sym::ptr_write_bytes
167-
| sym::ptr_write_unaligned
168-
| sym::ptr_write_volatile => (&[0], true),
166+
| sym::ptr_write_unaligned => (&[0], true),
169167
sym::slice_from_raw_parts | sym::slice_from_raw_parts_mut => (&[0], false),
170168
sym::ptr_copy
171169
| sym::ptr_copy_nonoverlapping

tests/ui/lint/invalid_null_args.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,9 @@ unsafe fn null_ptr() {
5858
let _a: A = ptr::read_unaligned(ptr::null_mut());
5959
//~^ ERROR calling this function with a null pointer is undefined behavior
6060

61+
// These two should *not* fire the lint.
6162
let _a: A = ptr::read_volatile(ptr::null());
62-
//~^ ERROR calling this function with a null pointer is undefined behavior
6363
let _a: A = ptr::read_volatile(ptr::null_mut());
64-
//~^ ERROR calling this function with a null pointer is undefined behavior
6564

6665
let _a: A = ptr::replace(ptr::null_mut(), v);
6766
//~^ ERROR calling this function with a null pointer is undefined behavior
@@ -82,8 +81,8 @@ unsafe fn null_ptr() {
8281
ptr::write_unaligned(ptr::null_mut(), v);
8382
//~^ ERROR calling this function with a null pointer is undefined behavior
8483

84+
// This one should *not* fire the lint.
8585
ptr::write_volatile(ptr::null_mut(), v);
86-
//~^ ERROR calling this function with a null pointer is undefined behavior
8786

8887
ptr::write_bytes::<usize>(ptr::null_mut(), 42, 0);
8988
//~^ ERROR calling this function with a null pointer is undefined behavior

tests/ui/lint/invalid_null_args.stderr

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -164,27 +164,7 @@ LL | let _a: A = ptr::read_unaligned(ptr::null_mut());
164164
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
165165

166166
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
167-
--> $DIR/invalid_null_args.rs:61:17
168-
|
169-
LL | let _a: A = ptr::read_volatile(ptr::null());
170-
| ^^^^^^^^^^^^^^^^^^^-----------^
171-
| |
172-
| null pointer originates from here
173-
|
174-
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
175-
176-
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
177-
--> $DIR/invalid_null_args.rs:63:17
178-
|
179-
LL | let _a: A = ptr::read_volatile(ptr::null_mut());
180-
| ^^^^^^^^^^^^^^^^^^^---------------^
181-
| |
182-
| null pointer originates from here
183-
|
184-
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
185-
186-
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
187-
--> $DIR/invalid_null_args.rs:66:17
167+
--> $DIR/invalid_null_args.rs:65:17
188168
|
189169
LL | let _a: A = ptr::replace(ptr::null_mut(), v);
190170
| ^^^^^^^^^^^^^---------------^^^^
@@ -194,7 +174,7 @@ LL | let _a: A = ptr::replace(ptr::null_mut(), v);
194174
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
195175

196176
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
197-
--> $DIR/invalid_null_args.rs:69:5
177+
--> $DIR/invalid_null_args.rs:68:5
198178
|
199179
LL | ptr::swap::<A>(ptr::null_mut(), &mut v);
200180
| ^^^^^^^^^^^^^^^---------------^^^^^^^^^
@@ -204,7 +184,7 @@ LL | ptr::swap::<A>(ptr::null_mut(), &mut v);
204184
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
205185

206186
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
207-
--> $DIR/invalid_null_args.rs:71:5
187+
--> $DIR/invalid_null_args.rs:70:5
208188
|
209189
LL | ptr::swap::<A>(&mut v, ptr::null_mut());
210190
| ^^^^^^^^^^^^^^^^^^^^^^^---------------^
@@ -214,7 +194,7 @@ LL | ptr::swap::<A>(&mut v, ptr::null_mut());
214194
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
215195

216196
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
217-
--> $DIR/invalid_null_args.rs:74:5
197+
--> $DIR/invalid_null_args.rs:73:5
218198
|
219199
LL | ptr::swap_nonoverlapping::<A>(ptr::null_mut(), &mut v, 0);
220200
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------^^^^^^^^^^^^
@@ -224,7 +204,7 @@ LL | ptr::swap_nonoverlapping::<A>(ptr::null_mut(), &mut v, 0);
224204
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
225205

226206
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
227-
--> $DIR/invalid_null_args.rs:76:5
207+
--> $DIR/invalid_null_args.rs:75:5
228208
|
229209
LL | ptr::swap_nonoverlapping::<A>(&mut v, ptr::null_mut(), 0);
230210
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------^^^^
@@ -234,7 +214,7 @@ LL | ptr::swap_nonoverlapping::<A>(&mut v, ptr::null_mut(), 0);
234214
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
235215

236216
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
237-
--> $DIR/invalid_null_args.rs:79:5
217+
--> $DIR/invalid_null_args.rs:78:5
238218
|
239219
LL | ptr::write(ptr::null_mut(), v);
240220
| ^^^^^^^^^^^---------------^^^^
@@ -244,7 +224,7 @@ LL | ptr::write(ptr::null_mut(), v);
244224
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
245225

246226
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
247-
--> $DIR/invalid_null_args.rs:82:5
227+
--> $DIR/invalid_null_args.rs:81:5
248228
|
249229
LL | ptr::write_unaligned(ptr::null_mut(), v);
250230
| ^^^^^^^^^^^^^^^^^^^^^---------------^^^^
@@ -254,17 +234,7 @@ LL | ptr::write_unaligned(ptr::null_mut(), v);
254234
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
255235

256236
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
257-
--> $DIR/invalid_null_args.rs:85:5
258-
|
259-
LL | ptr::write_volatile(ptr::null_mut(), v);
260-
| ^^^^^^^^^^^^^^^^^^^^---------------^^^^
261-
| |
262-
| null pointer originates from here
263-
|
264-
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
265-
266-
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
267-
--> $DIR/invalid_null_args.rs:88:5
237+
--> $DIR/invalid_null_args.rs:87:5
268238
|
269239
LL | ptr::write_bytes::<usize>(ptr::null_mut(), 42, 0);
270240
| ^^^^^^^^^^^^^^^^^^^^^^^^^^---------------^^^^^^^^
@@ -274,7 +244,7 @@ LL | ptr::write_bytes::<usize>(ptr::null_mut(), 42, 0);
274244
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
275245

276246
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
277-
--> $DIR/invalid_null_args.rs:93:18
247+
--> $DIR/invalid_null_args.rs:92:18
278248
|
279249
LL | let _a: u8 = ptr::read(const_ptr);
280250
| ^^^^^^^^^^^^^^^^^^^^
@@ -287,7 +257,7 @@ LL | let null_ptr = ptr::null_mut();
287257
| ^^^^^^^^^^^^^^^
288258

289259
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
290-
--> $DIR/invalid_null_args.rs:100:5
260+
--> $DIR/invalid_null_args.rs:99:5
291261
|
292262
LL | std::slice::from_raw_parts::<()>(ptr::null(), 0);
293263
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-----------^^^^
@@ -297,7 +267,7 @@ LL | std::slice::from_raw_parts::<()>(ptr::null(), 0);
297267
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
298268

299269
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
300-
--> $DIR/invalid_null_args.rs:102:5
270+
--> $DIR/invalid_null_args.rs:101:5
301271
|
302272
LL | std::slice::from_raw_parts::<Zst>(ptr::null(), 0);
303273
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-----------^^^^
@@ -307,7 +277,7 @@ LL | std::slice::from_raw_parts::<Zst>(ptr::null(), 0);
307277
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
308278

309279
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
310-
--> $DIR/invalid_null_args.rs:104:5
280+
--> $DIR/invalid_null_args.rs:103:5
311281
|
312282
LL | std::slice::from_raw_parts_mut::<()>(ptr::null_mut(), 0);
313283
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------^^^^
@@ -317,7 +287,7 @@ LL | std::slice::from_raw_parts_mut::<()>(ptr::null_mut(), 0);
317287
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
318288

319289
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
320-
--> $DIR/invalid_null_args.rs:106:5
290+
--> $DIR/invalid_null_args.rs:105:5
321291
|
322292
LL | std::slice::from_raw_parts_mut::<Zst>(ptr::null_mut(), 0);
323293
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------^^^^
@@ -326,5 +296,5 @@ LL | std::slice::from_raw_parts_mut::<Zst>(ptr::null_mut(), 0);
326296
|
327297
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
328298

329-
error: aborting due to 31 previous errors
299+
error: aborting due to 28 previous errors
330300

tests/ui/precondition-checks/read_volatile.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
//@ error-pattern: unsafe precondition(s) violated: ptr::read_volatile requires
44
//@ revisions: misaligned
55

6-
#![allow(invalid_null_arguments)]
7-
86
use std::ptr;
97

108
fn main() {

0 commit comments

Comments
 (0)