ascii pwned by reckless C

H

herning

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);
printf("arr[%d] = %x, %c, %s\n", i, arr, arr, arr);
}

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 **** 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.
 
R

Racaille

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

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


You're trying to print a string (arr) 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.
 
H

herning

char * arr[5] = {"Bite", "my", "shiny", "metal", "ass"};
int i;
for(i=0; i < 5; i++) {
printf("\t&arr[%d] = %x, ", i, &arr);
printf("arr[%d] = %x, %c, %s\n", i, arr, arr, arr);


You're trying to print a string (arr) 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.
 
S

Stephen Sprunk

....
char * arr[5] = {"Bite", "my", "shiny", "metal", "ass"}; ....
printf("arr[%d] = %x, %c, %s\n", i, arr, arr, arr); ....
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 -- 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, *arr, arr);

S
 
G

Gordon Burditt

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);
printf("arr[%d] = %x, %c, %s\n", i, arr, arr, arr);


arr 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 **** 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.
 
C

Clever Monkey

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);
printf("arr[%d] = %x, %c, %s\n", i, arr, arr, arr);
}

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?
 
U

user923005

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
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
foo.c(10,30): Corresponding format code
foo.c(10,55): Format argument 3 to printf (%c) expects int gets char
*: arr
foo.c(10,34): Corresponding format code

Finished checking --- 3 code warnings
 
P

Peter Shaggy Haywood

Groovy hepcat (e-mail address removed) 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);


Undefined behaviour. %x conversion specifier, char ** argument.
printf("arr[%d] = %x, %c, %s\n", i, arr, arr, arr);


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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top