Skip to content

Commit

Permalink
fix: address review comments
Browse files Browse the repository at this point in the history
Refs: #59 (review)
Refs: #59 (comment)
Signed-off-by: Darshan Sen <raisinten@gmail.com>
  • Loading branch information
RaisinTen committed Jan 9, 2023
1 parent cde382d commit 6da6e04
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
6 changes: 3 additions & 3 deletions postject-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#ifndef POSTJECT_SENTINEL_FUSE
#define POSTJECT_SENTINEL_FUSE \
"POSTJECT_SENTINEL_fce680ab2cc467b6e072b8b5df1996b2:0"
"POSTJECT_SENTINEL_fce680ab2cc467b6e072b8b5df1996b2"
#endif

struct postject_options {
Expand All @@ -40,8 +40,8 @@ inline void postject_options_init(struct postject_options* options) {
}

static inline bool postject_has_resource() {
static const volatile char* sentinel = POSTJECT_SENTINEL_FUSE;
return sentinel[sizeof(POSTJECT_SENTINEL_FUSE) - 2] == '1';
static const volatile char* sentinel = POSTJECT_SENTINEL_FUSE ":0";
return sentinel[sizeof(POSTJECT_SENTINEL_FUSE)] == '1';
}

static const void* postject_find_resource(
Expand Down
21 changes: 14 additions & 7 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ async function inject(filename, resourceName, resourceData, options) {
const overwrite = options?.overwrite || false;
let sentinelFuse =
options?.sentinelFuse ||
"POSTJECT_SENTINEL_fce680ab2cc467b6e072b8b5df1996b2:0";
if (sentinelFuse.slice(-1) !== "0") {
throw new Error("Sentinel must end with '0'.");
}
sentinelFuse = sentinelFuse.slice(0, -1);
"POSTJECT_SENTINEL_fce680ab2cc467b6e072b8b5df1996b2";

if (!Buffer.isBuffer(resourceData)) {
throw new TypeError("resourceData must be a buffer");
Expand Down Expand Up @@ -136,13 +132,24 @@ async function inject(filename, resourceName, resourceData, options) {
);
}

const hasResourceIndex = firstSentinel + sentinelFuse.length;
const colonIndex = firstSentinel + sentinelFuse.length;
if (buffer[colonIndex] !== ":".charCodeAt(0)) {
throw new Error(
`Value at index ${colonIndex} must be ':' but '${buffer[
colonIndex
].charCodeAt(0)}' was found`
);
}

const hasResourceIndex = firstSentinel + sentinelFuse.length + 1;
const hasResourceValue = buffer[hasResourceIndex];
if (hasResourceValue === "0".charCodeAt(0)) {
buffer[hasResourceIndex] = "1".charCodeAt(0);
} else if (hasResourceValue != "1".charCodeAt(0)) {
throw new Error(
`Value at index ${hasResourceIndex} must be '0' or '1' but '${hasResourceValue}' was found`
`Value at index ${hasResourceIndex} must be '0' or '1' but '${hasResourceValue.charCodeAt(
0
)}' was found`
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ if (require.main === module) {
)
.option(
"--sentinel-fuse <sentinel_fuse>",
"Sentinel fuse",
"POSTJECT_SENTINEL_fce680ab2cc467b6e072b8b5df1996b2:0"
"Sentinel fuse for resource presence detection",
"POSTJECT_SENTINEL_fce680ab2cc467b6e072b8b5df1996b2"
)
.option("--output-api-header", "Output the API header to stdout")
.option("--overwrite", "Overwrite the resource if it already exists")
Expand Down

0 comments on commit 6da6e04

Please sign in to comment.