C语言 编译错误:程序等中的stray '\302'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19198332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compilation error: stray ‘\302’ in program etc
提问by Ahmed Taher
I am having problem compiling the followed exploit code:
我在编译以下漏洞利用代码时遇到问题:
http://downloads.securityfocus.com/vulnerabilities/exploits/59846-1.c
http://downloads.securityfocus.com/vulnerabilities/exploits/59846-1.c
I am using: "gcc file.c" and "gcc -O2 file.c" but both of them gets the following errors:
我正在使用:“gcc file.c”和“gcc -O2 file.c”,但它们都出现以下错误:
sorbolinux-exec.c: In function ‘sc':
sorbolinux-exec.c:76: error: stray ‘2' in program
sorbolinux-exec.c:76: error: stray ‘4' in program
sorbolinux-exec.c:76: error: ‘t' undeclared (first use in this function)
sorbolinux-exec.c:76: error: (Each undeclared identifier is reported only once
sorbolinux-exec.c:76: error: for each function it appears in.)
I tried compiling them on both Kali linux and Ubuntu 10.04 and get the same result.
我尝试在 Kali linux 和 Ubuntu 10.04 上编译它们并得到相同的结果。
采纳答案by Yu Hao
You have an invalid character on that line. This is what I saw:
您在该行中有一个无效字符。这是我看到的:


