Connecting Tech Pros Worldwide Forums | Help | Site Map

ascii pwned by reckless C

herning@bluebottle.com
Guest
 
Posts: n/a
#1: Mar 20 '07
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.


Racaille
Guest
 
Posts: n/a
#2: Mar 20 '07

re: ascii pwned by reckless C


On Mar 20, 4:36 am, hern...@bluebottle.com wrote:
Quote:
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.
Quote:
Thanks, bitches.
you're welcome.

herning@bluebottle.com
Guest
 
Posts: n/a
#3: Mar 20 '07

re: ascii pwned by reckless C


On Mar 19, 11:01 pm, "Racaille" <0xef967...@gmail.comwrote:
Quote:
On Mar 20, 4:36 am, hern...@bluebottle.com wrote:
>
Quote:
char * arr[5] = {"Bite", "my", "shiny", "metal", "ass"};
int i;
>
Quote:
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.
>
Quote:
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.

Gordon Burditt
Guest
 
Posts: n/a
#4: Mar 20 '07

re: ascii pwned by reckless C


>the environment:
Quote:
*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.
Quote:
> }
>
return 0;
>}
>
>the result?
Quote:
My entire shell having all of its characters transposed in the
It's not the shell, it's your console.
Quote:
>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.
Quote:
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.
Quote:
>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.
Clever Monkey
Guest
 
Posts: n/a
#5: Mar 20 '07

re: ascii pwned by reckless C


herning@bluebottle.com wrote:
Quote:
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?
Stephen Sprunk
Guest
 
Posts: n/a
#6: Mar 20 '07

re: ascii pwned by reckless C


<herning@bluebottle.comwrote in message
news:1174358201.517391.78570@y80g2000hsf.googlegro ups.com...
....
Quote:
char * arr[5] = {"Bite", "my", "shiny", "metal", "ass"};
....
Quote:
printf("arr[%d] = %x, %c, %s\n", i, arr[i], arr[i], arr[i]);
....
Quote:
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

user923005
Guest
 
Posts: n/a
#7: Mar 20 '07

re: ascii pwned by reckless C


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

Peter Shaggy Haywood
Guest
 
Posts: n/a
#8: Mar 24 '07

re: ascii pwned by reckless C


Groovy hepcat herning@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!
Quote:
>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.
Quote:
> 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.
Quote:
>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"?
Closed Thread