473,321 Members | 1,667 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,321 software developers and data experts.

Examples of UB with incorrect specifier in printf()

C99 says:

"If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined."

I know what UB is. However, I am interested in a particular example of how can UB look like in such situation. For example, on x86 it may fail:
Expand|Select|Wrap|Line Numbers
  1.     int i = 10;
  2.     printf("%s\n", i);
Another example of UB I can think of is when i would be a trap value for a given type but there is no trap value in x86. What are other examples of UB either on x86 or other achitectures in this case?
Oct 27 '15 #1
3 1477
weaknessforcats
9,208 Expert Mod 8TB
Just have one typecast in your program and you have UB.
Oct 27 '15 #2
wtf? Is this illegal too:

size_t s = 1;
printf("%llu\n", (unsigned long long) s);
Oct 27 '15 #3
weaknessforcats
9,208 Expert Mod 8TB
On my machine:

Expand|Select|Wrap|Line Numbers
  1. size_t s = 1;
  2.     printf("%llu\n",  s);
  3.     printf("%llu\n", (unsigned long long) s);
the execution result is:

Expand|Select|Wrap|Line Numbers
  1. 658839178552344321
  2. 1
  3.  
That is UB. Unless you step in and force s to be an unsigned long long, this printf function croaks. printf ASSUMES %llu is safe to display the argument as an unsigned long long. Apparently, unless you jump in and tell the compiler to use it as an unsigned long long, you get a bad result from printf.

The typecast has broken type safety. The display is a unsigned long long but the program is using another variable of type size_t.

Now I realize that weaknesses in C force you to cast all the time. Check this out:

Expand|Select|Wrap|Line Numbers
  1. const int var = 10;
  2. int* ptr = &(int)var;
  3.     *ptr = 25;
  4.     printf("%d\n %d\n", var, *ptr);
  5.  
On my machine I get
Expand|Select|Wrap|Line Numbers
  1. 25
  2. 25
I was able to change a const int from 10 to 25 by typecasting the address of a const int to be the address of an int. Then by using a pointer to int, the value of the const was changed.

So the program is using a const of 10 for whatever until the typecast when it now starts using 25. You have no idea when you make a function call using this varable whether it is a const 10 or a const 25.

C++ won't even compile this code.
Oct 27 '15 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Piotr B. | last post by:
Hello, I use MingGW g++ 3.2.3 on Windows 2000/AMD Athlon XP. I tried to output a "long double" variable using stdio printf(). I've tried various %formats (%llf, %Lf etc.), but none of them...
3
by: buzzdee | last post by:
hi, i just wanted to print out some unsigned long int values in hexadecimal, printing out one value works, but not byte by byte. anybody has a suggestinon what my problem is? this is my...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
25
by: Joakim Hove | last post by:
Hello, I have code which makses use of variables of type size_t. The code is originally developed on a 32 bit machine, but now it is run on both a 32 bit and a 64 bit machine. In the code...
18
by: Money | last post by:
Here in this thread http://groups.google.co.in/group/comp.lang.c/browse_frm/thread/c16d280238a95c9/e3b0dbf76f3e02e3?q=finding+endianness&rnum=1#e3b0dbf76f3e02e3 Tydr Schnubbis in 3rd reply used...
10
by: lovecreatesbeauty | last post by:
Is parameter type conversion required for the 2nd argument on printf("%p", (void *)&i); ? But one would never call memcpy like: memcpy((void *)pi, (void *)pj, sizeof *pj); /*memcpy((void *)pi,...
4
by: marsarden | last post by:
code below: #include <stdio.h> int main(void) { char *aaa={"a","b","c"}; printf("%S\n",*aaa); /*the uppercase character S*/ getchar();
10
by: abubakarm | last post by:
Hi, How can I write a number 123456 formatted as 123,456 using printf? Regards, ...ab
8
by: asit | last post by:
plz fix the bug... #include <stdio.h> int main() { char ch; char i; printf("Enter an unsigned char : "); scanf("%d",&ch);
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.