Coverage Report

Created: 2026-12-01 06:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/c_api_fuzzer.cc
Line
Count
Source
1
/* Copyright 2026 Google LLC
2
3
Licensed under the Apache License, Version 2.0 (the "License");
4
you may not use this file except in compliance with the License.
5
You may obtain a copy of the License at
6
7
      http://www.apache.org/licenses/LICENSE-2.0
8
9
Unless required by applicable law or agreed to in writing, software
10
distributed under the License is distributed on an "AS IS" BASIS,
11
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
See the License for the specific language governing permissions and
13
limitations under the License.
14
*/
15
16
#include <stdint.h>
17
#include <stdio.h>
18
#include <string.h>
19
#include <vector>
20
#include <string>
21
22
#include "muParserDLL.h"
23
24
// Callbacks
25
762
muFloat_t MyFun1(muFloat_t v) { return v * 2; }
26
243
muFloat_t MyFun2(muFloat_t v1, muFloat_t v2) { return v1 + v2; }
27
21.5k
muFloat_t MyInfixFun(muFloat_t v) { return -v; }
28
1.03k
muFloat_t MyPostfixFun(muFloat_t v) { return v + 1; }
29
910
muFloat_t MyOprtFun(muFloat_t v1, muFloat_t v2) { return v1 * v2 + 1; }
30
31
24.6k
muFloat_t* MyVarFactory(const muChar_t* name, void* pUserData) {
32
24.6k
  static muFloat_t v[10];
33
24.6k
  return v;
34
24.6k
}
35
36
891k
muInt_t MyIdentFun(const muChar_t* name, muInt_t* pos, muFloat_t* val) {
37
891k
  if (strncmp(name, "test", 4) == 0) {
38
209
    *val = 123.0;
39
209
    *pos += 4;
40
209
    return 1;
41
209
  }
42
891k
  return 0;
43
891k
}
44
45
7.59k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
46
7.59k
  if (size < 10) return 0;
47
48
  // Use the first few bytes for configuration
49
7.59k
  uint8_t config = data[0];
50
7.59k
  uint8_t config2 = data[1];
51
7.59k
  int base_type = (config & 0x01) ? muBASETYPE_INT : muBASETYPE_FLOAT;
52
  
53
7.59k
  muParserHandle_t hParser = mupCreate(base_type);
54
7.59k
  if (!hParser) return 0;
55
56
  // Remaining data as expression
57
7.59k
  size_t expr_size = (size - 2) / 2;
58
7.59k
  std::string expr((char *)(data + 2), expr_size);
59
7.59k
  mupSetExpr(hParser, expr.c_str());
60
61
7.59k
  std::string custom_chars((char *)(data + 2 + expr_size), size - 2 - expr_size);
62
63
  // Conditional configurations based on config bits
64
7.59k
  if (config & 0x02) {
65
4.13k
    mupDefineInfixOprt(hParser, "!", MyInfixFun, 0, 1);
66
4.13k
  }
67
7.59k
  if (config & 0x04) {
68
3.84k
    mupDefinePostfixOprt(hParser, "!!", MyPostfixFun, 1);
69
3.84k
  }
70
7.59k
  if (config & 0x08) {
71
3.85k
    mupDefineOprt(hParser, "shr", MyOprtFun, 0, muOPRT_ASCT_LEFT, 1);
72
3.85k
  }
73
7.59k
  if (config & 0x10) {
74
6.44k
    mupSetVarFactory(hParser, MyVarFactory, NULL);
75
6.44k
  }
76
7.59k
  if (config & 0x20) {
77
6.23k
    mupAddValIdent(hParser, MyIdentFun);
78
6.23k
  }
79
  
80
7.59k
  if (!custom_chars.empty()) {
81
7.59k
    if (config & 0x40) {
82
2.04k
      mupDefineNameChars(hParser, custom_chars.c_str());
83
2.04k
    }
84
7.59k
    if (config & 0x80) {
85
1.62k
      mupDefineOprtChars(hParser, custom_chars.c_str());
86
1.62k
    }
87
7.59k
  }
88
89
  // Set some common things anyway
90
7.59k
  muFloat_t v1[10] = {1.0};
91
7.59k
  mupDefineVar(hParser, "v1", v1);
92
7.59k
  mupDefineConst(hParser, "c1", 3.14);
93
7.59k
  mupDefineStrConst(hParser, "s1", "hello");
94
  
95
  // From c_api_fuzzer.cc
96
7.59k
  mupDefineFun1(hParser, "f1", MyFun1, 1);
97
7.59k
  mupDefineFun2(hParser, "f2", MyFun2, 1);
98
99
  // Evaluate
100
7.59k
  try {
101
7.59k
    if (config2 & 0x01) {
102
      // Bulk mode evaluation
103
3.90k
      muFloat_t results[10];
104
3.90k
      mupEvalBulk(hParser, results, 10);
105
3.90k
    } else {
106
3.69k
      mupEval(hParser);
107
      
108
      // Try mupEvalMulti only if no error occurred in mupEval
109
3.69k
      if (!mupError(hParser)) {
110
1.20k
        int nNum;
111
1.20k
        mupEvalMulti(hParser, &nNum);
112
1.20k
      }
113
3.69k
    }
114
7.59k
  } catch (...) {
115
0
  }
116
117
  // Error handling
118
7.59k
  if (mupError(hParser)) {
119
2.36k
    mupGetErrorCode(hParser);
120
2.36k
    mupGetErrorPos(hParser);
121
2.36k
    mupGetErrorMsg(hParser);
122
2.36k
    mupGetErrorToken(hParser);
123
2.36k
  }
124
125
7.59k
  mupRelease(hParser);
126
7.59k
  return 0;
127
7.59k
}