473,387 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

ascii pwned by reckless C

the environment:
*nix, specifically CoreLinux.

the source (just a little diagnostic program):

#include <stdio.h>

int main()
{
char * arr[5] = {"Bite", "my", "shiny", "metal", "ass"};
int i;

for(i=0; i < 5; i++) {
printf("\t&arr[%d] = %x, ", i, &arr[i]);
printf("arr[%d] = %x, %c, %s\n", i, arr[i], arr[i], arr[i]);
}

return 0;
}

the result?
My entire shell having all of its characters transposed in the
ascii set. Except for the capital letters. Capital letters were
unaffected, but everything else turned into one of those strange
glyphs one find only in the higher reaches of the set.
I haven't tried rebooting the distrobution yet, and I suspect the
changes won't last through reset, but there are a couple of things I
want to know:

a) what the fuck man?
b) what was it in my carelessly written little program that triggered
this? (Mind you, I had run simpler permutations of
this very program earlier with no problem... in fact, the
decisive change was when I included the "%c" to the output.)

Thanks, bitches.

Mar 20 '07 #1
7 2570
On Mar 20, 4:36 am, hern...@bluebottle.com wrote:
char * arr[5] = {"Bite", "my", "shiny", "metal", "ass"};
int i;

for(i=0; i < 5; i++) {
printf("\t&arr[%d] = %x, ", i, &arr[i]);
printf("arr[%d] = %x, %c, %s\n", i, arr[i], arr[i], arr[i]);
You're trying to print a string (arr[i]) with a '%c' instead of a '%s'
format
specifier, and (instead of starting wwiii or having nasal daemons) you
end up
printing a '^N' character to the console, which is toggling the
alternate character
set on most vt100 compatible terminals.
Thanks, bitches.
you're welcome.

Mar 20 '07 #2
On Mar 19, 11:01 pm, "Racaille" <0xef967...@gmail.comwrote:
On Mar 20, 4:36 am, hern...@bluebottle.com wrote:
char * arr[5] = {"Bite", "my", "shiny", "metal", "ass"};
int i;
for(i=0; i < 5; i++) {
printf("\t&arr[%d] = %x, ", i, &arr[i]);
printf("arr[%d] = %x, %c, %s\n", i, arr[i], arr[i], arr[i]);

You're trying to print a string (arr[i]) with a '%c' instead of a '%s'
format
specifier, and (instead of starting wwiii or having nasal daemons) you
end up
printing a '^N' character to the console, which is toggling the
alternate character
set on most vt100 compatible terminals.
Thanks, bitches.

you're welcome.
Weird, thanks for the info! Now I'll know how to unscrew myself if I
screw myself thusly again. Which I shouldn't. But I'm tempted to
now.

Shortly after posting this I dove back into linuxland and took a more
careful look at what I was doing. Still, my own reaction at seeing my
entire screen turn into garbage was well worth it.

Mar 20 '07 #3
>the environment:
*nix, specifically CoreLinux.

the source (just a little diagnostic program):

#include <stdio.h>

int main()
{
char * arr[5] = {"Bite", "my", "shiny", "metal", "ass"};
int i;

for(i=0; i < 5; i++) {
printf("\t&arr[%d] = %x, ", i, &arr[i]);
printf("arr[%d] = %x, %c, %s\n", i, arr[i], arr[i], arr[i]);
arr[i] is a *pointer*, and you printed it with a %c format specifier.
You're likely to get assorted wierd characters from that, in particular
the ASCII ESC, SI, and SO characters might cause trouble.
> }

return 0;
}

the result?
My entire shell having all of its characters transposed in the
It's not the shell, it's your console.
>ascii set. Except for the capital letters. Capital letters were
unaffected, but everything else turned into one of those strange
glyphs one find only in the higher reaches of the set.
Your terminal emulator can probably shift character sets on command
by appropriate sequences. If it were a real vt100 terminal, I'd
recommend power-cycling it. In an xterm, try a "soft reset",
followed by a "full reset" if the "soft reset" doesn't work.

If this is a Linux console, I'm not sure how you reset it, although
on BSD the command control-J reset control-J, where control-J
represents a newline, reset represents the characters "are eeh ess
eeh tee" (say it out loud) is often useful after spewing random
binary to the screen to unhose it.
I haven't tried rebooting the distrobution yet, and I suspect the
changes won't last through reset, but there are a couple of things I
want to know:

a) what the fuck man?
If your computer is sexually reproducing, you've got much, much bigger
problems than a messed-up console.
>b) what was it in my carelessly written little program that triggered
this? (Mind you, I had run simpler permutations of
this very program earlier with no problem... in fact, the
decisive change was when I included the "%c" to the output.)
That's what generates raw binary trash.

Guess: shift-in, shift-out, or escape characters.
Mar 20 '07 #4
he*****@bluebottle.com wrote:
the environment:
*nix, specifically CoreLinux.

the source (just a little diagnostic program):

#include <stdio.h>

