Skip to content

Commit 74609bc

Browse files
committed
Add error for using null characters in export_name
1 parent 4fe88c0 commit 74609bc

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

src/librustc_typeck/collect.rs

+10-3
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

+8
Original file line numberDiff line numberDiff line change
@@ -4545,6 +4545,14 @@ 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+
```
4554+
"##,
4555+
45484556
E0689: r##"
45494557
This error indicates that the numeric value for the method being passed exists
45504558
but the type of the numeric value or binding could not be identified.

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

+14
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

+9
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)