From b9f890052974ac0e71221a48f32e1af24bf79894 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 12 Mar 2018 20:03:23 +0100 Subject: Fix 'src/cct.c:235,error,resourceLeak,Resource leak: fout' cppcheck warning --- src/cct.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src') diff --git a/src/cct.c b/src/cct.c index 1853a962..96019f2b 100644 --- a/src/cct.c +++ b/src/cct.c @@ -232,6 +232,8 @@ int main(int argc, char **argv) { /* fail if an inverse operation is not available */ if (!proj_pj_info(P).has_inverse) { fprintf (stderr, "Inverse operation not available\n"); + if (stdout != fout) + fclose (fout); return 1; } /* We have no API call for inverting an operation, so we brute force it. */ -- cgit v1.2.3 From 718495c62ed00d6bf2a60be3b67ecf7e2e8da2d2 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 12 Mar 2018 20:19:59 +0100 Subject: Fix 'bufferAccessOutOfBounds,Buffer is accessed out of bounds' cppcheck warnings --- src/proj_4D_api.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/proj_4D_api.c b/src/proj_4D_api.c index f96dd67d..3ad124e5 100644 --- a/src/proj_4D_api.c +++ b/src/proj_4D_api.c @@ -632,14 +632,18 @@ PJ *proj_create_crs_to_crs (PJ_CONTEXT *ctx, const char *srid_from, const char ******************************************************************************/ PJ *P; char buffer[512]; + size_t len; /* area not in use yet, suppressing warning */ (void)area; strcpy(buffer, "+proj=pipeline +step +init="); - strncat(buffer, srid_from, 512-strlen(buffer)); - strncat(buffer, " +inv +step +init=", 512-strlen(buffer)); - strncat(buffer, srid_to, 512-strlen(buffer)); + len = strlen(buffer); + strncat(buffer + len, srid_from, sizeof(buffer)-1-len); + len += strlen(buffer + len); + strncat(buffer + len, " +inv +step +init=", sizeof(buffer)-1-len); + len += strlen(buffer + len); + strncat(buffer + len, srid_to, sizeof(buffer)-1-len); P = proj_create(ctx, buffer); @@ -1004,7 +1008,7 @@ PJ_INIT_INFO proj_init_info(const char *initname){ key[64] = 0; strncat(key, ":metadata", 9); strcpy(param, "+init="); - strncat(param, key, 73); + strncat(param + strlen(param), key, sizeof(param)-1-strlen(param)); start = pj_mkparam(param); pj_expand_init(ctx, start); -- cgit v1.2.3 From 3563f1cea732b2c191311d4e57eec87ab228746d Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 12 Mar 2018 20:22:51 +0100 Subject: Fix 'src/proj.c:224,style,multiCondition,Expression is always false because 'else if' condition matches previous condition at line 221' cppcheck warning --- src/proj.c | 3 --- 1 file changed, 3 deletions(-) (limited to 'src') diff --git a/src/proj.c b/src/proj.c index 1c86854e..9892e9b3 100644 --- a/src/proj.c +++ b/src/proj.c @@ -221,9 +221,6 @@ static void vprocess(FILE *fid) { if (*s == 'I' || *s == 'i') { linvers = 1; ++s; - } else if (*s == 'I' || *s == 'i') { - linvers = 0; - ++s; } else linvers = inverse; -- cgit v1.2.3 From 8ee42db63cb1e21d7c7531b9224d469bab8ce4d6 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 12 Mar 2018 20:24:40 +0100 Subject: geodesic.c: fix multiple occurences of 'Clarify calculation precedence for '&' and '?'' cppcheck warnings --- src/geodesic.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/geodesic.c b/src/geodesic.c index 23557b5c..d62fdd99 100644 --- a/src/geodesic.c +++ b/src/geodesic.c @@ -585,7 +585,7 @@ real geod_genposition(const struct geod_geodesicline* l, salp2 = l->salp0; calp2 = l->calp0 * csig2; /* No need to normalize */ if (outmask & GEOD_DISTANCE) - s12 = flags & GEOD_ARCMODE ? + s12 = (flags & GEOD_ARCMODE) ? l->b * ((1 + l->A1m1) * sig12 + AB1) : s12_a12; @@ -594,7 +594,7 @@ real geod_genposition(const struct geod_geodesicline* l, /* tan(omg2) = sin(alp0) * tan(sig2) */ somg2 = l->salp0 * ssig2; comg2 = csig2; /* No need to normalize */ /* omg12 = omg2 - omg1 */ - omg12 = flags & GEOD_LONG_UNROLL + omg12 = (flags & GEOD_LONG_UNROLL) ? E * (sig12 - (atan2( ssig2, csig2) - atan2( l->ssig1, l->csig1)) + (atan2(E * somg2, comg2) - atan2(E * l->somg1, l->comg1))) @@ -604,7 +604,7 @@ real geod_genposition(const struct geod_geodesicline* l, ( sig12 + (SinCosSeries(TRUE, ssig2, csig2, l->C3a, nC3-1) - l->B31)); lon12 = lam12 / degree; - lon2 = flags & GEOD_LONG_UNROLL ? l->lon1 + lon12 : + lon2 = (flags & GEOD_LONG_UNROLL) ? l->lon1 + lon12 : AngNormalize(AngNormalize(l->lon1) + AngNormalize(lon12)); } @@ -674,7 +674,7 @@ real geod_genposition(const struct geod_geodesicline* l, if (outmask & GEOD_AREA) *pS12 = S12; - return flags & GEOD_ARCMODE ? s12_a12 : sig12 / degree; + return (flags & GEOD_ARCMODE) ? s12_a12 : sig12 / degree; } void geod_setdistance(struct geod_geodesicline* l, real s13) { @@ -689,7 +689,7 @@ static void geod_setarc(struct geod_geodesicline* l, real a13) { void geod_gensetdistance(struct geod_geodesicline* l, unsigned flags, real s13_a13) { - flags & GEOD_ARCMODE ? + (flags & GEOD_ARCMODE) ? geod_setarc(l, s13_a13) : geod_setdistance(l, s13_a13); } @@ -718,7 +718,7 @@ real geod_gendirect(const struct geod_geodesic* g, geod_lineinit(&l, g, lat1, lon1, azi1, /* Automatically supply GEOD_DISTANCE_IN if necessary */ outmask | - (flags & GEOD_ARCMODE ? GEOD_NONE : GEOD_DISTANCE_IN)); + ((flags & GEOD_ARCMODE) ? GEOD_NONE : GEOD_DISTANCE_IN)); return geod_genposition(&l, flags, s12_a12, plat2, plon2, pazi2, ps12, pm12, pM12, pM21, pS12); } @@ -845,8 +845,8 @@ static real geod_geninverse_int(const struct geod_geodesic* g, csig1 * csig2 + ssig1 * ssig2); Lengths(g, g->n, sig12, ssig1, csig1, dn1, ssig2, csig2, dn2, cbet1, cbet2, &s12x, &m12x, 0, - outmask & GEOD_GEODESICSCALE ? &M12 : 0, - outmask & GEOD_GEODESICSCALE ? &M21 : 0, + (outmask & GEOD_GEODESICSCALE) ? &M12 : 0, + (outmask & GEOD_GEODESICSCALE) ? &M21 : 0, Ca); /* Add the check for sig12 since zero length geodesics might yield m12 < * 0. Test case was @@ -969,8 +969,8 @@ static real geod_geninverse_int(const struct geod_geodesic* g, } Lengths(g, eps, sig12, ssig1, csig1, dn1, ssig2, csig2, dn2, cbet1, cbet2, &s12x, &m12x, 0, - outmask & GEOD_GEODESICSCALE ? &M12 : 0, - outmask & GEOD_GEODESICSCALE ? &M21 : 0, Ca); + (outmask & GEOD_GEODESICSCALE) ? &M12 : 0, + (outmask & GEOD_GEODESICSCALE) ? &M21 : 0, Ca); m12x *= g->b; s12x *= g->b; a12 = sig12 / degree; @@ -1126,7 +1126,7 @@ real SinCosSeries(boolx sinp, real sinx, real cosx, const real c[], int n) { real ar, y0, y1; c += (n + sinp); /* Point to one beyond last element */ ar = 2 * (cosx - sinx) * (cosx + sinx); /* 2 * cos(2 * x) */ - y0 = n & 1 ? *--c : 0; y1 = 0; /* accumulators for sum */ + y0 = (n & 1) ? *--c : 0; y1 = 0; /* accumulators for sum */ /* Now n is even */ n /= 2; while (n--) { -- cgit v1.2.3 From cca88007d3425cfc9d0eabb0b12956e899193727 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 12 Mar 2018 20:25:56 +0100 Subject: PJ_pipeline: remove unused sructure member (spotted by cppcheck) --- src/PJ_pipeline.c | 1 - 1 file changed, 1 deletion(-) (limited to 'src') diff --git a/src/PJ_pipeline.c b/src/PJ_pipeline.c index 3890f552..3f61ad88 100644 --- a/src/PJ_pipeline.c +++ b/src/PJ_pipeline.c @@ -106,7 +106,6 @@ PROJ_HEAD(pipeline, "Transformation pipeline manager"); /* Projection specific elements for the PJ object */ struct pj_opaque { - int reversible; int steps; char **argv; char **current_argv; -- cgit v1.2.3 From 45433be5745644f22e2fc5f6d64b029d9e5aa772 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 12 Mar 2018 20:57:50 +0100 Subject: Avoid cppcheck false positive 'Variable 'nadgrids' is assigned a value that is never used' and make code more readable --- src/pj_datum_set.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/pj_datum_set.c b/src/pj_datum_set.c index f9027bb0..014941e5 100644 --- a/src/pj_datum_set.c +++ b/src/pj_datum_set.c @@ -86,7 +86,8 @@ int pj_datum_set(projCtx ctx, paralist *pl, PJ *projdef) /* -------------------------------------------------------------------- */ /* Check for nadgrids parameter. */ /* -------------------------------------------------------------------- */ - if( (nadgrids = pj_param(ctx, pl,"snadgrids").s) != NULL ) + nadgrids = pj_param(ctx, pl,"snadgrids").s; + if( nadgrids != NULL ) { /* We don't actually save the value separately. It will continue to exist int he param list for use in pj_apply_gridshift.c */ -- cgit v1.2.3 From bc6d9139c6a0ceac454c53804fdc6680f77ba339 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 12 Mar 2018 21:01:13 +0100 Subject: Fix 'src/pj_internal.c:346,style,unreadVariable,Variable 'n' is assigned a value that is never used' cppcheck warning --- src/pj_internal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/pj_internal.c b/src/pj_internal.c index 40f06bd6..1204d27a 100644 --- a/src/pj_internal.c +++ b/src/pj_internal.c @@ -343,7 +343,7 @@ array. if (0==argc) return p; - for (i = n = 0; i < argc; i++) { + for (i = 0; i < argc; i++) { strcat (p, argv[i]); strcat (p, " "); } -- cgit v1.2.3 From 571036eab124920cdffcb73c37965e0a3a065938 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 12 Mar 2018 21:02:35 +0100 Subject: Fix 'src/nad2bin.c:80,style,arrayIndexThenCheck,Array index 'i' is used before limits check' false positive cppcheck warning --- src/nad2bin.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/nad2bin.c b/src/nad2bin.c index 03a55326..afb7d818 100644 --- a/src/nad2bin.c +++ b/src/nad2bin.c @@ -77,7 +77,7 @@ int main(int argc, char **argv) { /* ==================================================================== */ for( i = 1; i < argc; i++ ) { - if( strcmp(argv[i],"-f") == 0 && i < argc-1 ) + if( i < argc-1 && strcmp(argv[i],"-f") == 0 ) { format = argv[++i]; } -- cgit v1.2.3 From e11b5da023febe3425e92cdfbf32ef586469fbb5 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 12 Mar 2018 21:04:16 +0100 Subject: Fix 'Either the condition 'fromProj!=0' is redundant or there is possible null pointer dereference: fromProj' cppcheck warning --- src/cs2cs.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/cs2cs.c b/src/cs2cs.c index 730d37ee..b7e0ab7f 100644 --- a/src/cs2cs.c +++ b/src/cs2cs.c @@ -446,10 +446,8 @@ int main(int argc, char **argv) emess_dat.File_name = 0; } - if( fromProj != NULL ) - pj_free( fromProj ); - if( toProj != NULL ) - pj_free( toProj ); + pj_free( fromProj ); + pj_free( toProj ); pj_deallocate_grids(); -- cgit v1.2.3 From 381f1f1a2c8433a9865d22cf75afcdb19161d033 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 12 Mar 2018 21:06:00 +0100 Subject: Fix 'Consecutive return, break, continue, goto or throw statements are unnecessary' cppcheck warnings --- src/cs2cs.c | 1 + src/proj.c | 1 + 2 files changed, 2 insertions(+) (limited to 'src') diff --git a/src/cs2cs.c b/src/cs2cs.c index b7e0ab7f..de3259d2 100644 --- a/src/cs2cs.c +++ b/src/cs2cs.c @@ -263,6 +263,7 @@ int main(int argc, char **argv) } else emess(1,"invalid list option: l%c",arg[1]); exit(0); + /* cppcheck-suppress duplicateBreak */ continue; /* artificial */ case 'e': /* error line alternative */ if (--argc <= 0) diff --git a/src/proj.c b/src/proj.c index 9892e9b3..c3b604c9 100644 --- a/src/proj.c +++ b/src/proj.c @@ -410,6 +410,7 @@ int main(int argc, char **argv) { } else emess(1,"invalid list option: l%c",arg[1]); exit(0); + /* cppcheck-suppress duplicateBreak */ continue; /* artificial */ case 'e': /* error line alternative */ if (--argc <= 0) -- cgit v1.2.3 From 25bf200c1982b639a756eff9af4d40e2ac00faa3 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 12 Mar 2018 21:08:20 +0100 Subject: PJ_isea.c: Remove unused structure --- src/PJ_isea.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src') diff --git a/src/PJ_isea.c b/src/PJ_isea.c index f08a83fa..748ca9fb 100644 --- a/src/PJ_isea.c +++ b/src/PJ_isea.c @@ -139,11 +139,13 @@ struct isea_geo { double lon, lat; }; +#ifdef unused struct isea_address { int type; /* enum isea_address_form */ int number; double x,y; /* or i,j or lon,lat depending on type */ }; +#endif /* ENDINC */ -- cgit v1.2.3 From 40cd3f31cb0225673d845a3710dbfdba797246d9 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Wed, 14 Mar 2018 18:51:22 +0100 Subject: fixup --- src/PJ_isea.c | 8 -------- 1 file changed, 8 deletions(-) (limited to 'src') diff --git a/src/PJ_isea.c b/src/PJ_isea.c index 748ca9fb..d922ecbb 100644 --- a/src/PJ_isea.c +++ b/src/PJ_isea.c @@ -139,14 +139,6 @@ struct isea_geo { double lon, lat; }; -#ifdef unused -struct isea_address { - int type; /* enum isea_address_form */ - int number; - double x,y; /* or i,j or lon,lat depending on type */ -}; -#endif - /* ENDINC */ enum snyder_polyhedron { -- cgit v1.2.3 From 9edd47f207576a468d6c29cb11db70054815de19 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 12 Mar 2018 21:17:14 +0100 Subject: Add comment to justify 718495c62ed00d6bf2a60be3b67ecf7e2e8da2d2 --- src/proj_4D_api.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/proj_4D_api.c b/src/proj_4D_api.c index 3ad124e5..58b3d835 100644 --- a/src/proj_4D_api.c +++ b/src/proj_4D_api.c @@ -1008,6 +1008,7 @@ PJ_INIT_INFO proj_init_info(const char *initname){ key[64] = 0; strncat(key, ":metadata", 9); strcpy(param, "+init="); + /* The +strlen(param) avoids a cppcheck false positive warning */ strncat(param + strlen(param), key, sizeof(param)-1-strlen(param)); start = pj_mkparam(param); -- cgit v1.2.3 From 7ad9654a05451cb77e508cee00e45a695512a57c Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 12 Mar 2018 21:21:40 +0100 Subject: Add a few cppcheck suppressions --- src/PJ_isea.c | 4 +++- src/geodesic.c | 1 + src/geodtest.c | 2 ++ 3 files changed, 6 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/PJ_isea.c b/src/PJ_isea.c index d922ecbb..57412a38 100644 --- a/src/PJ_isea.c +++ b/src/PJ_isea.c @@ -149,7 +149,9 @@ enum snyder_polyhedron { }; struct snyder_constants { - double g, G, theta, ea_w, ea_a, ea_b, g_w, g_a, g_b; + double g, G, theta; + /* cppcheck-suppress unusedStructMember */ + double ea_w, ea_a, ea_b, g_w, g_a, g_b; }; /* TODO put these in radians to avoid a later conversion */ diff --git a/src/geodesic.c b/src/geodesic.c index d62fdd99..a7e4376a 100644 --- a/src/geodesic.c +++ b/src/geodesic.c @@ -94,6 +94,7 @@ static void Init() { #else { real minus1 = -1; + /* cppcheck-suppress wrongmathcall */ NaN = sqrt(minus1); } #endif diff --git a/src/geodtest.c b/src/geodtest.c index de729407..8cc48134 100644 --- a/src/geodtest.c +++ b/src/geodtest.c @@ -341,6 +341,7 @@ static int GeodSolve14() { int result = 0; { double minus1 = -1; + /* cppcheck-suppress wrongmathcall */ nan = sqrt(minus1); } geod_init(&g, wgs84_a, wgs84_f); @@ -482,6 +483,7 @@ static int GeodSolve55() { int result = 0; { double minus1 = -1; + /* cppcheck-suppress wrongmathcall */ nan = sqrt(minus1); } geod_init(&g, wgs84_a, wgs84_f); -- cgit v1.2.3 From 3ae126e05c9794a2883ee1613c215652f949a3f4 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 12 Mar 2018 21:23:26 +0100 Subject: Fix 'Variable 'XXXX' is reassigned a value before the old one has been used' warnings --- src/PJ_imw_p.c | 4 +--- src/PJ_ob_tran.c | 8 ++++---- 2 files changed, 5 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/PJ_imw_p.c b/src/PJ_imw_p.c index b731f656..c7939d3e 100644 --- a/src/PJ_imw_p.c +++ b/src/PJ_imw_p.c @@ -91,10 +91,8 @@ static XY loc_for(LP lp, PJ *P, double *yc) { static XY e_forward (LP lp, PJ *P) { /* Ellipsoidal, forward */ - XY xy = {0.0,0.0}; double yc; - - xy = loc_for(lp, P, &yc); + XY xy = loc_for(lp, P, &yc); return (xy); } diff --git a/src/PJ_ob_tran.c b/src/PJ_ob_tran.c index e6971138..8b13eb65 100644 --- a/src/PJ_ob_tran.c +++ b/src/PJ_ob_tran.c @@ -48,11 +48,11 @@ static XY t_forward(LP lp, PJ *P) { /* spheroid */ static LP o_inverse(XY xy, PJ *P) { /* spheroid */ - LP lp = {0.0,0.0}; + struct pj_opaque *Q = P->opaque; double coslam, sinphi, cosphi; - lp = Q->link->inv(xy, Q->link); + LP lp = Q->link->inv(xy, Q->link); if (lp.lam != HUGE_VAL) { coslam = cos(lp.lam -= Q->lamp); sinphi = sin(lp.phi); @@ -66,11 +66,11 @@ static LP o_inverse(XY xy, PJ *P) { /* spheroid */ static LP t_inverse(XY xy, PJ *P) { /* spheroid */ - LP lp = {0.0,0.0}; + struct pj_opaque *Q = P->opaque; double cosphi, t; - lp = Q->link->inv(xy, Q->link); + LP lp = Q->link->inv(xy, Q->link); if (lp.lam != HUGE_VAL) { cosphi = cos(lp.phi); t = lp.lam - Q->lamp; -- cgit v1.2.3 From 0682897c358c41f21871ef87001d4825b3076784 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 12 Mar 2018 22:15:37 +0100 Subject: cct.c: fix memory leak (spotted by clang static analyzer) --- src/cct.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/cct.c b/src/cct.c index 96019f2b..57e53f97 100644 --- a/src/cct.c +++ b/src/cct.c @@ -308,6 +308,7 @@ int main(int argc, char **argv) { if (stdout != fout) fclose (fout); free (o); + free (buf); return 0; } -- cgit v1.2.3 From 0223e2517eef43e0e6955408a1aef89d2d981983 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 12 Mar 2018 22:26:15 +0100 Subject: geodesic.c: initialize various variables to please CLang Static Analyzer --- src/geodesic.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/geodesic.c b/src/geodesic.c index a7e4376a..5121ea98 100644 --- a/src/geodesic.c +++ b/src/geodesic.c @@ -1894,7 +1894,7 @@ void geod_polygon_addedge(const struct geod_geodesic* g, struct geod_polygon* p, real azi, real s) { if (p->num) { /* Do nothing is num is zero */ - real lat, lon, S12 = 0; /* Initialize S12 to stop Visual Studio warning */ + real lat = 0, lon = 0, S12 = 0; /* Initialize S12 to stop Visual Studio warning */ geod_gendirect(g, p->lat, p->lon, azi, GEOD_LONG_UNROLL, s, &lat, &lon, 0, 0, 0, 0, 0, p->polyline ? 0 : &S12); @@ -2031,7 +2031,7 @@ unsigned geod_polygon_testedge(const struct geod_geodesic* g, tempsum = p->A[0]; crossings = p->crossings; { - real lat, lon, s12, S12; + real lat = 0, lon = 0, s12, S12 = 0; geod_gendirect(g, p->lat, p->lon, azi, GEOD_LONG_UNROLL, s, &lat, &lon, 0, 0, 0, 0, 0, &S12); -- cgit v1.2.3 From 4ba4bf25400271423c4cc94f97d814d8489ae46e Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 12 Mar 2018 22:35:29 +0100 Subject: Fix dead assignment spotted by clang static analyzer --- src/pj_datum_set.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src') diff --git a/src/pj_datum_set.c b/src/pj_datum_set.c index 014941e5..5955ed5e 100644 --- a/src/pj_datum_set.c +++ b/src/pj_datum_set.c @@ -81,6 +81,8 @@ int pj_datum_set(projCtx ctx, paralist *pl, PJ *projdef) if( pj_datums[i].defn && strlen(pj_datums[i].defn) > 0 ) curr = curr->next = pj_mkparam(pj_datums[i].defn); + + (void)curr; /* make clang static analyzer happy */ } /* -------------------------------------------------------------------- */ -- cgit v1.2.3 From 98258282af54a8ec315aaa216d48f890dd09bbdf Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 12 Mar 2018 22:40:25 +0100 Subject: Remove useless code (the variable is updated, but no longer used in the loop, and reinitialized at the next iteration) (spotted by clang static analyzer) The bug dates back from the original commit of src/PJ_lsat.c src/PJ_misrsom.c has likely copied & pasted from PJ_lsat.c --- src/PJ_lsat.c | 2 -- src/PJ_misrsom.c | 2 -- 2 files changed, 4 deletions(-) (limited to 'src') diff --git a/src/PJ_lsat.c b/src/PJ_lsat.c index 4b77f3d6..7e1567e1 100644 --- a/src/PJ_lsat.c +++ b/src/PJ_lsat.c @@ -61,8 +61,6 @@ static XY e_forward (LP lp, PJ *P) { /* Ellipsoidal, forward */ sav = lampp; lamtp = lp.lam + Q->p22 * lampp; cl = cos(lamtp); - if (fabs(cl) < TOL) - lamtp -= TOL; if( cl < 0 ) fac = lampp + sin(lampp) * M_HALFPI; else diff --git a/src/PJ_misrsom.c b/src/PJ_misrsom.c index c25ca597..b9b97117 100644 --- a/src/PJ_misrsom.c +++ b/src/PJ_misrsom.c @@ -78,8 +78,6 @@ static XY e_forward (LP lp, PJ *P) { /* Ellipsoidal, forward */ sav = lampp; lamtp = lp.lam + Q->p22 * lampp; cl = cos(lamtp); - if (fabs(cl) < TOL) - lamtp -= TOL; if( cl < 0 ) fac = lampp + sin(lampp) * M_HALFPI; else -- cgit v1.2.3 From cde489ce64556e0ad5fa5da64bcb8fd88aee4c39 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 12 Mar 2018 22:42:38 +0100 Subject: Fix various dead initialization warnings spotted by clang static analyzer --- src/PJ_chamb.c | 2 +- src/PJ_goode.c | 4 ++-- src/PJ_igh.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/PJ_chamb.c b/src/PJ_chamb.c index ca9c5edc..571bdf10 100644 --- a/src/PJ_chamb.c +++ b/src/PJ_chamb.c @@ -50,7 +50,7 @@ static double lc(projCtx ctx, double b,double c,double a) { static XY s_forward (LP lp, PJ *P) { /* Spheroidal, forward */ - XY xy = {0.0,0.0}; + XY xy; struct pj_opaque *Q = P->opaque; double sinphi, cosphi, a; VECT v[3]; diff --git a/src/PJ_goode.c b/src/PJ_goode.c index 1554fd41..65761c4b 100644 --- a/src/PJ_goode.c +++ b/src/PJ_goode.c @@ -17,7 +17,7 @@ struct pj_opaque { static XY s_forward (LP lp, PJ *P) { /* Spheroidal, forward */ - XY xy = {0.0,0.0}; + XY xy; struct pj_opaque *Q = P->opaque; if (fabs(lp.phi) <= PHI_LIM) @@ -31,7 +31,7 @@ static XY s_forward (LP lp, PJ *P) { /* Spheroidal, forward */ static LP s_inverse (XY xy, PJ *P) { /* Spheroidal, inverse */ - LP lp = {0.0,0.0}; + LP lp; struct pj_opaque *Q = P->opaque; if (fabs(xy.y) <= PHI_LIM) diff --git a/src/PJ_igh.c b/src/PJ_igh.c index d1d684e7..bb97a8b5 100644 --- a/src/PJ_igh.c +++ b/src/PJ_igh.c @@ -36,7 +36,7 @@ struct pj_opaque { static XY s_forward (LP lp, PJ *P) { /* Spheroidal, forward */ - XY xy = {0.0,0.0}; + XY xy; struct pj_opaque *Q = P->opaque; int z; -- cgit v1.2.3 From a49738902a27624d835e5a6b9983a9803c322996 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 12 Mar 2018 23:32:03 +0100 Subject: Fix (mostly false positive) clang static analyzer warnings about potential null pointer dereference --- src/PJ_pipeline.c | 9 +++++++-- src/geod_set.c | 2 +- src/geodesic.c | 12 ++++++------ src/pj_datum_set.c | 5 ++++- src/pj_gridcatalog.c | 2 +- src/pj_init.c | 3 +++ src/pj_param.c | 9 ++++++--- 7 files changed, 28 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/PJ_pipeline.c b/src/PJ_pipeline.c index 3f61ad88..2791a4e8 100644 --- a/src/PJ_pipeline.c +++ b/src/PJ_pipeline.c @@ -286,7 +286,9 @@ static void set_ellipsoid(PJ *P) { /* Break the linked list after the global args */ attachment = 0; for (cur = P->params; cur != 0; cur = cur->next) - if (strcmp("step", cur->next->param) == 0) { + /* cur->next will always be non 0 given argv_sentinel presence, */ + /* but this is far from being obvious for a static analyzer */ + if (cur->next != 0 && strcmp(argv_sentinel, cur->next->param) == 0) { attachment = cur->next; cur->next = 0; break; @@ -310,7 +312,10 @@ static void set_ellipsoid(PJ *P) { geod_init(P->geod, P->a, (1 - sqrt (1 - P->es))); /* Re-attach the dangling list */ - cur->next = attachment; + /* Note: cur will always be non 0 given argv_sentinel presence, */ + /* but this is far from being obvious for a static analyzer */ + if( cur != 0 ) + cur->next = attachment; proj_errno_restore (P, err); } diff --git a/src/geod_set.c b/src/geod_set.c index 26a86b61..571a7d20 100644 --- a/src/geod_set.c +++ b/src/geod_set.c @@ -19,7 +19,7 @@ geod_set(int argc, char **argv) { start = curr = pj_mkparam(argv[0]); if (!curr) emess(1, "memory allocation failed"); - for (i = 1; i < argc; ++i) { + for (i = 1; curr != 0 && i < argc; ++i) { curr->next = pj_mkparam(argv[i]); if (!curr->next) emess(1, "memory allocation failed"); diff --git a/src/geodesic.c b/src/geodesic.c index 5121ea98..40dffbc1 100644 --- a/src/geodesic.c +++ b/src/geodesic.c @@ -658,21 +658,21 @@ real geod_genposition(const struct geod_geodesicline* l, S12 = l->c2 * atan2(salp12, calp12) + l->A4 * (B42 - l->B41); } - if (outmask & GEOD_LATITUDE) + if ((outmask & GEOD_LATITUDE) && plat2) *plat2 = lat2; - if (outmask & GEOD_LONGITUDE) + if ((outmask & GEOD_LONGITUDE) && plon2) *plon2 = lon2; - if (outmask & GEOD_AZIMUTH) + if ((outmask & GEOD_AZIMUTH) && pazi2) *pazi2 = azi2; - if (outmask & GEOD_DISTANCE) + if ((outmask & GEOD_DISTANCE) && ps12) *ps12 = s12; - if (outmask & GEOD_REDUCEDLENGTH) + if ((outmask & GEOD_REDUCEDLENGTH) && pm12) *pm12 = m12; if (outmask & GEOD_GEODESICSCALE) { if (pM12) *pM12 = M12; if (pM21) *pM21 = M21; } - if (outmask & GEOD_AREA) + if ((outmask & GEOD_AREA) && pS12) *pS12 = S12; return (flags & GEOD_ARCMODE) ? s12_a12 : sig12 / degree; diff --git a/src/pj_datum_set.c b/src/pj_datum_set.c index 5955ed5e..374beda2 100644 --- a/src/pj_datum_set.c +++ b/src/pj_datum_set.c @@ -61,7 +61,10 @@ int pj_datum_set(projCtx ctx, paralist *pl, PJ *projdef) /* find the end of the list, so we can add to it */ for (curr = pl; curr && curr->next ; curr = curr->next) {} - + + /* cannot happen in practice, but makes static analyzers happy */ + if( !curr ) return -1; + /* find the datum definition */ for (i = 0; (s = pj_datums[i].id) && strcmp(name, s) ; ++i) {} diff --git a/src/pj_gridcatalog.c b/src/pj_gridcatalog.c index c0194146..8777185b 100644 --- a/src/pj_gridcatalog.c +++ b/src/pj_gridcatalog.c @@ -260,7 +260,7 @@ PJ_GRIDINFO *pj_gc_findgrid( projCtx ctx, PJ_GridCatalog *catalog, int after, break; } - if( iEntry == catalog->entry_count ) + if( entry == NULL ) { if( grid_date ) *grid_date = 0.0; diff --git a/src/pj_init.c b/src/pj_init.c index 16408ebb..2d549317 100644 --- a/src/pj_init.c +++ b/src/pj_init.c @@ -67,6 +67,9 @@ static paralist *string_to_paralist (PJ_CONTEXT *ctx, char *definition) { c++; } + if( next == 0 ) + return 0; + /* Terminate list and return */ next->next = 0; return first; diff --git a/src/pj_param.c b/src/pj_param.c index 133f3ea6..b52ac940 100644 --- a/src/pj_param.c +++ b/src/pj_param.c @@ -131,17 +131,20 @@ PROJVALUE pj_param (projCtx ctx, paralist *pl, const char *opt) { /* Not found */ if (0==pl) { + /* Return value after the switch, so that the return path is */ + /* taken in all cases */ switch (type) { case 'b': case 'i': value.i = 0; - return value; + break; case 'd': case 'r': value.f = 0.; - return value; + break; case 's': value.s = 0; - return value; + break; } + return value; } /* Found parameter - now find its value */ -- cgit v1.2.3 From 6459f4fe68e38fba7cb2000523843d5fa84332cc Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 12 Mar 2018 23:49:01 +0100 Subject: Workaround false positive older cppcheck warning about null pointer dereference --- src/pj_apply_gridshift.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/pj_apply_gridshift.c b/src/pj_apply_gridshift.c index a2267cbd..c503ec0b 100644 --- a/src/pj_apply_gridshift.c +++ b/src/pj_apply_gridshift.c @@ -206,10 +206,10 @@ int pj_apply_gridshift_3( projCtx ctx, PJ_GRIDINFO **tables, int grid_count, if( ct != NULL ) { output = nad_cvt( input, inverse, ct ); - } - if ( output.lam != HUGE_VAL && debug_count++ < 20 ) - pj_log( ctx, PJ_LOG_DEBUG_MINOR, "pj_apply_gridshift(): used %s", ct->id ); + if ( output.lam != HUGE_VAL && debug_count++ < 20 ) + pj_log( ctx, PJ_LOG_DEBUG_MINOR, "pj_apply_gridshift(): used %s", ct->id ); + } if ( output.lam == HUGE_VAL ) { -- cgit v1.2.3 From 5b0a54f8d7ab0bd15a81352566f21d932facbb2c Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 12 Mar 2018 23:50:28 +0100 Subject: Avoid 'invalidscanf,scanf without field width limits can crash with huge input data on some versions of libc' with older cppcheck versions --- src/cct.c | 1 + src/nad2bin.c | 3 +++ 2 files changed, 4 insertions(+) (limited to 'src') diff --git a/src/cct.c b/src/cct.c index 57e53f97..8096986d 100644 --- a/src/cct.c +++ b/src/cct.c @@ -202,6 +202,7 @@ int main(int argc, char **argv) { } if (opt_given (o, "c")) { + /* cppcheck-suppress invalidscanf */ int ncols = sscanf (opt_arg (o, "c"), "%d,%d,%d,%d", columns_xyzt, columns_xyzt+1, columns_xyzt+2, columns_xyzt+3); if (ncols != nfields) { fprintf (stderr, "%s: Too few input columns given: '%s'\n", o->progname, opt_arg (o, "c")); diff --git a/src/nad2bin.c b/src/nad2bin.c index afb7d818..0c337513 100644 --- a/src/nad2bin.c +++ b/src/nad2bin.c @@ -103,6 +103,7 @@ int main(int argc, char **argv) { perror("fgets"); exit(1); } + /* cppcheck-suppress invalidscanf */ if ( EOF == scanf("%d %d %*d %lf %lf %lf %lf", &ct.lim.lam, &ct.lim.phi, &ct.ll.lam, &ct.del.lam, &ct.ll.phi, &ct.del.phi) ) { perror("scanf"); @@ -119,6 +120,7 @@ int main(int argc, char **argv) { ct.del.phi *= DEG_TO_RAD; /* load table */ for (p = ct.cvs, i = 0; i < ct.lim.phi; ++i) { + /* cppcheck-suppress invalidscanf */ if ( EOF == scanf("%d:%ld %ld", &ichk, &laml, &phil) ) { perror("scanf on row"); exit(1); @@ -131,6 +133,7 @@ int main(int argc, char **argv) { t.phi = (float) (phil * U_SEC_TO_RAD); *p++ = t; for (j = 1; j < ct.lim.lam; ++j) { + /* cppcheck-suppress invalidscanf */ if ( EOF == scanf("%ld %ld", &lam, &phi) ) { perror("scanf on column"); exit(1); -- cgit v1.2.3 From e5cf02937189b011f387e5b618ce73d6e296a38d Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 12 Mar 2018 23:51:22 +0100 Subject: Avoid 'duplicateExpression,Same expression on both sides of '!='' false positive with older cppcheck versions --- src/pj_apply_vgridshift.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/pj_apply_vgridshift.c b/src/pj_apply_vgridshift.c index 5a40ddfd..1b32c0f6 100644 --- a/src/pj_apply_vgridshift.c +++ b/src/pj_apply_vgridshift.c @@ -41,6 +41,7 @@ static double read_vgrid_value( PJ *defn, LP input, int *gridlist_count_p, PJ_GR int grid_ix2, grid_iy2; float *cvs; /* do not deal with NaN coordinates */ + /* cppcheck-suppress duplicateExpression */ if( input.phi != input.phi || input.lam != input.lam ) itable = *gridlist_count_p; -- cgit v1.2.3 From 643a5237908cbb40fd592ac6bfec9a095c267f6e Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 12 Mar 2018 23:52:22 +0100 Subject: Avoid 'Found calculation inside sizeof()' false positive with older cppcheck versions --- src/nad2bin.c | 3 +++ src/pj_gridinfo.c | 6 ++++++ 2 files changed, 9 insertions(+) (limited to 'src') diff --git a/src/nad2bin.c b/src/nad2bin.c index 0c337513..dfc4e2dd 100644 --- a/src/nad2bin.c +++ b/src/nad2bin.c @@ -180,7 +180,9 @@ int main(int argc, char **argv) { exit(2); } + /* cppcheck-suppress sizeofCalculation */ STATIC_ASSERT( MAX_TAB_ID == 80 ); + /* cppcheck-suppress sizeofCalculation */ STATIC_ASSERT( sizeof(pj_int32) == 4 ); /* for ct.lim.lam/phi */ memset( header, 0, sizeof(header) ); @@ -276,6 +278,7 @@ int main(int argc, char **argv) { ur.lam = ct.ll.lam + (ct.lim.lam-1) * ct.del.lam; ur.phi = ct.ll.phi + (ct.lim.phi-1) * ct.del.phi; + /* cppcheck-suppress sizeofCalculation */ STATIC_ASSERT( sizeof(nGSCount) == 4 ); memset( achHeader, 0, sizeof(achHeader) ); diff --git a/src/pj_gridinfo.c b/src/pj_gridinfo.c index 664effdc..e5e08c92 100644 --- a/src/pj_gridinfo.c +++ b/src/pj_gridinfo.c @@ -422,7 +422,9 @@ static int pj_gridinfo_init_ntv2( projCtx ctx, PAFile fid, PJ_GRIDINFO *gilist ) int num_subfiles, subfile; int must_swap; + /* cppcheck-suppress sizeofCalculation */ STATIC_ASSERT( sizeof(pj_int32) == 4 ); + /* cppcheck-suppress sizeofCalculation */ STATIC_ASSERT( sizeof(double) == 8 ); /* -------------------------------------------------------------------- */ @@ -645,7 +647,9 @@ static int pj_gridinfo_init_ntv1( projCtx ctx, PAFile fid, PJ_GRIDINFO *gi ) struct CTABLE *ct; LP ur; + /* cppcheck-suppress sizeofCalculation */ STATIC_ASSERT( sizeof(pj_int32) == 4 ); + /* cppcheck-suppress sizeofCalculation */ STATIC_ASSERT( sizeof(double) == 8 ); /* -------------------------------------------------------------------- */ @@ -730,7 +734,9 @@ static int pj_gridinfo_init_gtx( projCtx ctx, PAFile fid, PJ_GRIDINFO *gi ) double xorigin,yorigin,xstep,ystep; int rows, columns; + /* cppcheck-suppress sizeofCalculation */ STATIC_ASSERT( sizeof(pj_int32) == 4 ); + /* cppcheck-suppress sizeofCalculation */ STATIC_ASSERT( sizeof(double) == 8 ); /* -------------------------------------------------------------------- */ -- cgit v1.2.3 From a9269cc2aca3cd8851e3860d191eec27ae4a7a56 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 12 Mar 2018 23:57:46 +0100 Subject: Use a function to dereference a double value, to avoid false positive warning with older cppcheck versions --- src/pj_gridinfo.c | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/pj_gridinfo.c b/src/pj_gridinfo.c index e5e08c92..9a8b3dd5 100644 --- a/src/pj_gridinfo.c +++ b/src/pj_gridinfo.c @@ -65,6 +65,19 @@ static void swap_words( unsigned char *data, int word_size, int word_count ) } } +/************************************************************************/ +/* to_double() */ +/* */ +/* Returns a double from the pointed data. */ +/************************************************************************/ + +static double to_double( unsigned char* data ) +{ + double d; + memcpy(&d, data, sizeof(d)); + return d; +} + /************************************************************************/ /* pj_gridinfo_free() */ /************************************************************************/ @@ -510,14 +523,14 @@ static int pj_gridinfo_init_ntv2( projCtx ctx, PAFile fid, PJ_GRIDINFO *gilist ) strncpy( ct->id, (const char *) header + 8, 8 ); ct->id[8] = '\0'; - ct->ll.lam = - *((double *) (header+7*16+8)); /* W_LONG */ - ct->ll.phi = *((double *) (header+4*16+8)); /* S_LAT */ + ct->ll.lam = - to_double(header+7*16+8); /* W_LONG */ + ct->ll.phi = to_double(header+4*16+8); /* S_LAT */ - ur.lam = - *((double *) (header+6*16+8)); /* E_LONG */ - ur.phi = *((double *) (header+5*16+8)); /* N_LAT */ + ur.lam = - to_double(header+6*16+8); /* E_LONG */ + ur.phi = to_double(header+5*16+8); /* N_LAT */ - ct->del.lam = *((double *) (header+9*16+8)); - ct->del.phi = *((double *) (header+8*16+8)); + ct->del.lam = to_double(header+9*16+8); + ct->del.phi = to_double(header+8*16+8); ct->lim.lam = (pj_int32) (fabs(ur.lam-ct->ll.lam)/ct->del.lam + 0.5) + 1; ct->lim.phi = (pj_int32) (fabs(ur.phi-ct->ll.phi)/ct->del.phi + 0.5) + 1; @@ -693,12 +706,12 @@ static int pj_gridinfo_init_ntv1( projCtx ctx, PAFile fid, PJ_GRIDINFO *gi ) } strcpy( ct->id, "NTv1 Grid Shift File" ); - ct->ll.lam = - *((double *) (header+72)); - ct->ll.phi = *((double *) (header+24)); - ur.lam = - *((double *) (header+56)); - ur.phi = *((double *) (header+40)); - ct->del.lam = *((double *) (header+104)); - ct->del.phi = *((double *) (header+88)); + ct->ll.lam = - to_double(header+72); + ct->ll.phi = to_double(header+24); + ur.lam = - to_double(header+56); + ur.phi = to_double(header+40); + ct->del.lam = to_double(header+104); + ct->del.phi = to_double(header+88); ct->lim.lam = (pj_int32) (fabs(ur.lam-ct->ll.lam)/ct->del.lam + 0.5) + 1; ct->lim.phi = (pj_int32) (fabs(ur.phi-ct->ll.phi)/ct->del.phi + 0.5) + 1; -- cgit v1.2.3