int main()
{
char * arr[5] = {"Bite", "my", "shiny", "metal", "ass"};
int i;

for(i=0; i < 5; i++) {
printf("\t&arr[%d] = %x, ", i, &arr[i]);
printf("arr[%d] = %x, %c, %s\n", i, arr[i], arr[i], arr[i]);
}

return 0;
}

the result?
My entire shell having all of its characters transposed in the
ascii set. Except for the capital letters. Capital letters were
unaffected, but everything else turned into one of those strange
glyphs one find only in the higher reaches of the set.
I haven't tried rebooting the distrobution yet, and I suspect the
changes won't last through reset, but there are a couple of things I
want to know:
1.) Open up a shell
2.) Find some arbitrary binary file, like an executable or database file
3.) Open up this file with "more" or "less"
4.) Hilarity ensues

I guess Unix made you its bitch, didn't it?
Mar 20 '07 #5
<he*****@bluebottle.comwrote in message
news:11*********************@y80g2000hsf.googlegro ups.com...
....
char * arr[5] = {"Bite", "my", "shiny", "metal", "ass"};
....
printf("arr[%d] = %x, %c, %s\n", i, arr[i], arr[i], arr[i]);
....
b) what was it in my carelessly written little program that triggered
this? (Mind you, I had run simpler permutations of
this very program earlier with no problem... in fact, the
decisive change was when I included the "%c" to the output.)
You're passing arr[i] -- of type char* -- as the fourth argument to printf()
but you used the %c format specifier, which told printf() to expect a char.
A char* is not a char.

Also, you're using %x to print an argument of type char*. That's not valid;
use %p and cast the argument to void* if you want to print the value of a
pointer.

Try this:

printf("arr[%d] = %p, %c, %s\n", i, (void*)arr[i], *arr[i], arr[i]);

S

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov
--
Posted via a free Usenet account from http://www.teranews.com

Mar 20 '07 #6
If you provoke undefined behavior, expect anything to happen.
"If a conversion specification is invalid, the behavior is undefined.
239) If any argument is not the correct type for the corresponding
conversion specification, the behavior is undefined.
footnote 239) See "future library directions" (7.26.9)."

C:\tmp>splint foo.c
Splint 3.1.1 --- 12 Mar 2007

foo.c: (in function main)
foo.c(9,42): Format argument 2 to printf (%x) expects unsigned int
gets char
**: &arr[i]
Type of parameter is not consistent with corresponding code in
format string.
(Use -formattype to inhibit warning)
foo.c(9,33): Corresponding format code
foo.c(10,47): Format argument 2 to printf (%x) expects unsigned int
gets char
*: arr[i]
foo.c(10,30): Corresponding format code
foo.c(10,55): Format argument 3 to printf (%c) expects int gets char
*: arr[i]
foo.c(10,34): Corresponding format code

Finished checking --- 3 code warnings

Mar 20 '07 #7
Groovy hepcat he*****@bluebottle.com was jivin' on 19 Mar 2007
19:36:41 -0700 in comp.lang.c.
ascii pwned by reckless C's a cool scene! Dig it!
>the environment:
*nix, specifically CoreLinux.

the source (just a little diagnostic program):

#include <stdio.h>

int main()
{
char * arr[5] = {"Bite", "my", "shiny", "metal", "ass"};
int i;

for(i=0; i < 5; i++) {
printf("\t&arr[%d] = %x, ", i, &arr[i]);
Undefined behaviour. %x conversion specifier, char ** argument.
> printf("arr[%d] = %x, %c, %s\n", i, arr[i], arr[i], arr[i]);
Undefined behaviour. %x conversion specifier, char * argument. %c
conversion specifier, char * argument.
>Thanks, bitches.
Charming!

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Mar 24 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: chandy | last post by:
Hi, I have an Html document that declares that it uses the utf-8 character set. As this document is editable via a web interface I need to make sure than high-ascii characters that may be...
13
by: bgbauer70 | last post by:
My appologies if this ends up being a duplicate post. For some reason the first post never showed up. I've tried about 300 iterrations of this same ability, and none of them seem to work in...
4
by: wob | last post by:
Many thanks for those who responded to my question of "putting greek char into C string". In searching for an solution, I noticed that there are more than one version of "Extended ASCII...
2
by: Martín Marconcini | last post by:
Hello there, I'm writting (or trying to) a Console Application in C#. I has to be console. I remember back in the old days of Cobol (Unisys), Clipper and even Basic, I used to use a program...
18
by: Ger | last post by:
I have not been able to find a simple, straight forward Unicode to ASCII string conversion function in VB.Net. Is that because such a function does not exists or do I overlook it? I found...
31
by: Claude Yih | last post by:
Hi, everyone. I got a question. How can I identify whether a file is a binary file or an ascii text file? For instance, I wrote a piece of code and saved as "Test.c". I knew it was an ascii text...
24
by: ChaosKCW | last post by:
Hi I am reading from an oracle database using cx_Oracle. I am writing to a SQLite database using apsw. The oracle database is returning utf-8 characters for euopean item names, ie special...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
9
by: =?Utf-8?B?RGFu?= | last post by:
I have the following code section that I thought would strip out all the non-ascii characters from a string after decoding it. Unfortunately the non-ascii characters are still in the string....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.