<commit_before>
/*
 * Read the documentation in debugMexEval.m for instructions.
 * Happy bugfinding!  - Russ
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "drake/util/MexWrapper.h"

#include <matrix.h>
#include <mat.h>
#include <string>
#include <map>

using namespace std;

void cleanupDrakeMexPointers(void) {
  // intentionally left blank; overloaded in debugMexLib.cpp using a
  // DYLD_INSERT_LIBRARIES
}

int mexPrintf(const char* message)  // todo: handle the variable arguments case
{
  return printf("%s", message);
}

mxArray* mexCallMATLABWithTrap(int nlhs, mxArray* plhs[], int nrhs,
                               mxArray* prhs[], const char* functionName) {
  printf("called mexCallMATLABWithTrap.\n");
  return NULL;
}

// todo: implement stubs for other mex functions (mexErrMsgAndTxt, ...) here

int main(
    int argc,
    char* argv[])  // todo: take the mex function and dynamically load / run it.
{
  map<string, MexWrapper> mexfiles;
  map<string, MexWrapper>::iterator iter;

  // load inputs from matfile
  MATFile* pmatfile = matOpen("/tmp/mex_debug.mat", "r");
  if (!pmatfile) {
    fprintf(stderr, "Failed to open %s\n", "/tmp/mex_debug.mat");
    exit(EXIT_FAILURE);
  }

  int i, count = 1;
  char buff[100], countstr[6] = "00000";
  string name;

  while (true) {
    sprintf(buff, "%d", count);
    memcpy(&countstr[5 - strlen(buff)], buff, strlen(buff));

    name = "fun_";
    name += countstr;
    mxArray* fun = matGetVariable(pmatfile, name.c_str());
    if (!fun) break;  // then we've gotten to the end of the input "tape"
<commit_msg>Convert sprintf to snprintf in drakeDebugMex.cpp. Make buff smaller so that the memcpy also doesn't overflow.<commit_after>
/*
 * Read the documentation in debugMexEval.m for instructions.
 * Happy bugfinding!  - Russ
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "drake/util/MexWrapper.h"

#include <matrix.h>
#include <mat.h>
#include <string>
#include <map>

using namespace std;

void cleanupDrakeMexPointers(void) {
  // intentionally left blank; overloaded in debugMexLib.cpp using a
  // DYLD_INSERT_LIBRARIES
}

int mexPrintf(const char* message)  // todo: handle the variable arguments case
{
  return printf("%s", message);
}

mxArray* mexCallMATLABWithTrap(int nlhs, mxArray* plhs[], int nrhs,
                               mxArray* prhs[], const char* functionName) {
  printf("called mexCallMATLABWithTrap.\n");
  return NULL;
}

// todo: implement stubs for other mex functions (mexErrMsgAndTxt, ...) here

int main(
    int argc,
    char* argv[])  // todo: take the mex function and dynamically load / run it.
{
  map<string, MexWrapper> mexfiles;
  map<string, MexWrapper>::iterator iter;

  // load inputs from matfile
  MATFile* pmatfile = matOpen("/tmp/mex_debug.mat", "r");
  if (!pmatfile) {
    fprintf(stderr, "Failed to open %s\n", "/tmp/mex_debug.mat");
    exit(EXIT_FAILURE);
  }

  int i, count = 1;
  char buff[6], countstr[6] = "00000";
  string name;

  while (true) {
    snprintf(buff, sizeof(buff), "%d", count);
    memcpy(&countstr[5 - strlen(buff)], buff, strlen(buff));

    name = "fun_";
    name += countstr;
    mxArray* fun = matGetVariable(pmatfile, name.c_str());
    if (!fun) break;  // then we've gotten to the end of the input "tape"

{'fraction_non_alphanumeric': 0.1378582202111614, 'fraction_numerical': 0.006033182503770739, 'mean_word_length': 3.5674931129476586, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5910124', 'n_tokens_mistral': 1165, 'n_tokens_neox': 1133, 'n_words': 332}