From c83169f343c9a1b7ebbc98badf0d351ef406730e Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Wed, 8 Oct 2014 14:12:54 -0700 Subject: Only set err_info on an error, and use g_strdup_printf(). If the goal is to look like a libwiretap routine, we only need to set *err_info on an error, and we should set it to a g_mallocated string (or NULL). Handle WTAP_ERR_UNSUPPORTED while we're at it - we never return it, but we never return WTAP_ERR_UNSUPPORTED_ENCAP, either, but we handle it. Change-Id: I9d93c43278d22f0fa77ec1cf7f29b476c8dd0dd0 Reviewed-on: https://code.wireshark.org/review/4565 Reviewed-by: Guy Harris --- rawshark.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'rawshark.c') diff --git a/rawshark.c b/rawshark.c index 901187bed4..1ca43f865f 100644 --- a/rawshark.c +++ b/rawshark.c @@ -903,13 +903,12 @@ main(int argc, char *argv[]) * @return TRUE on success, FALSE on failure. */ static gboolean -raw_pipe_read(struct wtap_pkthdr *phdr, guchar * pd, int *err, const gchar **err_info, gint64 *data_offset) { +raw_pipe_read(struct wtap_pkthdr *phdr, guchar * pd, int *err, gchar **err_info, gint64 *data_offset) { struct pcap_pkthdr mem_hdr; struct pcaprec_hdr disk_hdr; ssize_t bytes_read = 0; size_t bytes_needed = sizeof(disk_hdr); guchar *ptr = (guchar*) &disk_hdr; - static gchar err_str[100]; if (want_pcap_pkthdr) { bytes_needed = sizeof(mem_hdr); @@ -921,9 +920,11 @@ raw_pipe_read(struct wtap_pkthdr *phdr, guchar * pd, int *err, const gchar **err bytes_read = read(fd, ptr, (int)bytes_needed); if (bytes_read == 0) { *err = 0; + *err_info = NULL; return FALSE; } else if (bytes_read < 0) { *err = errno; + *err_info = NULL; return FALSE; } bytes_needed -= bytes_read; @@ -955,9 +956,8 @@ raw_pipe_read(struct wtap_pkthdr *phdr, guchar * pd, int *err, const gchar **err #endif if (bytes_needed > WTAP_MAX_PACKET_SIZE) { *err = WTAP_ERR_BAD_FILE; - g_snprintf(err_str, 100, "Bad packet length: %lu\n", + *err_info = g_strdup_printf("Bad packet length: %lu\n", (unsigned long) bytes_needed); - *err_info = err_str; return FALSE; } @@ -966,12 +966,13 @@ raw_pipe_read(struct wtap_pkthdr *phdr, guchar * pd, int *err, const gchar **err bytes_read = read(fd, ptr, (int)bytes_needed); if (bytes_read == 0) { *err = WTAP_ERR_SHORT_READ; + *err_info = NULL; return FALSE; } else if (bytes_read < 0) { *err = errno; + *err_info = NULL; return FALSE; } - *err_info = NULL; bytes_needed -= bytes_read; *data_offset += bytes_read; ptr += bytes_read; @@ -983,7 +984,7 @@ static gboolean load_cap_file(capture_file *cf) { int err; - const gchar *err_info; + gchar *err_info; gint64 data_offset = 0; guchar pd[WTAP_MAX_PACKET_SIZE]; @@ -1004,9 +1005,16 @@ load_cap_file(capture_file *cf) /* Print a message noting that the read failed somewhere along the line. */ switch (err) { + case WTAP_ERR_UNSUPPORTED: + cmdarg_err("The file \"%s\" contains record data that Rawshark doesn't support.\n(%s)", + cf->filename, err_info); + g_free(err_info); + break; + case WTAP_ERR_UNSUPPORTED_ENCAP: cmdarg_err("The file \"%s\" has a packet with a network type that Rawshark doesn't support.\n(%s)", cf->filename, err_info); + g_free(err_info); break; case WTAP_ERR_SHORT_READ: @@ -1017,11 +1025,13 @@ load_cap_file(capture_file *cf) case WTAP_ERR_BAD_FILE: cmdarg_err("The file \"%s\" appears to be damaged or corrupt.\n(%s)", cf->filename, err_info); + g_free(err_info); break; case WTAP_ERR_DECOMPRESS: cmdarg_err("The compressed file \"%s\" appears to be damaged or corrupt.\n(%s)", cf->filename, err_info); + g_free(err_info); break; default: -- cgit v1.2.3