Skip to content

Commit

Permalink
Do not allow NA as double/complex in scan() when not in na.strings()
Browse files Browse the repository at this point in the history
(PR#17289).


git-svn-id: https://svn.r-project.org/R/trunk@86508 00db46b3-68df-0310-9c12-caf00c1e9a41
  • Loading branch information
kalibera committed May 2, 2024
1 parent cae99e9 commit ac4a7db
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/main/scan.c
@@ -1,7 +1,7 @@
/*
* R : A Computer Language for Statistical Data Analysis
* Copyright (C) 1995, 1996 Robert Gentleman and Ross Ihaka
* Copyright (C) 1998-2023 The R Core Team.
* Copyright (C) 1998-2024 The R Core Team.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -522,7 +522,7 @@ static void extractItem(char *buffer, SEXP ans, R_xlen_t i, LocalData *d)
if (isNAstring(buffer, 0, d))
REAL(ans)[i] = NA_REAL;
else {
REAL(ans)[i] = Strtod(buffer, &endp, TRUE, d);
REAL(ans)[i] = Strtod(buffer, &endp, FALSE, d);
if (!isBlankString(endp))
expected("a real", buffer, d);
}
Expand All @@ -531,7 +531,7 @@ static void extractItem(char *buffer, SEXP ans, R_xlen_t i, LocalData *d)
if (isNAstring(buffer, 0, d))
COMPLEX(ans)[i].r = COMPLEX(ans)[i].i = NA_REAL;
else {
COMPLEX(ans)[i] = strtoc(buffer, &endp, TRUE, d);
COMPLEX(ans)[i] = strtoc(buffer, &endp, FALSE, d);
if (!isBlankString(endp))
expected("a complex", buffer, d);
}
Expand Down
6 changes: 6 additions & 0 deletions tests/reg-tests-1e.R
Expand Up @@ -1380,6 +1380,12 @@ stopifnot(exprs = {
})
## the last lost row.names => dim(.) was 0 x 3 instead of d0's 2 x 3, in R <= 4.4.0

## Scan should not treat "NA" as double/complex when na.strings doesn't
## include it (PR#17289)
(r <- tryCid(scan(text="NA", what=double(), na.strings=character())))
stopifnot(inherits(r, "error"))
(r <- tryCid(scan(text="NA", what=complex(), na.strings=character())))
stopifnot(inherits(r, "error"))


## keep at end
Expand Down

0 comments on commit ac4a7db

Please sign in to comment.