Skip to content

Commit 118d209

Browse files
Rollup merge of #51747 - varkor:export_name-null-character, r=estebank
Add error for using null characters in #[export_name] Fixes #51741.
2 parents eb981cf + f94c075 commit 118d209

File tree

6 files changed

+44
-10
lines changed

6 files changed

+44
-10
lines changed

src/librustc_codegen_llvm/diagnostics.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,3 @@ unsafe { simd_add(i32x2(0, 0), i32x2(1, 2)); } // ok!
4848
"##,
4949

5050
}
51-
52-
53-
register_diagnostics! {
54-
E0558
55-
}

src/librustc_typeck/collect.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1898,11 +1898,18 @@ fn codegen_fn_attrs<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, id: DefId) -> Codegen
18981898
}
18991899
});
19001900
} else if attr.check_name("export_name") {
1901-
if let s @ Some(_) = attr.value_str() {
1902-
codegen_fn_attrs.export_name = s;
1901+
if let Some(s) = attr.value_str() {
1902+
if s.as_str().contains("\0") {
1903+
// `#[export_name = ...]` will be converted to a null-terminated string,
1904+
// so it may not contain any null characters.
1905+
struct_span_err!(tcx.sess, attr.span, E0648,
1906+
"`export_name` may not contain null characters")
1907+
.emit();
1908+
}
1909+
codegen_fn_attrs.export_name = Some(s);
19031910
} else {
19041911
struct_span_err!(tcx.sess, attr.span, E0558,
1905-
"export_name attribute has invalid format")
1912+
"`export_name` attribute has invalid format")
19061913
.span_label(attr.span, "did you mean #[export_name=\"*\"]?")
19071914
.emit();
19081915
}

src/librustc_typeck/diagnostics.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3709,7 +3709,7 @@ The `export_name` attribute was malformed.
37093709
Erroneous code example:
37103710
37113711
```ignore (error-emitted-at-codegen-which-cannot-be-handled-by-compile_fail)
3712-
#[export_name] // error: export_name attribute has invalid format
3712+
#[export_name] // error: `export_name` attribute has invalid format
37133713
pub fn something() {}
37143714
37153715
fn main() {}
@@ -4545,6 +4545,15 @@ fn start(_: isize, _: *const *const u8) -> isize where (): Copy {
45454545
```
45464546
"##,
45474547

4548+
E0648: r##"
4549+
`export_name` attributes may not contain null characters (`\0`).
4550+
4551+
```compile_fail,E0648
4552+
#[export_name="\0foo"] // error: `export_name` may not contain null characters
4553+
pub fn bar() {}
4554+
```
4555+
"##,
4556+
45484557
E0689: r##"
45494558
This error indicates that the numeric value for the method being passed exists
45504559
but the type of the numeric value or binding could not be identified.

src/test/ui/error-codes/E0558.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0558]: export_name attribute has invalid format
1+
error[E0558]: `export_name` attribute has invalid format
22
--> $DIR/E0558.rs:11:1
33
|
44
LL | #[export_name]

src/test/ui/error-codes/E0648.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[export_name="\0foo"] //~ ERROR E0648
12+
pub fn bar() {}
13+
14+
fn main() {}

src/test/ui/error-codes/E0648.stderr

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0648]: `export_name` may not contain null characters
2+
--> $DIR/E0648.rs:11:1
3+
|
4+
LL | #[export_name="/0foo"] //~ ERROR E0648
5+
| ^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0648`.

0 commit comments

Comments
 (0)