From bf3a283b9eef75741b6eda3a3ec5d3450b68eeb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szabolcs=20Horva=CC=81t?= Date: Tue, 2 Jan 2024 14:06:37 +0000 Subject: [PATCH] fix: add scalar conversion checks in a few critical places in manually written code --- src/rinterface_extra.c | 28 ++++++++++++++++++++++------ tests/testthat/test-make_graph.R | 4 ++-- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/rinterface_extra.c b/src/rinterface_extra.c index f6ac22eccd1..820caa7d9f5 100644 --- a/src/rinterface_extra.c +++ b/src/rinterface_extra.c @@ -3865,7 +3865,8 @@ SEXP R_igraph_add_vertices(SEXP graph, SEXP pnv) { igraph_t g; SEXP result; - nv=(igraph_integer_t) REAL(pnv)[0]; + R_check_int_scalar(pnv); + nv = (igraph_integer_t) REAL(pnv)[0]; R_SEXP_to_igraph_copy(graph, &g); IGRAPH_R_CHECK(igraph_add_vertices(&g, nv, 0)); PROTECT(result=R_igraph_to_SEXP(&g)); @@ -3985,10 +3986,16 @@ SEXP R_igraph_create(SEXP edges, SEXP pn, SEXP pdirected) { igraph_t g; igraph_vector_int_t v; - igraph_integer_t n=(igraph_integer_t) REAL(pn)[0]; - igraph_bool_t directed=LOGICAL(pdirected)[0]; + igraph_integer_t n; + igraph_bool_t directed; SEXP result; + R_check_int_scalar(pn); + n = (igraph_integer_t) REAL(pn)[0]; + + R_check_bool_scalar(pdirected); + directed = LOGICAL(pdirected)[0]; + R_SEXP_to_vector_int_copy(edges, &v); IGRAPH_R_CHECK(igraph_create(&g, &v, n, directed)); PROTECT(result=R_igraph_to_SEXP(&g)); @@ -4346,7 +4353,7 @@ SEXP R_igraph_barabasi_game(SEXP pn, SEXP ppower, SEXP pm, SEXP poutseq, SEXP palgo, SEXP pstart) { igraph_t g; - igraph_integer_t n=(igraph_integer_t) REAL(pn)[0]; + igraph_integer_t n; igraph_real_t power=REAL(ppower)[0]; igraph_integer_t m=Rf_isNull(pm) ? 0 : (igraph_integer_t) REAL(pm)[0]; igraph_vector_int_t outseq, *myoutseq=0; @@ -4357,6 +4364,9 @@ SEXP R_igraph_barabasi_game(SEXP pn, SEXP ppower, SEXP pm, SEXP poutseq, igraph_t start, *ppstart=0; SEXP result; + R_check_int_scalar(pn); + n = (igraph_integer_t) REAL(pn)[0]; + if (!Rf_isNull(poutseq)) { R_SEXP_to_vector_int_copy(poutseq, &outseq); } else { @@ -4913,13 +4923,16 @@ SEXP R_igraph_erdos_renyi_game(SEXP pn, SEXP ptype, SEXP pporm, SEXP pdirected, SEXP ploops) { igraph_t g; - igraph_integer_t n=(igraph_integer_t) REAL(pn)[0]; + igraph_integer_t n; igraph_integer_t type=(igraph_integer_t) REAL(ptype)[0]; igraph_real_t porm=REAL(pporm)[0]; igraph_bool_t directed=LOGICAL(pdirected)[0]; igraph_bool_t loops=LOGICAL(ploops)[0]; SEXP result; + R_check_int_scalar(pn); + n=(igraph_integer_t) REAL(pn)[0]; + igraph_erdos_renyi_game(&g, (igraph_erdos_renyi_t) type, n, porm, directed, loops); PROTECT(result=R_igraph_to_SEXP(&g)); @@ -4932,11 +4945,14 @@ SEXP R_igraph_erdos_renyi_game(SEXP pn, SEXP ptype, SEXP R_igraph_full(SEXP pn, SEXP pdirected, SEXP ploops) { igraph_t g; - igraph_integer_t n=(igraph_integer_t) REAL(pn)[0]; + igraph_integer_t n; igraph_bool_t directed=LOGICAL(pdirected)[0]; igraph_bool_t loops=LOGICAL(ploops)[0]; SEXP result; + R_check_int_scalar(pn); + n=(igraph_integer_t) REAL(pn)[0]; + IGRAPH_R_CHECK(igraph_full(&g, n, directed, loops)); PROTECT(result=R_igraph_to_SEXP(&g)); IGRAPH_I_DESTROY(&g); diff --git a/tests/testthat/test-make_graph.R b/tests/testthat/test-make_graph.R index 6a9823dce06..d64ad2a511e 100644 --- a/tests/testthat/test-make_graph.R +++ b/tests/testthat/test-make_graph.R @@ -9,10 +9,10 @@ test_that("make_graph accepts an empty vector or NULL", { g2 <- make_empty_graph(n = 0) expect_true(identical_graphs(g, g2)) - g <- make_graph(NULL, n = NULL) + g <- make_graph(NULL, n = 0) expect_true(identical_graphs(g, g2)) - g <- make_graph(edges = c(), n = NULL) + g <- make_graph(edges = c(), n = 0) expect_true(identical_graphs(g, g2)) })