From b227b8f49209b2f4b9dafdfad06570842af20bb1 Mon Sep 17 00:00:00 2001 From: kamphaus Date: Tue, 8 May 2018 01:40:51 +0200 Subject: [PATCH] Cast array to slice. Fixes #719 (#720) --- tests/cast.c | 15 ++++++++++++++- types/cast.go | 7 +++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/tests/cast.c b/tests/cast.c index 11bb97026..be8518424 100644 --- a/tests/cast.c +++ b/tests/cast.c @@ -93,6 +93,10 @@ void test_preprocessor() } typedef unsigned char pcre_uchar; +typedef unsigned char pcre_uint8; +typedef struct pcre_study_data { + pcre_uint8 start_bits[32]; +} pcre_study_data; void caststr() { pcre_uchar str[] = "abcd"; @@ -131,7 +135,7 @@ void cast_pointer_diff(pcre_uchar *str, int *x) { int main() { - plan(38); + plan(39); START_TEST(cast); START_TEST(castbool); @@ -238,6 +242,15 @@ int main() cast_pointer_diff(&s[0], &b); is_eq(b, 0); } + diag("Cast array to slice"); + { + pcre_study_data sdata; + sdata.start_bits[1] = 42; + const pcre_study_data *study = &sdata; + const pcre_uint8 *p = 0; + p = study->start_bits; + is_eq(p[1], 42); + } done_testing(); } diff --git a/types/cast.go b/types/cast.go index e582b90d4..a1e5aba6e 100644 --- a/types/cast.go +++ b/types/cast.go @@ -292,6 +292,13 @@ func CastExpr(p *program.Program, expr goast.Expr, cFromType, cToType string) ( } if fromType == toType { + match := util.GetRegex(`\[(\d+)\]$`).FindStringSubmatch(cFromType) + if strings.HasSuffix(cToType, "*") && len(match) > 0 { + // we need to convert from array to slice + return &goast.SliceExpr{ + X: expr, + }, nil + } return expr, nil }