Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

force gcc compilation / ignore error messages

Tags:

c

hex

I'm trying to compile some code I found online, but gcc keeps getting me error messages.

Is there any way I can bypass the error, and compile?

ttys000$ gcc -o s-proc s-proc.c 
s-proc.c:84:18: error: \x used with no following hex digits

Here's the line it keeps bitching about:

printf("\x%02x", ((unsigned char *)code)[i]);

...

First post on here, so if I broke any rules or wasn't specific enough, let me know.

like image 840
dcat Avatar asked Dec 31 '25 23:12

dcat


1 Answers

You can't ignore errors1. You can only ignore warnings. Change the code.

printf("\\x%02x", ((unsigned char *)code)[i]);

It's just a guess, since without documentation or input from the original author of the code, we have no solid evidence for what the code is actually supposed to do. However, the above correction is extremely plausible, it's a simple typo (the original author forgot a \), and it's conceivable that the author uses a C compiler which silently ignores the error (Python has the same behavior by design).

The line of code above, or something almost exactly like it, is found in probably tens of thousands of source files across the globe. It is used for encoding a binary blob using escape sequences so it can be embedded as a literal in a C program. Similar code appears in JSON, XML, and HTML emitters. I've probably written it a hundred times.

Alternatively, if the code were supposed to print out the character, this would not work:

printf("\x%02x", ((unsigned char *)code)[i]);

This doesn't work because escape sequences (the things that start with \, like \x42) are handled by the C compiler, but format strings (the things that start with %, like %02x) are handled by printf. The above line of code might only work if the order were reversed: if printf ran first, before you compiled the program. So no, it doesn't work.

If the author had intended to write literal characters, the following is more plausible:

printf("%c", ((unsigned char *)code)[i]);  // clumsy
putchar((unsigned char *)code)[i]);        // simpler

So you know either the original author simply typo'd and forgot a single \ (I make that mistake all the time), or the author has no clue.

Notes:

1: An error means that GCC doesn't know what the code is supposed to do, so continuing would be impossible.

like image 94
Dietrich Epp Avatar answered Jan 03 '26 13:01

Dietrich Epp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!