回答by Klaus
You have invalid chars in your source. If you don't have any valid non ascii chars in your source, maybe in a double quoted string literal, you can simply convert your file back to ascii with:
您的源中有无效字符。如果您的源代码中没有任何有效的非 ascii 字符,也许在双引号字符串文字中,您可以简单地将您的文件转换回 ascii:
tr -cd '-6' < old.c > new.c
Edit: method with iconv will stop at wrong chars which makes no sense. The above command line is working with the example file. Good luck :-)
编辑:带有 iconv 的方法将在错误的字符处停止,这是没有意义的。上面的命令行正在使用示例文件。祝你好运 :-)
回答by TheMagicCow
I got the same with a character that visibly appeared as an asterisk, but was a UTF-8 sequence instead.
我得到了一个明显显示为星号的字符,但它是一个 UTF-8 序列。
Encoder * st;
When compiled returned:
编译后返回:
g.c:2:1: error: stray ‘2' in program
g.c:2:1: error: stray ‘0' in program
g.c:2:1: error: stray ‘7' in program
342 210 227 turns out to be UTF-8 for ASTERISK OPERATOR.
342 210 227 原来是 ASTERISK OPERATOR 的 UTF-8。
Deleting the '*' and typing it again fixed the problem.
删除“*”并再次输入它解决了问题。
回答by visu
Whenever compiler found special character .. it gives these king of compile error .... what error i found is as following
每当编译器发现特殊字符..它给这些编译错误之王..我发现的错误如下
error: stray '\302' in program and error: stray '\240' in program
错误:程序中的“\302”和错误:程序中的“\240”
....
....
Some piece of code i copied from chatting messanger. In messanger it was special character only.. after copiying into vim editor it changed to correct character only. But compiler was giving above error .. then .. that stamenet i wrote mannualy after .. it got resolve.. :)
我从聊天信使中复制的一些代码。在messanger中,它只是特殊字符。复制到vim编辑器后,它只更改为正确的字符。但是编译器给出了上面的错误..然后..我在之后手动写的那个stamenet..它得到了解决..:)
回答by Agrim Gupta
It's perhaps because you copied code from net ( from a site which has perhaps not an ASCII encoded page, but UTF-8 encoded page), so you can convert the code to ASCII from this site :
这可能是因为您从网络(从可能不是 ASCII 编码的页面,而是 UTF-8 编码的页面的站点)复制了代码,因此您可以从该站点将代码转换为 ASCII:
"http://www.percederberg.net/tools/text_converter.html"
" http://www.percederberg.net/tools/text_converter.html"
There you can either detect errors manually by converting it back to UTF-8, or you can automatically convert it to ASCII and remove all the stray characters.
在那里,您可以通过将错误转换回 UTF-8 来手动检测错误,也可以自动将其转换为 ASCII 并删除所有杂散字符。
回答by J.M.I. MADISON
Sure, convert the file to ascii and blast all unicode characters away. It will probably work.... BUT...
当然,将文件转换为 ascii 并清除所有 unicode 字符。 它可能会工作......但是......
- You won't know what you fixed.
- It will also destroy any unicode comments. Ex: //: A2+B2=C2
- It could potentially damage obvious logic, the code will still be broken, but the solution less obvious. For example: A string with "Smart-Quotes" (“ & ”) or a pointer with a full-width astrix ( * ). Now “SOME_THING” looks like a #define ( SOME_THING ) and *SomeType is the wrong type ( SomeType ).
- 你不会知道你修复了什么。
- 它还会破坏任何 unicode 注释。例如://:A2+B2=C2
- 它可能潜在地破坏明显的逻辑,代码仍然会被破坏,但解决方案不太明显。例如:带有“Smart-Quotes”(“ & ”)的字符串或带有全角星号( * )的指针。现在“SOME_THING”看起来像一个#define ( SOME_THING ) 并且 *SomeType 是错误的类型 ( SomeType )。
Two more sugrical approaches to fixing the problem:
解决问题的两种更简单的方法:
- Switch fonts to see the character. (It might be invisible in your current font)
Regex search all unicode characters not part non-extended ascii. In notepad++ I can search up to FFFF, which hasn't failed me yet.
[\x{80}-\x{FFFF}]
80 is hex for 128, the first extended ascii character.
After hitting "find next" and highlighting what appears to be empty space, you can close your search dialog and press CTRL+C to copy to clipboard.
Then paste the character into a unicode search tool. I usually use an online one. http://unicode.scarfboy.com/
- 切换字体以查看字符。(它可能在您当前的字体中不可见)
正则表达式搜索所有 unicode 字符,而不是非扩展 ascii 的一部分。在记事本++中,我最多可以搜索FFFF,这还没有让我失望。
[\x{80}-\x{FFFF}]
80 是 128 的十六进制,第一个扩展的 ascii 字符。
点击“查找下一个”并突出显示空白区域后,您可以关闭搜索对话框并按 CTRL+C 复制到剪贴板。
然后将该字符粘贴到 unicode 搜索工具中。我通常使用在线的。 http://unicode.scarfboy.com/
Example: I had a bullet point (?) in my code somehow. The unicode value is 2022 (hex), but when read as ascii by the compiler you get \342 \200 \242 (3 octal values). It's not as simple as converting each octal values to hex and smashing them together. So "E2 80 A2" is NOT the hex unicode point in your code.
示例:不知何故,我的代码中有一个要点 (?)。unicode 值为 2022(十六进制),但是当被编译器读取为 ascii 时,您会得到 \342 \200 \242(3 个八进制值)。这并不像将每个八进制值转换为十六进制并将它们粉碎在一起那么简单。所以“E2 80 A2”不是代码中的十六进制unicode点。
回答by Victor Mwenda
Invalid character on your code. A common copy paste error specially when code is copied from Word Documents or PDF files.
代码中的无效字符。一种常见的复制粘贴错误,特别是从 Word 文档或 PDF 文件复制代码时。
回答by Saifu Khan
This problem comes when you have copied some text from html or you have done modification in windows environment and trying to compile in Unix/Solaris environment.
当您从 html 中复制了一些文本或您在 windows 环境中进行了修改并尝试在 Unix/Solaris 环境中编译时,就会出现此问题。
Please do "dos2unix" to remove the special characters from the file:
请执行“ dos2unix”以从文件中删除特殊字符:
dos2unix fileName.ext fileName.ext
dos2unix 文件名.ext 文件名.ext
回答by Eeyore
Codo was exactly right on Oct. 5 that ¤t[i] is the intended text (with the currency symbol inadvertently introduced when the source was put into HTML (see original): http://downloads.securityfocus.com/vulnerabilities/exploits/59846-1.c
Codo 在 10 月 5 日完全正确,¤t[i] 是预期的文本(在将源代码放入 HTML 时无意中引入了货币符号(见原文):http: //downloads.securityfocus.com/vulnerabilities/exploits/ 59846-1.c
Codo's change makes this exploit code compile without error. I did that and was able to use the exploit on Ubuntu 12.04 to escalate to root privilege.
Codo 的更改使此漏洞利用代码编译无误。我做到了,并且能够在 Ubuntu 12.04 上使用漏洞来升级到 root 权限。
回答by ravi
The explanations given here are correct. I just wanted to add that this problem might be because you copied the code from somewhere, from a website or a pdf file due to which there are some invalid characters in the code.
这里给出的解释是正确的。我只是想补充一点,这个问题可能是因为您从某个地方复制了代码,从网站或 pdf 文件中复制了代码,因此代码中存在一些无效字符。
Try to find those invalid characters, or just retype the code if you can't. It will definitely compile then.
尝试找到那些无效字符,或者如果找不到就重新输入代码。它肯定会编译。
Source: stray error reason
来源:误报原因

