| 837 | } |
| 838 | |
| 839 | DISABLE_WARNING_FORMAT_NONLITERAL |
| 840 | |
| 841 | /* |
| 842 | * Wrap snprintf for use in the main code. |
| 843 | * |
| 844 | * Wrap reasons: |
| 845 | * 1. If there are any platform issues, we have one spot to fix them - |
| 846 | * snprintf is a routine with a troubling history of bad implementations. |
| 847 | * 2. Add cumbersome error checking in one spot. Problems with text |
| 848 | * wrangling do not have to be fatal. |
| 849 | * 3. Gcc 9+ will issue a warning unless the return value is used. |
| 850 | * Annoyingly, explicitly casting to void does not remove the error. |
| 851 | * So, use the result - see reason #2. |
| 852 | */ |
| 853 | void |
| 854 | nh_snprintf( |
| 855 | const char *func UNUSED, int line UNUSED, |
| 856 | char *str, size_t size, |
| 857 | const char *fmt, ...) |
| 858 | { |
| 859 | va_list ap; |
| 860 | int n; |
| 861 | |
| 862 | va_start(ap, fmt); |
| 863 | n = vsnprintf(str, size, fmt, ap); |
| 864 | va_end(ap); |
| 865 | if (n < 0 || (size_t) n >= size) { /* is there a problem? */ |
| 866 | #if 0 |
| 867 | TODO: add set_impossible(), impossible -> func pointer, |
| 868 | test funcpointer before call |
| 869 | impossible("snprintf %s: func %s, file line %d", |
| 870 | (n < 0) ? "format error" : "overflow", |
| 871 | func, line); |
| 872 | #endif |
| 873 | str[size - 1] = '\0'; /* make sure it is nul terminated */ |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | RESTORE_WARNING_FORMAT_NONLITERAL |
| 878 |
nothing calls this directly
no outgoing calls
no test coverage detected