Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature #2

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

Feature #2

wants to merge 8 commits into from

Conversation

SVS97
Copy link

@SVS97 SVS97 commented Jul 15, 2018

Changes to the training project.

  1. In makefile I disabled warnings (-Wall -Wextra -Wpedantic -Werror), because it doesn't work normally with Ubuntu.
  2. In db.c used functions, which absent in ANSI C. Proposed functions are more portable.
  3. Added icnlude guard

@thodnev
Copy link
Owner

thodnev commented Jul 16, 2018

In makefile I disabled warnings (-Wall -Wextra -Wpedantic -Werror), because it doesn't work normally with Ubuntu.

People managing packages at Ubuntu are "special". They decided that putting an old as hell 6th version gcc in a new distro would be a great idea. To fix the problem, you need to install a fresh gcc:
sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y
sudo apt-get update
sudo apt-get install gcc-8 g++-8 -y
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 60 --slave /usr/bin/g++ g++ /usr/bin/g++-8
If everything worked good, you should see version 8 in gcc -v output

I agree, the -Werror option seems overkill, but it's the only option that makes build fail (for some strange reason that "special" people built even new gcc with stupid default flags). Thus no need to suppress all warnings, removing -Werror will do the business.
For other stuff, see the code review (should appear ASAP)

Copy link
Owner

@thodnev thodnev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for contributing!
It'll be able to merge your changes after you fix problems mentioned in review
Also please pay attention to your git history. Squash your commits before PRing to keep project history clean

@@ -3,7 +3,7 @@ DEPS=simpledb
LIBS=zlib

CC=gcc
CFLAGS=-O2 -Wall -Wextra -Wpedantic -Werror
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that getting rid of -Werror seems good idea. But other options are useful indeed.
CFLAGS=-O2 -Wall -Wextra -Wpedantic

db.c Outdated
@@ -229,10 +230,10 @@ int main(int argc, char *argv[]) {
}

if (args.query) {
char *qval = strdup(args.query);
char *qval = malloc(sizeof(args.query)); /* Function in ANSI C, more portable */
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

args.query is a string. So what strdup essentially does is allocates the required memory and copies passed string there, returning the starting address. See man 3 strdup for more.
A sizeof is not a function, but operator. And it would never ever calculate the length of a string. Try adding a printf("sizeof(args.query)=%d chars", (int) sizeof args.query); and see the output.
We don't care about backwards compatibility, so no need for old ANSI C and K&R C stuff, use the latest things available

db.c Outdated
@@ -247,7 +248,7 @@ int main(int argc, char *argv[]) {
int lerr = E_OK;
switch(qtype) {
case Q_HELP:
printf(qhelp_docstring);
printf(qhelp_docstring,"\n");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good find. But here qhelp_docstring works as a format specifier (the same as printf("Hello world");). So to do it proper, it could be rewritten as:

printf("%s\n", qhelp_docstring);

@@ -1,4 +1,6 @@
#include "simpledb.h"
#ifndef __SIMPLEDB_H__
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include guards are usually not required given that you work with modern compiler.
For more, see #1 (comment)

simpledb.c Outdated
@@ -10,6 +12,8 @@
#include <string.h>
#include <errno.h>


Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a good reason for 2 extra newlines. Before committing, it's better to see git diff

@@ -18,7 +22,7 @@ enum err item_read_f(FILE *fp, struct dbitem *item)
uint8_t buffer[sizeof(*item) + HASH_SIZE];
if (sizeof(buffer) != fread(buffer, 1, sizeof(buffer), fp))
return E_FREAD;
uLong crc_file = *(uLong *)buffer;
uLong crc_file = *(uLong *)buffer; /* endianness-independency for zlib */
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good find! But <tab> is 8 spaces, not 4 (as required by CodingStyle)

simpledb.h Outdated
@@ -1,13 +1,14 @@
#include <limits.h>
#include <stdio.h>

#define SIMPLEDB_H
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useless stuff here

simpledb.h Outdated
@@ -1,13 +1,14 @@
#include <limits.h>
#include <stdio.h>

#define SIMPLEDB_H
#ifdef DEBUG
#define dbg(X, ...) do { \
fprintf(stderr, ("LOG> " X "\n"), ## __VA_ARGS__); \
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be good to allow this macro to return value, i.e.

#define dbg(X, ...)	({int __ret = fprintf(stderr, ("LOG> " X "\n"), ## __VA_ARGS__); __ret;})

But it relies on GCC extensions. See this link for more

@SVS97
Copy link
Author

SVS97 commented Jul 16, 2018

Thanks for your comments! I make the appropriate changes to my branch of the project. Please consider them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants