Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

when I am using below code on Linux the build is failing with the below error .

Could you please let me know what could be wrong with this usage on LInux.

/usr/include/bits/stdio2.h:66:44: error: call to int __builtin___snprintf_chk(char*, unsigned int, int, unsigned int, const char*, ...) will always overflow destination buffer Linking CXX shared library

C++
snprintf(formatStr,sizeof(result),"%%0%ullX", precision);


Appreciate your help.

Thanks,
Sudhakar
Posted
Comments
Richard MacCutchan 3-Mar-14 5:10am    
What is formatStr, what is result, what is precision?
Member 3975629 3-Mar-14 5:13am    
char result[255] = {0};
char formatStr[20] = {0};
unsigned char precision;

precision i am passing as 16

1 solution

I think usage of snprintf is wrong. see here: snprintf[^]
the first parameter is buffer, the second parameter is the size of the first parameter(buffer size).

It should be like
snprintf(result,sizeof(result),"%%0%ullX", precision);


I tried the following example and it is OK.

C#
#include <stdio.h>

int main() {

 int i;
 char buff[100];
 i=snprintf(buff, sizeof(buff), "%d", 10);
 if(i>0)
  buff[i]=0;
 printf("%s %d", buff, i);

 return 0;
}


But I am not sure if your format string is correct ("%%0%ullX")
 
Share this answer
 
Comments
Member 3975629 3-Mar-14 5:36am    
Ok. thanks. btw can't we give extra size. I mean more than size of buffer.
Vedat Ozan Oner 3-Mar-14 6:52am    
yes, because by doing that, you try to get access beyond your allocated memory which may cause (most probably) undesired results.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900