void nullpointer1() { checkNullPointer("int foo(const Token *tok)\n" "{\n" " while (tok);\n" " tok = tok->next();\n" "}\n"); ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference\n", errout.str()); checkNullPointer("void foo()\n" "{\n" " for (const Token *tok = tokens; tok; tok = tok->next())\n" " {\n" " while (tok && tok->str() != \";\")\n" " tok = tok->next();\n" " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference\n", errout.str()); checkNullPointer("void foo()\n" "{\n" " for (const Token *tok = tokens; tok; tok = tok ? tok->next() : NULL)\n" " {\n" " while (tok && tok->str() != \";\")\n" " tok = tok->next();\n" " }\n" "}\n"); ASSERT_EQUALS("", errout.str()); }
// Dereferencing a pointer and then checking if it is null void nullpointer4() { // errors.. checkNullPointer("void foo(int *p)\n" "{\n" " *p = 0;\n" " if (!p)\n" " ;\n" "}\n"); ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference\n", errout.str()); // no error checkNullPointer("void foo()\n" "{\n" " int *p;\n" " f(&p);\n" " if (!p)\n" " ;\n" "}\n"); ASSERT_EQUALS("", errout.str()); checkNullPointer("void foo()\n" "{\n" " int **p = f();\n" " if (!p)\n" " ;\n" "}\n"); ASSERT_EQUALS("", errout.str()); checkNullPointer("void foo(int *p)\n" "{\n" " if (x)\n" " p = 0;\n" " else\n" " *p = 0;\n" " if (!p)\n" " ;\n" "}\n"); ASSERT_EQUALS("", errout.str()); checkNullPointer("void foo(int x)\n" "{\n" " int a = 2 * x;" " if (x == 0)\n" " ;\n" "}\n"); ASSERT_EQUALS("", errout.str()); }
void Card::setStatic() { if(state->isActive()) { delete state; CardState *nState = new CardStateStatic(this); checkNullPointer(nState); state = nState; } }
void nullpointer2() { // Null pointer dereference can only happen with pointers checkNullPointer("void foo()\n" "{\n" " Fred fred;\n" " while (fred);\n" " fred.hello();\n" "}\n"); ASSERT_EQUALS("", errout.str()); }
Card::Card(unsigned short num, GameWindow *window, QWidget *parent) : QWidget(parent), number(num), gameWindow(window) { state = new CardStateStatic(this); checkNullPointer(state); std::string path = std::string(Card::IMG_PATH) + std::to_string(number) + std::string(Card::IMG_EXT); QPixmap logo(path.c_str()); logo = logo.scaledToHeight(Card::DEFAULT_HEIGHT); int height = logo.height(), width = logo.width(); resize(width, height); label = new QLabel(this); checkNullPointer(label, [=]() { delete state; }); label->setGeometry(0, 0, width, height); label->setPixmap(logo); label->show(); }
/* read one or more query files specified with an option -i */ void getFileNames(int argc, char *argv[], char list[], char c, char ***fileNames, int *cntFiles, int *arg) { char **r; int numberOfFiles = 1; // get all file names (supposition: file name cannot start with -) while ((*arg + numberOfFiles) < argc && argv[*arg + numberOfFiles][0] != '-') { *fileNames = (char **)erealloc(*fileNames, numberOfFiles * sizeof (char *)); r = *fileNames; r[numberOfFiles - 1] = argv[*arg + numberOfFiles]; checkNullPointer(c, r[numberOfFiles - 1]); ++ numberOfFiles; } *cntFiles = numberOfFiles - 1; // set the argument checkMultipleArgDef(list, c); // position of the new argument (-1 because arg is increased by 1 as the last step of while loop) *arg = *arg + numberOfFiles - 1; }
Args *getArgs(int argc, char *argv[]){ Args *args; // char **r; char c; int arg; // int numberOfFiles; char list[256] = {0}; // options are not yet set args = (Args *)emalloc(sizeof(Args)); args->i = NULL; args->j = NULL; args->o = NULL; args->h = 0; args->p = 0; args->e = 0; args->d = NULL; args->n = NAMELENGTH; args->D = DEFAULT_D; args->w = DEFAULT_W; args->c = 0; args->q = 10000; args->W = NULL; args->P = 1-DEFAULT_P; args->v = 0; arg = 1; args->m = DEFAULT_M; args->t = 0; args->r = 0; args->s = 0; args->f = -1; // if none of the options is set as an argument, then take o1 args->o1 = 0; args->o2 = 0; args->I = NULL; args->M = 0; // default min window sum to be considered relevant while(arg < argc){ c = argv[arg][1]; switch (c){ case 'i': /* query file; if the option -d is included */ checkNullPointer(c, argv[arg]); //args->i = argv[arg]; /* then search for the query file: (1) in the program's dir (2) in the directory specified with -d*/ // read one or more query files specified with an option -i getFileNames(argc, argv, list, c, &args->i, &args->queryFileNumber, &arg); break; case 'j': /* subject files */ /* check that -d and -j option are used mutually exclusive! */ if (args->d) { eprintf("Options -d and -j are mutually exclusive"); } else { getFileNames(argc, argv, list, c, &args->j, &args->subjectFileNumber, &arg); break; } case 'd': /* directory where the subject files are saved and possibly the query file */ if (args->j) { eprintf("Options -d and -j are mutually exclusive"); } else { args->d = argv[++arg]; checkNullPointer(c, args->d); checkMultipleArgDef(list, c); break; } case 'o': /* output file */ checkNullPointer(c, argv[++arg]); args->o = argv[arg]; checkMultipleArgDef(list, c); break; case 'n': /* number of characters from header line printed */ checkNullPointer(c, argv[++arg]); args->n = atoi(argv[arg]); if(args->n < 0) { args->n = INT_MAX; } checkMultipleArgDef(list, c); break; case 'D': /* maximum depth of suffix tree */ args->D = atoi(argv[++arg]); checkNullPointer(c, argv[arg]); checkMultipleArgDef(list, c); break; case 'w': /* window width and window increment */ checkNullPointer(c, argv[++arg]); args->w = atoi(argv[arg]); checkMultipleArgDef(list, c); break; case 'c': /* sliding window increment */ checkNullPointer(c, argv[++arg]); args->c = atoi(argv[arg]); checkMultipleArgDef(list, c); break; case 'v': /* print program version test */ args->v = 1; break; case 'h': /* print help */ args->h = 1; break; case 'W': /* output file where window analysis for all subjects are printed */ args->W = argv[++arg]; checkNullPointer(c, args->W); checkMultipleArgDef(list, c); break; case 'P': /* fraction of random shustrings excluded from analysis */ args->P = atof(argv[++arg]); if (args->P < 0) { args->P = 0; } else if(args->P > 1) { args->P = 1; } args->P = 1 - args->P; break; case 'q': /* fast search - size of mini list*/ args->q = atol(argv[++arg]); if (args->q < 0) { args->q = 1000; } break; case 'm': /* multiplier for maxshulen by chance alone -- used for defining significant intervals */ args->m = atol(argv[++arg]); if (args->m < 0) { args->m = DEFAULT_M; } break; case 't': /* print run-time information */ args->t = 1; break; case 'r': /* print runner(s)-up information */ args->r = 1; break; case 'f': /* minimal length of recombination fragment */ args->f = atoi(argv[++arg]); //if (args->f < 0) { // args->f = (int)args->w; //} break; case 'o1': /* each annotation has the same subject set over all windows within annotation */ args->o1 = 1; break; case 'o2': /* subject set of an annotation is a subset of all windows within annotation */ args->o2 = 1; break; case 'M': /* minimal sum: max shulen by chance alone x window size */ args->M = 1; break; case 'I': /* name of input file that contains list of intervals; used for windows analysis based on precomputed intervals */ args->I = argv[++arg]; checkNullPointer(c, args->I); checkMultipleArgDef(list, c); break; default: printf("# unknown argument: %c\n", c); args->e = 1; return args; } // end switch arg++; } // end while // allowed stdin? // if (!args->i) { // printf("ERROR[gt]: Query file must be specified using -i option!\n"); // args->e = 1; //} if (args->c == 0) { args->c = (int)((float)args->w/10. + 0.5); } if (args->f < 0) { //args->f = (int)args->c; args->f = (int)((float)args->w / 4. + 0.5); } if (args->w < args->c) { printf("ERROR: Sliding window increment is greater than window size\n"); args->e = 1; } if (!args->o1 && !args->o2) { args->o1 = 1; // when none of the options is set, set o1 as default } else if (args->o1 && args->o2) { printf("ERROR: Options 1 and 2 are mutually exclusive\n"); args->e = 1; } if (args->o && args->I) { printf("Interval analysis exists, so it won't be computed again (-o will be ignored)\n"); } return args; }
extern "C" JNIEXPORT int JVM_handle_linux_signal(int sig, siginfo_t* info, void* ucVoid, int abort_if_unrecognized) { // in fact this isn't ucontext_t* at all, but struct sigcontext* // but Linux porting layer uses ucontext_t, so to minimize code change // we cast as needed ucontext_t* ucFake = (ucontext_t*) ucVoid; sigcontext* uc = (sigcontext*)ucVoid; Thread* t = ThreadLocalStorage::get_thread_slow(); SignalHandlerMark shm(t); // Note: it's not uncommon that JNI code uses signal/sigset to install // then restore certain signal handler (e.g. to temporarily block SIGPIPE, // or have a SIGILL handler when detecting CPU type). When that happens, // JVM_handle_linux_signal() might be invoked with junk info/ucVoid. To // avoid unnecessary crash when libjsig is not preloaded, try handle signals // that do not require siginfo/ucontext first. if (sig == SIGPIPE || sig == SIGXFSZ) { // allow chained handler to go first if (os::Linux::chained_handler(sig, info, ucVoid)) { return true; } else { if (PrintMiscellaneous && (WizardMode || Verbose)) { char buf[64]; warning("Ignoring %s - see bugs 4229104 or 646499219", os::exception_name(sig, buf, sizeof(buf))); } return true; } } JavaThread* thread = NULL; VMThread* vmthread = NULL; if (os::Linux::signal_handlers_are_installed) { if (t != NULL ){ if(t->is_Java_thread()) { thread = (JavaThread*)t; } else if(t->is_VM_thread()){ vmthread = (VMThread *)t; } } } // decide if this trap can be handled by a stub address stub = NULL; address pc = NULL; address npc = NULL; //%note os_trap_1 if (info != NULL && uc != NULL && thread != NULL) { pc = address(SIG_PC(uc)); npc = address(SIG_NPC(uc)); // Check to see if we caught the safepoint code in the // process of write protecting the memory serialization page. // It write enables the page immediately after protecting it // so we can just return to retry the write. if ((sig == SIGSEGV) && checkSerializePage(thread, (address)info->si_addr)) { // Block current thread until the memory serialize page permission restored. os::block_on_serialize_page_trap(); return 1; } if (checkPrefetch(uc, pc)) { return 1; } // Handle ALL stack overflow variations here if (sig == SIGSEGV) { if (checkOverflow(uc, pc, (address)info->si_addr, thread, &stub)) { return 1; } } if (sig == SIGBUS && thread->thread_state() == _thread_in_vm && thread->doing_unsafe_access()) { stub = StubRoutines::handler_for_unsafe_access(); } if (thread->thread_state() == _thread_in_Java) { do { // Java thread running in Java code => find exception handler if any // a fault inside compiled code, the interpreter, or a stub if ((sig == SIGSEGV) && checkPollingPage(pc, (address)info->si_addr, &stub)) { break; } if ((sig == SIGBUS) && checkByteBuffer(pc, &stub)) { break; } if ((sig == SIGSEGV || sig == SIGBUS) && checkVerifyOops(pc, (address)info->si_addr, &stub)) { break; } if ((sig == SIGSEGV) && checkZombie(uc, &pc, &stub)) { break; } if ((sig == SIGILL) && checkICMiss(uc, &pc, &stub)) { break; } if ((sig == SIGFPE) && checkFPFault(pc, info->si_code, thread, &stub)) { break; } if ((sig == SIGSEGV) && checkNullPointer(pc, (intptr_t)info->si_addr, thread, &stub)) { break; } } while (0); // jni_fast_Get<Primitive>Field can trap at certain pc's if a GC kicks in // and the heap gets shrunk before the field access. if ((sig == SIGSEGV) || (sig == SIGBUS)) { checkFastJNIAccess(pc, &stub); } } if (stub != NULL) { // save all thread context in case we need to restore it thread->set_saved_exception_pc(pc); thread->set_saved_exception_npc(npc); set_cont_address(uc, stub); return true; } } // signal-chaining if (os::Linux::chained_handler(sig, info, ucVoid)) { return true; } if (!abort_if_unrecognized) { // caller wants another chance, so give it to him return false; } if (pc == NULL && uc != NULL) { pc = os::Linux::ucontext_get_pc((ucontext_t*)uc); } // unmask current signal sigset_t newset; sigemptyset(&newset); sigaddset(&newset, sig); sigprocmask(SIG_UNBLOCK, &newset, NULL); VMError err(t, sig, pc, info, ucVoid); err.report_and_die(); ShouldNotReachHere(); }
// Dereferencing a struct and then checking if it is null void nullpointer3() { // errors.. checkNullPointer("void foo(struct ABC *abc)\n" "{\n" " int *a = abc->a;\n" " if (!abc)\n" " ;\n" "}\n"); ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference\n", errout.str()); // ok dereferencing in a condition checkNullPointer("void foo(struct ABC *abc)\n" "{\n" " if (abc && abc->a);\n" " if (!abc)\n" " ;\n" "}\n"); ASSERT_EQUALS("", errout.str()); // ok to use a linked list.. checkNullPointer("void foo(struct ABC *abc)\n" "{\n" " abc = abc->next;\n" " if (!abc)\n" " ;\n" "}\n"); ASSERT_EQUALS("", errout.str()); // reassign struct.. checkNullPointer("void foo(struct ABC *abc)\n" "{\n" " a = abc->a;\n" " abc = abc->next;\n" " if (!abc)\n" " ;\n" "}\n"); ASSERT_EQUALS("", errout.str()); checkNullPointer("void foo(struct ABC *abc)\n" "{\n" " a = abc->a;\n" " f(&abc);\n" " if (!abc)\n" " ;\n" "}\n"); ASSERT_EQUALS("", errout.str()); // goto.. checkNullPointer("void foo(struct ABC *abc)\n" "{\n" " if (!abc)\n" " goto out;" " a = abc->a;\n" " return;\n" "out:\n" " if (!abc)\n" " ;\n" "}\n"); ASSERT_EQUALS("", errout.str()); // loops.. checkNullPointer("void freeAbc(struct ABC *abc)\n" "{\n" " while (abc)\n" " {\n" " struct ABC *next = abc->next;\n" " if (abc) delete abc;\n" " abc = next;\n" " }\n" "}\n"); ASSERT_EQUALS("", errout.str()); checkNullPointer("void foo(struct ABC *abc)\n" "{\n" " int a = abc->a;" " do\n" " {\n" " if (abc)\n" " abc = abc->next;\n" " --a;\n" " }\n" " while (a > 0)\n" "}\n"); ASSERT_EQUALS("", errout.str()); }
void checkMultipleArgDef(char list[], char c) { #elif WIN static void checkMultipleArgDef(char list[], char c) { #endif if(!list[(int)c]){ list[(int)c] = 1; } else { printf("Warning: -%c multiply defined\n", c); // or error ??????? } } /* check whether an argument is a null pointer */ #if UNIX void checkNullPointer(char c, char *arg) { #elif WIN static void checkNullPointer(char c, char *arg) { #endif if (!arg) { eprintf("[ERROR] Option -%c: argument is NULL!\n", c); } } Args *getArgs(int argc, char *argv[]){ Args *args; char **r; char c; int arg = 1; int numberOfFiles = 0; char list[256] = {0}; // options are not yet set args = (Args *)emalloc(sizeof(Args)); args->j = NULL; args->o = NULL; args->h = 0; args->p = 0; args->e = 0; args->t = 0; args->g = 0; args->d = NULL; args->n = NULL; args->D = DEFAULT_D; args->subjectFileNumber = 0; /* Parameters for the divergence calculation */ /* When this values are all set to 0, then the Kr results are the same as for the original divergence calculation from the old kr program */ /* These default values are set to increase the speed of the divergence calculation, with keeping the Kr results with relative error < 1%*/ args->E = DEFAULT_E; args->T = DEFAULT_T; args->M = DEFAULT_M; //args->P = 0.5; /* best tree results ? with args->P == 0 */ args->P = 0; if (arg == argc) { return args; } while (arg < argc) { if (argv[arg][0] == '-') { c = argv[arg][1]; switch (c) { case 'j': /* subject files */ /* check that -d and -j option are used mutually exclusive! */ if (args->d) { eprintf("Options -d and -j are mutually exclusive!"); } else if (numberOfFiles > 0) { eprintf("Input files were already defined!"); } else { // get all query subject names // (supposition: file name cannot start with -) numberOfFiles = 1; while ((arg + numberOfFiles) < argc && argv[arg + numberOfFiles][0] != '-') { args->j = (char **)erealloc(args->j, numberOfFiles * sizeof (char *)); r = args->j; r[numberOfFiles - 1] = argv[arg + numberOfFiles]; checkNullPointer(c, r[numberOfFiles - 1]); ++ numberOfFiles; } args->subjectFileNumber = numberOfFiles - 1; // set the argument if (args->subjectFileNumber == 0) { checkNullPointer(c, NULL); } checkMultipleArgDef(list, c); // position of the new argument (-1 because arg is increased by 1 as the last step of while loop) arg = arg + numberOfFiles - 1; break; } case 'd': /* directory where the subject files are saved and possibly the query file */ if (args->j) { eprintf("Options -d and -j are mutually exclusive!"); } else { args->d = argv[++arg]; if (args->d[0] == '-') { args->d = NULL; } checkNullPointer(c, args->d); checkMultipleArgDef(list, c); break; } case 'o': /* output file */ checkNullPointer(c, argv[++arg]); args->o = argv[arg]; checkMultipleArgDef(list, c); break; case 'n': /* output file - phylip/neighbor format */ checkNullPointer(c, argv[++arg]); args->n = argv[arg]; checkMultipleArgDef(list, c); break; //case 'n': /* number of characters from header line printed */ // checkNullPointer(c, argv[++arg]); // args->n = atoi(argv[arg]); // if(args->n < 0) { // args->n = INT_MAX; // } // checkMultipleArgDef(list, c); // break; case 'D': /* maximum depth of suffix tree */ args->D = atoi(argv[++arg]); checkNullPointer(c, argv[arg]); checkMultipleArgDef(list, c); break; case 'p': /* print program information test */ args->p = 1; break; case 'g': /* use global GC content */ args->g = 1; break; case 'h': /* print help */ args->h = 1; break; case 't': /* print run-time */ args->t = 1; break; case 'P': /* fraction of random shustrings excluded from analysis */ args->P = atof(argv[++arg]); if (args->P < 0) { args->P = 0; } else if(args->P > 1) { args->P = 1; } break; /* parameters for the divergence calculation */ case 'E': /* function divergence: errd */ args->E = atof(argv[++arg]); if (args->E < 0) { args->E = 0; } else if (args->E > 1) { args->E = DEFAULT_E; } break; case 'T': /* function expShulen: t1 */ args->T = atof(argv[++arg]); if (args->T < 0) { args->T = 0; } else if (args->T > 1) { args->T = DEFAULT_T; } break; case 'M': /* function pmax: m_t */ args->M = atof(argv[++arg]); if (args->M < 0) { args->M = 0; } else if (args->M > 1) { args->M = DEFAULT_M; } break; default: printf("# unknown argument: %c\n", c); args->e = 1; return args; } // end switch } else { // get all query subject names - "fake" option '*' // (supposition: file name cannot start with -) numberOfFiles = 0; while (arg < argc && argv[arg][0] != '-') { args->j = (char **)erealloc(args->j, (numberOfFiles + 1)* sizeof (char *)); r = args->j; r[numberOfFiles] = argv[arg]; checkNullPointer('*', r[numberOfFiles]); ++ numberOfFiles; ++ arg; } args->subjectFileNumber = numberOfFiles; // set the argument if (args->subjectFileNumber == 0) { checkNullPointer('*', NULL); } checkMultipleArgDef(list, '*'); // position of the new argument (-1 because arg is increased by 1 as the last step of while loop, and -1 because it again increases in the while-loop) arg = arg - 1; } arg++; } return args; } void printUsage(char *version){ printf("kr version %s ", version); printf("distributed under the GNU General Public License.\n"); printf("purpose: estimate pairwise distances, K_r, from DNA sequences\n"); printf("usage: %s [options] [filenames]\n", progname()); printf(" or: %s [options] -j <FILE(s)> \n", progname()); printf(" or: %s [options] -d <DIRECTORY> \n", progname()); printf("options:\n"); printf("\t[-o <FILE> write output to FILE; default: FILE=stdout]\n"); printf("\t[-n <FILE> write detailed output to FILE]\n"); printf("\t[-t print run-time in the detailed output file; printed only when option -n is used]\n"); printf("\t[-D <NUM> maximum depth of suffix tree; default: %d]\n", DEFAULT_D); printf("\t[-P <NUM> fraction of random shustrings excluded from analysis; P e [0, 1]; default: 0]\n"); printf("\t[-E <NUM> relative divergence error; default: %lf]\n", DEFAULT_E); printf("\t[-T <NUM> relative error of the average shustring length; default: %lf]\n", DEFAULT_T); printf("\t[-M <NUM> threshold for small values of logarithm; default: %lf]\n", DEFAULT_M); printf("\t[-g use global GC content (fast); default: use pairwise GC content (slow, accurate)]\n"); printf("\t[-p print information about program]\n"); printf("\t[-h print this help message]\n"); }