Skip to content

Commit 98c78e7

Browse files
committed
Fix aliasing bug between NodeKafka::Conf and ConfImpl
1 parent ae6525d commit 98c78e7

File tree

4 files changed

+61
-38
lines changed

4 files changed

+61
-38
lines changed

lib/client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var util = require('util');
1515
var Kafka = require('../librdkafka.js');
1616
var assert = require('assert');
1717

18-
const bindingVersion = 'v0.1.11-devel';
18+
const bindingVersion = require('./util').bindingVersion;
1919

2020
var LibrdKafkaError = require('./error');
2121

lib/util.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ util.shallowCopy = function (obj) {
2727
util.isObject = function (obj) {
2828
return obj && typeof obj === 'object';
2929
};
30+
31+
util.bindingVersion = 'v0.1.11-devel';

src/config.cc

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,8 @@ Conf * Conf::create(RdKafka::Conf::ConfType type, v8::Local<v8::Object> object,
8181
return NULL;
8282
}
8383
} else {
84-
v8::Local<v8::Function> cb = value.As<v8::Function>();
85-
rdconf->ConfigureCallback(string_key, cb, true, errstr);
86-
if (!errstr.empty()) {
87-
delete rdconf;
88-
return NULL;
89-
}
90-
rdconf->ConfigureCallback(string_key, cb, false, errstr);
91-
if (!errstr.empty()) {
92-
delete rdconf;
93-
return NULL;
94-
}
84+
// Do nothing - Connection::NodeConfigureCallbacks will handle this for each
85+
// of the three client types.
9586
}
9687
}
9788

@@ -100,56 +91,76 @@ Conf * Conf::create(RdKafka::Conf::ConfType type, v8::Local<v8::Object> object,
10091

10192
void Conf::ConfigureCallback(const std::string &string_key, const v8::Local<v8::Function> &cb, bool add, std::string &errstr) {
10293
if (string_key.compare("rebalance_cb") == 0) {
94+
NodeKafka::Callbacks::Rebalance *rebalance = rebalance_cb();
10395
if (add) {
104-
if (this->m_rebalance_cb == NULL) {
105-
this->m_rebalance_cb = new NodeKafka::Callbacks::Rebalance();
96+
if (rebalance == NULL) {
97+
rebalance = new NodeKafka::Callbacks::Rebalance();
98+
this->set(string_key, rebalance, errstr);
10699
}
107-
this->m_rebalance_cb->dispatcher.AddCallback(cb);
108-
this->set(string_key, this->m_rebalance_cb, errstr);
100+
rebalance->dispatcher.AddCallback(cb);
101+
this->set(string_key, rebalance, errstr);
109102
} else {
110-
if (this->m_rebalance_cb != NULL) {
111-
this->m_rebalance_cb->dispatcher.RemoveCallback(cb);
103+
if (rebalance == NULL) {
104+
rebalance->dispatcher.RemoveCallback(cb);
105+
this->set(string_key, rebalance, errstr);
112106
}
113107
}
114108
} else if (string_key.compare("offset_commit_cb") == 0) {
109+
NodeKafka::Callbacks::OffsetCommit *offset_commit = offset_commit_cb();
115110
if (add) {
116-
if (this->m_offset_commit_cb == NULL) {
117-
this->m_offset_commit_cb = new NodeKafka::Callbacks::OffsetCommit();
111+
if (offset_commit == NULL) {
112+
offset_commit = new NodeKafka::Callbacks::OffsetCommit();
113+
this->set(string_key, offset_commit, errstr);
118114
}
119-
this->m_offset_commit_cb->dispatcher.AddCallback(cb);
120-
this->set(string_key, this->m_offset_commit_cb, errstr);
115+
offset_commit->dispatcher.AddCallback(cb);
121116
} else {
122-
if (this->m_offset_commit_cb != NULL) {
123-
this->m_offset_commit_cb->dispatcher.RemoveCallback(cb);
117+
if (offset_commit != NULL) {
118+
offset_commit->dispatcher.RemoveCallback(cb);
124119
}
125120
}
126121
}
127122
}
128123

129124
void Conf::listen() {
130-
if (m_rebalance_cb) {
131-
m_rebalance_cb->dispatcher.Activate();
125+
NodeKafka::Callbacks::Rebalance *rebalance = rebalance_cb();
126+
if (rebalance) {
127+
rebalance->dispatcher.Activate();
132128
}
133129

134-
if (m_offset_commit_cb) {
135-
m_offset_commit_cb->dispatcher.Activate();
130+
NodeKafka::Callbacks::OffsetCommit *offset_commit = offset_commit_cb();
131+
if (offset_commit) {
132+
offset_commit->dispatcher.Activate();
136133
}
137134
}
138135

139136
void Conf::stop() {
140-
if (m_rebalance_cb) {
141-
m_rebalance_cb->dispatcher.Deactivate();
137+
NodeKafka::Callbacks::Rebalance *rebalance = rebalance_cb();
138+
if (rebalance) {
139+
rebalance->dispatcher.Deactivate();
140+
}
141+
142+
NodeKafka::Callbacks::OffsetCommit *offset_commit = offset_commit_cb();
143+
if (offset_commit) {
144+
offset_commit->dispatcher.Deactivate();
142145
}
146+
}
147+
148+
Conf::~Conf() {}
143149

144-
if (m_offset_commit_cb) {
145-
m_offset_commit_cb->dispatcher.Deactivate();
150+
NodeKafka::Callbacks::Rebalance* Conf::rebalance_cb() const {
151+
RdKafka::RebalanceCb *cb = NULL;
152+
if (this->get(cb) != RdKafka::Conf::CONF_OK) {
153+
return NULL;
146154
}
155+
return static_cast<NodeKafka::Callbacks::Rebalance*>(cb);
147156
}
148157

149-
Conf::~Conf() {
150-
if (m_rebalance_cb) {
151-
delete m_rebalance_cb;
158+
NodeKafka::Callbacks::OffsetCommit* Conf::offset_commit_cb() const {
159+
RdKafka::OffsetCommitCb *cb = NULL;
160+
if (this->get(cb) != RdKafka::Conf::CONF_OK) {
161+
return NULL;
152162
}
163+
return static_cast<NodeKafka::Callbacks::OffsetCommit*>(cb);
153164
}
154165

155166
} // namespace NodeKafka

src/config.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,19 @@ class Conf : public RdKafka::Conf {
3333
void stop();
3434

3535
void ConfigureCallback(const std::string &string_key, const v8::Local<v8::Function> &cb, bool add, std::string &errstr);
36-
protected:
37-
NodeKafka::Callbacks::Rebalance * m_rebalance_cb = NULL;
38-
NodeKafka::Callbacks::OffsetCommit * m_offset_commit_cb = NULL;
36+
37+
private:
38+
NodeKafka::Callbacks::Rebalance* rebalance_cb() const;
39+
NodeKafka::Callbacks::OffsetCommit *offset_commit_cb() const;
40+
41+
// NOTE: Do NOT add any members to this class.
42+
// Internally, to get an instance of this class, we just cast RdKafka::Conf* that we
43+
// obtain from RdKafka::Conf::create(). However, that's internally an instance of a sub-class,
44+
// ConfImpl.
45+
// This means that any members here are aliased to that with the wrong name (for example, the first
46+
// member of this class, if it's a pointer, will be aliased to consume_cb_ in the ConfImpl, and
47+
// and changing one will change the other!)
48+
// TODO: Just don't inherit from RdKafka::Conf, and instead have a member of type RdKafka::Conf*.
3949
};
4050

4151
} // namespace NodeKafka

0 commit comments

Comments
 (0)