From 7380bc81b5e9942f8346b77fc63346d86e8a236f Mon Sep 17 00:00:00 2001 From: Danny Smith Date: Mon, 26 Jul 2021 01:10:09 +1000 Subject: [PATCH] Fix issue with read_dta crashing R (#600, #608) --- NEWS.md | 3 +++ src/DfReader.cpp | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index d0a116be..20d27c43 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # haven (development version) +* Fix issue with `read_dta()` crashing R when StrL variables with missing values +were present (@gorcha, #600, #608). + # haven 2.4.1 * Fix buglet when combining `labelled()` with identical labels. diff --git a/src/DfReader.cpp b/src/DfReader.cpp index 20d54563..31a6ae93 100644 --- a/src/DfReader.cpp +++ b/src/DfReader.cpp @@ -354,6 +354,7 @@ class DfReader { case READSTAT_TYPE_STRING: { cpp11::writable::strings col(output_[var_index]); + const char* str_value = readstat_string_value(value); if (readstat_value_is_tagged_missing(value)) { col[obs_index] = NA_STRING; @@ -361,8 +362,10 @@ class DfReader { col[obs_index] = NA_STRING; } else if (readstat_value_is_system_missing(value)) { col[obs_index] = NA_STRING; + } else if (str_value == NULL) { + col[obs_index] = NA_STRING; } else { - col[obs_index] = cpp11::r_string(readstat_string_value(value)); + col[obs_index] = cpp11::r_string(str_value); } break; }