FreeCalypso > hg > freecalypso-tools
view rvinterf/ctracedec/decode.c @ 465:003e48f8ebe1
rvinterf/etmsync/fsnew.c: cast 0 to (char *) for execl sentinel
I generally don't use NULL and use plain 0 instead, based on a "NULL
considered harmful" discussion on the classiccmp mailing list many aeons
ago (I couldn't find it, and I reason that it must have been 2005 or
earlier), but a recent complaint by a packager sent me searching, and I
found this:
https://ewontfix.com/11/
While I don't give a @#$% about "modern" systems and code-nazi tools,
I realized that passing a plain 0 as a pointer sentinel in execl is wrong
because it will break on systems where pointers are longer than the plain
int type. Again, I don't give a @#$% about the abomination of x86_64 and
the like, but if anyone ever manages to port my code to something like a
PDP-11 (16-bit int, 32-bit long and pointers), then passing a plain 0
as a function argument where a pointer is expected most definitely won't
work: if the most natural stack slot and SP alignment unit is 16 bits,
fitting an int, with longs and pointers taking up two such slots, then
the call stack will be totally wrong with a plain 0 passed for a pointer.
Casting the 0 to (char *) ought to be the most kosher solution for the
most retro systems possible.
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Mon, 11 Feb 2019 00:00:19 +0000 |
parents | e7502631a0f9 |
children |
line wrap: on
line source
/* * This module implements the actual decoding of compressed traces. */ #include <sys/types.h> #include <stdio.h> #include <ctype.h> #include <string.h> #include <strings.h> #include <stdlib.h> extern char *str2ind_tab_filename; extern int str2ind_array_size; extern char **str2ind_orig_strings; extern char **str2ind_param_strings; #define MAX_PRINTF_PARAMS 16 static int cindex, cindex_nchars; static u_char param_bytes_array[256]; static int param_bytes_count; static char *format_string, *param_type_string; static int num_printf_params; static u_long printf_params[MAX_PRINTF_PARAMS]; static char output_buf[2048]; decode_hex_digit(c) { if (isdigit(c)) return(c - '0'); else if (isupper(c)) return(c - 'A' + 10); else return(c - 'a' + 10); } static int decode_idx_and_params(line) char *line; { char *cp; u_char *dp; for (cp = line; isdigit(*cp); cp++) ; if (*cp && *cp != ' ') return(-1); cindex = atoi(line); cindex_nchars = cp - line; for (dp = param_bytes_array; *cp; ) { if (*cp++ != ' ') return(-1); if (!isxdigit(cp[0]) || !isxdigit(cp[1])) return(-1); *dp++ = decode_hex_digit(cp[0]) << 4 | decode_hex_digit(cp[1]); cp += 2; } param_bytes_count = dp - param_bytes_array; return(0); } static void decode_parameters(filename_for_errs, lineno_for_errs) char *filename_for_errs; { int pi, type; u_char *bp, *endp; bp = param_bytes_array; endp = bp + param_bytes_count; for (pi = 0; pi < num_printf_params; pi++) { type = param_type_string[pi]; switch (type) { case 'c': if (bp >= endp) { wrong_param_byte_count: fprintf(stderr, "%s line %d: wrong number of parameter bytes for %s entry #%d\n", filename_for_errs, lineno_for_errs, str2ind_tab_filename, cindex); exit(1); } printf_params[pi] = *bp++; continue; case 'i': case 'p': case '*': if (bp > endp - 4) goto wrong_param_byte_count; printf_params[pi] = (u_long) bp[0] | (u_long) bp[1] << 8 | (u_long) bp[2] << 16 | (u_long) bp[3] << 24; bp += 4; continue; case 's': printf_params[pi] = (u_long) bp; for (;;) { if (bp >= endp) { fprintf(stderr, "%s line %d: unterminated string parameter in compressed trace\n", filename_for_errs, lineno_for_errs); exit(1); } if (!*bp++) break; } continue; default: fprintf(stderr, "%s entry #%d: parameter type \'%c\' not supported\n", str2ind_tab_filename, cindex, type); exit(1); } } if (bp != endp) goto wrong_param_byte_count; } process_ctrace_line(line, cindex_offset, filename_for_errs, lineno_for_errs) char *line, *filename_for_errs; { if (decode_idx_and_params(line + cindex_offset) < 0) { fprintf(stderr, "%s line %d: unable to decode compressed trace line\n", filename_for_errs, lineno_for_errs); exit(1); } if (cindex >= str2ind_array_size) { fprintf(stderr, "%s line %d: index %d exceeds the range of %s\n", filename_for_errs, lineno_for_errs, cindex, str2ind_tab_filename); exit(1); } format_string = str2ind_orig_strings[cindex]; param_type_string = str2ind_param_strings[cindex]; num_printf_params = strlen(param_type_string); if (num_printf_params > MAX_PRINTF_PARAMS) { fprintf(stderr, "error: entry #%d in %s has too many parameters\n", cindex, str2ind_tab_filename); exit(1); } decode_parameters(filename_for_errs, lineno_for_errs); ind2str_doprnt(format_string, printf_params, output_buf); printf("%.*s \"%s\"\n", cindex_offset + cindex_nchars, line, output_buf); return(0); }