Dict Options::sanitize(const Dict& opts) { // Drop nulls if (has_null(opts)) { // Create a new dictionary without the null entries Dict ret; for (auto&& op : opts) { if (!op.second.is_null()) ret[op.first] = op.second; } return ret; } // Treat the case where any of the options have a dot (dictionary shorthand) if (has_dot(opts)) { // New options dictionary being constructed Dict ret; // Sub-dictionary and corresponding name being constructed Dict sopts; std::string sname; // Process options for (auto&& op : opts) { // Find the dot if any string::size_type dotpos = op.first.find('.'), dotpos_end; if (dotpos==string::npos) { dotpos = op.first.find("__"); if (dotpos!=string::npos) dotpos_end = dotpos+2; } else { dotpos_end = dotpos+1; } // Flush last sub-dictionary if (!sname.empty() && (dotpos==string::npos || op.first.compare(0, dotpos, sname)!=0)) { ret[sname] = sopts; sname.clear(); sopts.clear(); } // Add to dictionary if (dotpos != string::npos) { sname = op.first.substr(0, dotpos); sopts[op.first.substr(dotpos_end)] = op.second; } else { ret[op.first] = op.second; } } // Flush trailing sub-dictionary if (!sname.empty()) ret[sname] = sopts; return ret; } // Nothing to do return opts; }
char *pt_debugger::readstr(unsigned long addr, size_t max_size) { size_t size = 4096, read = 0; char *buf = (char *) malloc(size); union { ptrace_read_t val; char byte[sizeof(ptrace_read_t)]; } data; while (true) { if (read + sizeof(ptrace_read_t) > size) { if (max_size && size >= max_size) { buf[max_size-1] = 0; break; } size += 4096; if (max_size && size > max_size) size = max_size; void *nbuf = realloc(buf, size); if (!nbuf) { buf[size-4097] = 0; break; } buf = (char *) nbuf; } #if PTBOX_FREEBSD // TODO: we could use PT_IO to speed up this entire function by reading chunks rather than byte data.val = ptrace(PT_READ_D, tid, (caddr_t) (addr + read), 0); #else data.val = ptrace(PTRACE_PEEKDATA, tid, addr + read, NULL); #endif memcpy(buf + read, data.byte, sizeof(ptrace_read_t)); if (has_null(data.byte, sizeof(ptrace_read_t))) break; read += sizeof(ptrace_read_t); } return buf; }
static void compile_regexp(struct grep_pat *p, struct grep_opt *opt) { int icase, ascii_only; int err; p->word_regexp = opt->word_regexp; p->ignore_case = opt->ignore_case; icase = opt->regflags & REG_ICASE || p->ignore_case; ascii_only = !has_non_ascii(p->pattern); /* * Even when -F (fixed) asks us to do a non-regexp search, we * may not be able to correctly case-fold when -i * (ignore-case) is asked (in which case, we'll synthesize a * regexp to match the pattern that matches regexp special * characters literally, while ignoring case differences). On * the other hand, even without -F, if the pattern does not * have any regexp special characters and there is no need for * case-folding search, we can internally turn it into a * simple string match using kws. p->fixed tells us if we * want to use kws. */ if (opt->fixed || has_null(p->pattern, p->patternlen) || is_fixed(p->pattern, p->patternlen)) p->fixed = !icase || ascii_only; else p->fixed = 0; if (p->fixed) { p->kws = kwsalloc(icase ? tolower_trans_tbl : NULL); kwsincr(p->kws, p->pattern, p->patternlen); kwsprep(p->kws); return; } else if (opt->fixed) { /* * We come here when the pattern has the non-ascii * characters we cannot case-fold, and asked to * ignore-case. */ compile_fixed_regexp(p, opt); return; } if (opt->pcre2) { compile_pcre2_pattern(p, opt); return; } if (opt->pcre1) { compile_pcre1_regexp(p, opt); return; } err = regcomp(&p->regexp, p->pattern, opt->regflags); if (err) { char errbuf[1024]; regerror(err, &p->regexp, errbuf, 1024); regfree(&p->regexp); compile_regexp_failed(p, errbuf); } }
bool Options::is_sane(const Dict& opts) { return !has_dot(opts) && !has_null(opts); }