By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,376 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

ascii pwned by reckless C

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
>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

P: n/a
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

P: n/a
<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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.