[c] Compilation error: stray ‘\302’ in program etc

I am having problem compiling the followed exploit code:

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:

 sorbolinux-exec.c: In function ‘sc’:
 sorbolinux-exec.c:76: error: stray ‘\302’ in program
 sorbolinux-exec.c:76: error: stray ‘\244’ 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.

This question is related to c compiler-construction

The answer is


I noticed an issue in using the above tr command. The tr command COMPLETELY removes the "smart quotes". It would be better to replace the "smart quotes" with something like this.

This will give you a quick preview of what will be replaced.

sed s/[”“]/'"'/g File.txt

This will do the replacements and put the replacement in a new file called WithoutSmartQuotes.txt.

sed s/[”“]/'"'/g File.txt > WithoutSmartQuotes.txt

This will overwrite the original file.

sed -i ".bk" s/[”“]/'"'/g File.txt

http://developmentality.wordpress.com/2010/10/11/how-to-remove-smart-quotes-from-a-text-file/


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 :

"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.


Change the name of the file.c to some other file423.c. it may be because of the name conflict between c files.


With me this error ocurred when I copied and pasted a code in text format to my editor (gedit). The code was in a text document (.odt) and I copied it and pasted it into gedit. If you did the same, you have manually rewrite the code.


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:

tr -cd '\11\12\15\40-\176' < 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 :-)


I got the same with a character that visibly appeared as an asterisk, but was a UTF-8 sequence instead.

Encoder * st;

When compiled returned:

g.c:2:1: error: stray ‘\342’ in program
g.c:2:1: error: stray ‘\210’ in program
g.c:2:1: error: stray ‘\227’ in program

342 210 227 turns out to be UTF-8 for ASTERISK OPERATOR.

Deleting the '*' and typing it again fixed the problem.


Codo was exactly right on Oct. 5 that &current[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'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.


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.

Try to find those invalid characters, or just retype the code if you can't. It will definitely compile then.

Source: stray error reason


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

....

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.. :)


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.

Please do "dos2unix" to remove the special characters from the file:

dos2unix fileName.ext fileName.ext


Sure, convert the file to ascii and blast all unicode characters away. It will probably work.... BUT...

  1. You won't know what you fixed.
  2. It will also destroy any unicode comments. Ex: //: A²+B²=C²
  3. 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 ).

Two more sugrical approaches to fixing the problem:

  1. Switch fonts to see the character. (It might be invisible in your current font)
  2. 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/

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.


Invalid character on your code. A common copy paste error specially when code is copied from Word Documents or PDF files.