* Use loc_buf for small strings, check for error return from vsnprintf * cleanup arg when bailing out of new * Use malloc/free instead of new/delete in printf * Return actual bytes written in printf * FIX: write before free
This commit is contained in:
		
							parent
							
								
									07613b3158
								
							
						
					
					
						commit
						717ca79ecb
					
				@ -52,19 +52,24 @@ size_t Print::printf(const char *format, ...)
 | 
				
			|||||||
    va_list copy;
 | 
					    va_list copy;
 | 
				
			||||||
    va_start(arg, format);
 | 
					    va_start(arg, format);
 | 
				
			||||||
    va_copy(copy, arg);
 | 
					    va_copy(copy, arg);
 | 
				
			||||||
    size_t len = vsnprintf(NULL, 0, format, copy);
 | 
					    int len = vsnprintf(temp, sizeof(loc_buf), format, copy);
 | 
				
			||||||
    va_end(copy);
 | 
					    va_end(copy);
 | 
				
			||||||
 | 
					    if(len < 0) {
 | 
				
			||||||
 | 
					        va_end(arg);
 | 
				
			||||||
 | 
					        return 0;
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
    if(len >= sizeof(loc_buf)){
 | 
					    if(len >= sizeof(loc_buf)){
 | 
				
			||||||
        temp = new char[len+1];
 | 
					        temp = (char*) malloc(len+1);
 | 
				
			||||||
        if(temp == NULL) {
 | 
					        if(temp == NULL) {
 | 
				
			||||||
 | 
					            va_end(arg);
 | 
				
			||||||
            return 0;
 | 
					            return 0;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        len = vsnprintf(temp, len+1, format, arg);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    len = vsnprintf(temp, len+1, format, arg);
 | 
					 | 
				
			||||||
    write((uint8_t*)temp, len);
 | 
					 | 
				
			||||||
    va_end(arg);
 | 
					    va_end(arg);
 | 
				
			||||||
    if(len >= sizeof(loc_buf)){
 | 
					    len = write((uint8_t*)temp, len);
 | 
				
			||||||
        delete[] temp;
 | 
					    if(temp != loc_buf){
 | 
				
			||||||
 | 
					        free(temp);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    return len;
 | 
					    return len;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user