problem about snprintf-Collection of common programming errors


  • Tim Cooper

  • antirez
    c casting integer double snprintf
    in Redis (http://code.google.com/p/redis) there are scores associated to elements, in order to take this elements sorted. This scores are doubles, even if many users actually sort by integers (for instance unix times).When the database is saved we need to write this doubles ok disk. This is what is used currently:snprintf((char*)buf+1,sizeof(buf)-1,”%.17g”,val);Additionally infinity and not-a-number conditions are checked in order to also represent this in the final database file.Unfortunat

  • pythonic metaphor
    c segmentation-fault strlen snprintf
    I have a void *, call it data, whose length I know, but is not null terminated. I make a call like this snprintf(line, sizeof(line), “%*s”, n, (const char*)data) where n is the known length. Almost always this works, but occasionally it results in a segfault. Whenever the segfault occurs, the back trace says the problem is inside

  • greTech
    c++ c pointers snprintf
    What is the expected behaviour for this code snippet?char * aNullPointer = 0; snprintf (res, 128, “Testing %s null pointe

  • rolls
    c programming-languages sprintf fprintf snprintf
    Here is my code, basically one the 4 computer I have tested it on they all work perfectly with very large data sizes, eg textfiles up to 500mb in size, but when I run them on the server with real data even files as small as 6mb seem to overrun somewhere and writes garbage to the end of my files.Here is the source of the entire function so people can have a more indepth look/** Reads values from tagname between start_time and end_time which are strings in the format of 01/01/1970-12:00, a null string is treated as 01/01/1970, values are stored in”tagname”.csv */ int ReadValues(char * tagname, char * start_time, char * end_time, char * working_directory, char * new_tag_name) {long lRet;int number_of_samples;int loop;IHU_DATA_SAMPLE * pSamples=NULL;IHU_TIMESTAMP StartTime;IHU_TIMESTAMP EndTime;FILE *stream;char outputFileName[200];char szQuality[100];char newTempTagName[100];int Year;int Month;int Day;int Hour;int Minute;int Second;long Subsecond;int date[10];//if we want to change the tagname do it nowif(new_tag_name != 0){strncpy(newTempTagName, new_tag_name, 100);} else {strncpy(newTempTagName, tagname, 100); } // if the tagname contains a character that is invalid for a filename then we have to make a nameif ( (strstr(newTempTagName, “/”)) || (strstr(newTempTagName, “\\”)) || (strstr(newTempTagName, “:”))){sprintf(outputFileName, “MadeUpTag%d”, MadeUpTagCount++);}else{//If a working directory was passed in use itif(!working_directory){ strncpy(outputFileName, newTempTagName,199); } else {strcpy(outputFileName, working_directory); //Copy the working directory to the filename// Append a / on the end of the working directory if it doesn’t already have oneif(outputFileName[strlen(outputFileName)-1] == ‘\”‘){ outputFileName[strlen(outputFileName)-1] = 0;} if(outputFileName[strlen(outputFileName)-1] != ‘\\’){ strncat(outputFileName, “\\”, 199); }strncat(outputFileName, newTempTagName, 199); //Copy the tagname to the end of the working directory}}//Add the csv file extensionstrcat(outputFileName, “.csv”);#ifdef DEBUGprintf(“Output filename: %s\n”, outputFileName);#endifstream = fopen(outputFileName, “w”);if( stream == NULL ) {printf(“The file %s can not be opened\n”, outputFileName );} else {//If start_time == 0 we want to start at 1970if(start_time == 0){// we want all the data so use an old start timestruct tm local;local.tm_year = date[2] – 1900;local.tm_mon = date[1] – 1;local.tm_mday = date[0];local.tm_hour = date[3];local.tm_min = date[4];local.tm_sec = 0;time_t utc_seconds = mktime(&local);StartTime.Seconds = (long)utc_seconds;StartTime.Subseconds = 0; } else {//we have been supplied a start time#ifdef DEBUGprintf(“Start Time: “);#endiflRet = convert_date(start_time, &StartTime);#ifdef DEBUGprintf(“Seconds %ld \n”, StartTime.Seconds);#endif}//if end_time == 0 we want to go all the way until now.if(end_time == 0){// end time of 0 means nowmemset(&EndTime, 0, sizeof(EndTime));} else {//we have been supplied an end time#ifdef DEBU

Originally posted 2013-11-09 23:23:07.