Bug Summary

File:gromacs/fileio/vmdio.c
Location:line 431, column 29
Description:Access to field 'has_velocities' results in a dereference of a null pointer (loaded from variable 'metadata')

Annotated Source Code

1/*
2 * This file is part of the GROMACS molecular simulation package.
3 *
4 * Copyright (c) 2009,2010,2012,2013,2014, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
8 *
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
13 *
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 *
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
31 *
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
34 */
35
36#ifdef HAVE_CONFIG_H1
37#include <config.h>
38#endif
39
40/* Derived from PluginMgr.C and catdcd.c */
41
42/* PluginMgr.C: Copyright: */
43/***************************************************************************
44 * cr
45 * cr (C) Copyright 1995-2009 The Board of Trustees of the
46 * cr University of Illinois
47 * cr All Rights Reserved
48 * cr
49 Developed by: Theoretical and Computational Biophysics Group
50 University of Illinois at Urbana-Champaign
51 http://www.ks.uiuc.edu/
52
53 Permission is hereby granted, free of charge, to any person obtaining a copy of
54 this software and associated documentation files (the Software), to deal with
55 the Software without restriction, including without limitation the rights to
56 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
57 of the Software, and to permit persons to whom the Software is furnished to
58 do so, subject to the following conditions:
59
60 Redistributions of source code must retain the above copyright notice,
61 this list of conditions and the following disclaimers.
62
63 Redistributions in binary form must reproduce the above copyright notice,
64 this list of conditions and the following disclaimers in the documentation
65 and/or other materials provided with the distribution.
66
67 Neither the names of Theoretical and Computational Biophysics Group,
68 University of Illinois at Urbana-Champaign, nor the names of its contributors
69 may be used to endorse or promote products derived from this Software without
70 specific prior written permission.
71
72 THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
73 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
74 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
75 THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
76 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
77 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
78 OTHER DEALINGS WITH THE SOFTWARE.
79 ***************************************************************************/
80
81/* catdcd.c: Copyright: */
82/*****************************************************************************/
83/* */
84/* (C) Copyright 2001-2005 Justin Gullingsrud and the University of Illinois.*/
85/* */
86/*****************************************************************************/
87
88#include <stdlib.h>
89#include <stdio.h>
90#include <string.h>
91#include <assert.h>
92
93/*
94 * Plugin header files; get plugin source from www.ks.uiuc.edu/Research/vmd"
95 */
96#include "external/vmd_molfile/molfile_plugin.h"
97#include "external/vmd_molfile/vmddlopen.h"
98#ifndef GMX_NATIVE_WINDOWS
99#include <glob.h>
100#else
101#include <windows.h>
102#include <shlobj.h>
103#endif
104#include "gromacs/utility/smalloc.h"
105#include "gromacs/utility/futil.h"
106#include "vmdio.h"
107
108
109#include "types/simple.h"
110#include "gromacs/math/vec.h"
111#include "gmxfio.h"
112
113
114typedef int (*initfunc)(void);
115typedef int (*regfunc)(void *, vmdplugin_register_cb);
116typedef int (*finifunc)(void);
117
118
119
120static int register_cb(void *v, vmdplugin_t *p)
121{
122 const char *key = p->name;
123 t_gmxvmdplugin *vmdplugin = (t_gmxvmdplugin*)v;
124
125 if (strcmp(key, vmdplugin->filetype)__extension__ ({ size_t __s1_len, __s2_len; (__builtin_constant_p
(key) && __builtin_constant_p (vmdplugin->filetype
) && (__s1_len = strlen (key), __s2_len = strlen (vmdplugin
->filetype), (!((size_t)(const void *)((key) + 1) - (size_t
)(const void *)(key) == 1) || __s1_len >= 4) && (!
((size_t)(const void *)((vmdplugin->filetype) + 1) - (size_t
)(const void *)(vmdplugin->filetype) == 1) || __s2_len >=
4)) ? __builtin_strcmp (key, vmdplugin->filetype) : (__builtin_constant_p
(key) && ((size_t)(const void *)((key) + 1) - (size_t
)(const void *)(key) == 1) && (__s1_len = strlen (key
), __s1_len < 4) ? (__builtin_constant_p (vmdplugin->filetype
) && ((size_t)(const void *)((vmdplugin->filetype)
+ 1) - (size_t)(const void *)(vmdplugin->filetype) == 1) ?
__builtin_strcmp (key, vmdplugin->filetype) : (__extension__
({ const unsigned char *__s2 = (const unsigned char *) (const
char *) (vmdplugin->filetype); int __result = (((const unsigned
char *) (const char *) (key))[0] - __s2[0]); if (__s1_len >
0 && __result == 0) { __result = (((const unsigned char
*) (const char *) (key))[1] - __s2[1]); if (__s1_len > 1 &&
__result == 0) { __result = (((const unsigned char *) (const
char *) (key))[2] - __s2[2]); if (__s1_len > 2 &&
__result == 0) __result = (((const unsigned char *) (const char
*) (key))[3] - __s2[3]); } } __result; }))) : (__builtin_constant_p
(vmdplugin->filetype) && ((size_t)(const void *)(
(vmdplugin->filetype) + 1) - (size_t)(const void *)(vmdplugin
->filetype) == 1) && (__s2_len = strlen (vmdplugin
->filetype), __s2_len < 4) ? (__builtin_constant_p (key
) && ((size_t)(const void *)((key) + 1) - (size_t)(const
void *)(key) == 1) ? __builtin_strcmp (key, vmdplugin->filetype
) : (- (__extension__ ({ const unsigned char *__s2 = (const unsigned
char *) (const char *) (key); int __result = (((const unsigned
char *) (const char *) (vmdplugin->filetype))[0] - __s2[0
]); if (__s2_len > 0 && __result == 0) { __result =
(((const unsigned char *) (const char *) (vmdplugin->filetype
))[1] - __s2[1]); if (__s2_len > 1 && __result == 0
) { __result = (((const unsigned char *) (const char *) (vmdplugin
->filetype))[2] - __s2[2]); if (__s2_len > 2 &&
__result == 0) __result = (((const unsigned char *) (const char
*) (vmdplugin->filetype))[3] - __s2[3]); } } __result; })
))) : __builtin_strcmp (key, vmdplugin->filetype)))); })
== 0)
126 {
127 vmdplugin->api = (molfile_plugin_t *)p;
128 }
129 return VMDPLUGIN_SUCCESS0;
130}
131
132static int load_sharedlibrary_plugins(const char *fullpath, t_gmxvmdplugin* vmdplugin)
133{
134 /* Open the dll; try to execute the init function. */
135 void *handle, *ifunc, *registerfunc;
136 handle = vmddlopen(fullpath);
137 if (!handle)
138 {
139 if (debug)
140 {
141 fprintf(debug, "\nUnable to open dynamic library %s.\n%s\n", fullpath, vmddlerror()); /*only to debug because of stdc++ erros */
142 }
143 return 0;
144 }
145
146 ifunc = vmddlsym(handle, "vmdplugin_init");
147 if (!ifunc || ((initfunc)(ifunc))())
148 {
149 printf("\nvmdplugin_init() for %s returned an error; plugin(s) not loaded.\n", fullpath);
150 vmddlclose(handle);
151 return 0;
152 }
153
154 registerfunc = vmddlsym(handle, "vmdplugin_register");
155 if (!registerfunc)
156 {
157 printf("\nDidn't find the register function in %s; plugin(s) not loaded.\n", fullpath);
158 vmddlclose(handle);
159 return 0;
160 }
161 else
162 {
163 /* Load plugins from the library.*/
164 ((regfunc)registerfunc)(vmdplugin, register_cb);
165 }
166
167 /* in case this library does not support the filetype, close it */
168 if (vmdplugin->api == NULL((void*)0))
169 {
170 vmddlclose(handle);
171 }
172
173 return 1;
174}
175
176/*return: 1: success, 0: last frame, -1: error*/
177gmx_bool read_next_vmd_frame(t_trxframe *fr)
178{
179 int rc, i;
180 rvec vec, angle;
181 molfile_timestep_t ts;
182
183
184 fr->bV = fr->vmdplugin->bV;
185
186#ifdef GMX_DOUBLE
187 snew(ts.coords, fr->natoms*3)(ts.coords) = save_calloc("ts.coords", "/home/alexxy/Develop/gromacs/src/gromacs/fileio/vmdio.c"
, 187, (fr->natoms*3), sizeof(*(ts.coords)))
;
188 if (fr->bV)
189 {
190 snew(ts.velocities, fr->natoms*3)(ts.velocities) = save_calloc("ts.velocities", "/home/alexxy/Develop/gromacs/src/gromacs/fileio/vmdio.c"
, 190, (fr->natoms*3), sizeof(*(ts.velocities)))
;
191 }
192#else
193 ts.coords = (float*)fr->x;
194 if (fr->bV)
195 {
196 ts.velocities = (float*)fr->v;
197 }
198#endif
199
200 rc = fr->vmdplugin->api->read_next_timestep(fr->vmdplugin->handle, fr->natoms, &ts);
201
202 if (rc < -1)
203 {
204 fprintf(stderrstderr, "\nError reading input file (error code %d)\n", rc);
205 }
206 if (rc < 0)
207 {
208 fr->vmdplugin->api->close_file_read(fr->vmdplugin->handle);
209 return 0;
210 }
211
212#ifdef GMX_DOUBLE
213 for (i = 0; i < fr->natoms; i++)
214 {
215 fr->x[i][0] = .1*ts.coords[i*3];
216 fr->x[i][1] = .1*ts.coords[i*3+1];
217 fr->x[i][2] = .1*ts.coords[i*3+2];
218 if (fr->bV)
219 {
220 fr->v[i][0] = .1*ts.velocities[i*3];
221 fr->v[i][1] = .1*ts.velocities[i*3+1];
222 fr->v[i][2] = .1*ts.velocities[i*3+2];
223 }
224 }
225 sfree(ts.coords)save_free("ts.coords", "/home/alexxy/Develop/gromacs/src/gromacs/fileio/vmdio.c"
, 225, (ts.coords))
;
226 if (fr->bV)
227 {
228 sfree(ts.velocities)save_free("ts.velocities", "/home/alexxy/Develop/gromacs/src/gromacs/fileio/vmdio.c"
, 228, (ts.velocities))
;
229 }
230#else
231 for (i = 0; i < fr->natoms; i++)
232 {
233 svmul(.1, fr->x[i], fr->x[i]);
234 if (fr->bV)
235 {
236 svmul(.1, fr->v[i], fr->v[i]);
237 }
238 }
239#endif
240
241 fr->bX = 1;
242 fr->bBox = 1;
243 vec[0] = .1*ts.A; vec[1] = .1*ts.B; vec[2] = .1*ts.C;
244 angle[0] = ts.alpha; angle[1] = ts.beta; angle[2] = ts.gamma;
245 matrix_convert(fr->box, vec, angle);
246 if (fr->vmdplugin->api->abiversion > 10)
247 {
248 fr->bTime = TRUE1;
249 fr->time = ts.physical_time;
250 }
251 else
252 {
253 fr->bTime = FALSE0;
254 }
255
256
257 return 1;
258}
259
260static int load_vmd_library(const char *fn, t_gmxvmdplugin *vmdplugin)
261{
262 char pathname[GMX_PATH_MAX4096], filename[GMX_PATH_MAX4096];
263 const char *pathenv;
264 const char *err;
265 int i;
266 int ret = 0;
267 char pathenv_buffer[GMX_PATH_MAX4096];
268#ifndef GMX_NATIVE_WINDOWS
269 glob_t globbuf;
270 const char *defpath_suffix = "/plugins/*/molfile";
271 const char *defpathenv = GMX_VMD_PLUGIN_PATH"/usr/local/lib/vmd/plugins/*/molfile";
272#else
273 WIN32_FIND_DATA ffd;
274 HANDLE hFind = INVALID_HANDLE_VALUE;
275 char progfolder[GMX_PATH_MAX4096];
276 char defpathenv[GMX_PATH_MAX4096];
277 const char *defpath_suffix = "\\plugins\\WIN32\\molfile";
278 SHGetFolderPath(NULL((void*)0), CSIDL_PROGRAM_FILES, NULL((void*)0), SHGFP_TYPE_CURRENT, progfolder);
279 sprintf(defpathenv, "%s\\University of Illinois\\VMD\\plugins\\WIN32\\molfile", progfolder);
280#endif
281
282 vmdplugin->api = NULL((void*)0);
283 vmdplugin->filetype = strrchr(fn, '.');
284 if (!vmdplugin->filetype)
285 {
286 return 0;
287 }
288 vmdplugin->filetype++;
289
290 /* First look for an explicit path given at run time for the
291 * plugins, then an implicit run-time path, and finally for one
292 * given at configure time. This last might be hard-coded to the
293 * default for VMD installs. */
294 pathenv = getenv("VMD_PLUGIN_PATH");
295 if (pathenv == NULL((void*)0))
296 {
297 pathenv = getenv("VMDDIR");
298 if (NULL((void*)0) == pathenv)
299 {
300 printf("\nNeither VMD_PLUGIN_PATH or VMDDIR set. ");
301 printf("Using default location:\n%s\n", defpathenv);
302 pathenv = defpathenv;
303 }
304 else
305 {
306 printf("\nVMD_PLUGIN_PATH no set, but VMDDIR is set. ");
307#ifdef _MSC_VER
308 _snprintf_s(pathenv_buffer, sizeof(pathenv_buffer), _TRUNCATE, "%s%s", pathenv, defpath_suffix);
309#else
310 snprintf(pathenv_buffer, sizeof(pathenv_buffer), "%s%s", pathenv, defpath_suffix);
311#endif
312 printf("Using semi-default location:\n%s\n", pathenv_buffer);
313 pathenv = pathenv_buffer;
314 }
315 }
316 strncpy(pathname, pathenv, sizeof(pathname))__builtin_strncpy (pathname, pathenv, sizeof(pathname));
317#ifndef GMX_NATIVE_WINDOWS
318 strcat(pathname, "/*.so");
319 glob(pathname, 0, NULL((void*)0), &globbuf);
320 if (globbuf.gl_pathc == 0)
321 {
322 printf("\nNo VMD Plugins found\n"
323 "Set the environment variable VMD_PLUGIN_PATH to the molfile folder within the\n"
324 "VMD installation.\n"
325 "The architecture (e.g. 32bit versus 64bit) of Gromacs and VMD has to match.\n");
326 return 0;
327 }
328 for (i = 0; i < globbuf.gl_pathc && vmdplugin->api == NULL((void*)0); i++)
329 {
330 /* FIXME: Undefined which plugin is chosen if more than one plugin
331 can read a certain file ending. Requires some additional command
332 line option or enviroment variable to specify which plugin should
333 be picked.
334 */
335 ret |= load_sharedlibrary_plugins(globbuf.gl_pathv[i], vmdplugin);
336 }
337 globfree(&globbuf);
338#else
339 strcat(pathname, "\\*.so");
340 hFind = FindFirstFile(pathname, &ffd);
341 if (INVALID_HANDLE_VALUE == hFind)
342 {
343 printf("\nNo VMD Plugins found\n");
344 return 0;
345 }
346 do
347 {
348 sprintf(filename, "%s\\%s", pathenv, ffd.cFileName);
349 ret |= load_sharedlibrary_plugins(filename, vmdplugin);
350 }
351 while (FindNextFile(hFind, &ffd ) != 0 && vmdplugin->api == NULL((void*)0));
352 FindClose(hFind);
353#endif
354
355 if (!ret)
356 {
357 printf("\nCould not open any VMD library.\n");
358 err = vmddlerror();
359 if (!err)
360 {
361 printf("Compiled with dlopen?\n");
362 }
363 else
364 {
365 printf("Last error:\n%s\n", err);
366 }
367 return 0;
368 }
369
370 if (vmdplugin->api == NULL((void*)0))
371 {
372 printf("\nNo plugin for %s found\n", vmdplugin->filetype);
373 return 0;
374 }
375
376 if (vmdplugin->api->abiversion < 10)
377 {
378 printf("\nPlugin and/or VMD is too old. At least VMD 1.8.6 is required.\n");
379 return 0;
380 }
381
382 printf("\nUsing VMD plugin: %s (%s)\n", vmdplugin->api->name, vmdplugin->api->prettyname);
383
384 return 1;
385
386}
387
388int read_first_vmd_frame(const char *fn, t_trxframe *fr)
389{
390 molfile_timestep_metadata_t *metadata = NULL((void*)0);
1
'metadata' initialized to a null pointer value
391
392 snew(fr->vmdplugin, 1)(fr->vmdplugin) = save_calloc("fr->vmdplugin", "/home/alexxy/Develop/gromacs/src/gromacs/fileio/vmdio.c"
, 392, (1), sizeof(*(fr->vmdplugin)))
;
393 if (!load_vmd_library(fn, fr->vmdplugin))
2
Taking false branch
394 {
395 return 0;
396 }
397
398 fr->vmdplugin->handle = fr->vmdplugin->api->open_file_read(fn, fr->vmdplugin->filetype, &fr->natoms);
399
400 if (!fr->vmdplugin->handle)
3
Taking false branch
401 {
402 fprintf(stderrstderr, "\nError: could not open file '%s' for reading.\n",
403 fn);
404 return 0;
405 }
406
407 if (fr->natoms == MOLFILE_NUMATOMS_UNKNOWN-1)
4
Taking false branch
408 {
409 fprintf(stderrstderr, "\nFormat of file %s does not record number of atoms.\n", fn);
410 return 0;
411 }
412 else if (fr->natoms == MOLFILE_NUMATOMS_NONE0)
5
Taking false branch
413 {
414 fprintf(stderrstderr, "\nNo atoms found by VMD plugin in file %s.\n", fn );
415 return 0;
416 }
417 else if (fr->natoms < 1) /*should not be reached*/
6
Taking false branch
418 {
419 fprintf(stderrstderr, "\nUnknown number of atoms %d for VMD plugin opening file %s.\n",
420 fr->natoms, fn );
421 return 0;
422 }
423
424 snew(fr->x, fr->natoms)(fr->x) = save_calloc("fr->x", "/home/alexxy/Develop/gromacs/src/gromacs/fileio/vmdio.c"
, 424, (fr->natoms), sizeof(*(fr->x)))
;
425
426 fr->vmdplugin->bV = 0;
427 if (fr->vmdplugin->api->abiversion > 10 && fr->vmdplugin->api->read_timestep_metadata)
7
Taking true branch
428 {
429 fr->vmdplugin->api->read_timestep_metadata(fr->vmdplugin->handle, metadata);
430 assert(metadata)((void) (0));
431 fr->vmdplugin->bV = metadata->has_velocities;
8
Access to field 'has_velocities' results in a dereference of a null pointer (loaded from variable 'metadata')
432 if (fr->vmdplugin->bV)
433 {
434 snew(fr->v, fr->natoms)(fr->v) = save_calloc("fr->v", "/home/alexxy/Develop/gromacs/src/gromacs/fileio/vmdio.c"
, 434, (fr->natoms), sizeof(*(fr->v)))
;
435 }
436 }
437 else
438 {
439 fprintf(stderrstderr,
440 "\nThis trajectory is being read with a VMD plug-in from before VMD"
441 "\nversion 1.8, or from a trajectory that lacks time step metadata."
442 "\nEither way, GROMACS cannot tell whether the trajectory has velocities.\n");
443 }
444 return 1;
445
446}